jun-csbio / CClass_ZJUT

CClass_ZJUT
1 stars 0 forks source link

写的那个4-7的近似值的问题 #31

Open Flyces opened 1 year ago

Flyces commented 1 year ago
屏幕截图 2023-10-18 193503

#include <stdio.h>
#include <math.h>

int main(){
    double x=0.5 , n=1 , sum=0 , jc=1 , re ;
    for(;fabs(re)>=1e-6;n++){
    jc=jc*n;
    re=(pow((-1),n)*pow(x,2*n+1))/((2*n+1)*jc);
    sum+=re;
    }
    printf("the end number is: %.8lf\nthe result is %.8lf",re,sum);
    return 0;
}   
Flyces commented 1 year ago

找出了问题所在,第一次执行就跳过了for循环导致最后结果异常,重新初始化了之后就能正常输出了


#include <stdio.h>
#include <math.h>

int main(){
    double x=0.5 , n=1 , sum=0 , jc=1 , re , a , b , c ;
    re=x;
    for(;fabs(re)>=0.000001;n++){
    jc=jc*n;
    a=pow((-1),n);
    b=pow(x,2*n+1);
    re=(a*b)/((2*n+1)*jc);
    sum+=re;
    }
    printf("the last number is: %.8lf\nthe result is %.8lf",re,sum);
    return 0;
}