为什么当e=0.001时答案错了哪里需要改
#include <stdio.h>#include <math.h>
double funcos( double e, double x );
int main() {
double e, x;
scanf("%lf %lf", &e, &x);
printf("cos(%.2f) = %.6f\n", x, funcos(e, x));
return 0;
}
double funcos( double e, double x ) {
int fact,i;
int flag=1,b=0;
double c=1,a,result=0;
do {
if(e<0)
e=-e;
a=pow(x,b);
fact=1;
for(i=1; i<=b; i++) {
fact*=i;
}
c=a/fact;
flag=-flag;
result+=c*flag;
b+=2;
} while(c>e);
return result;
}
本帖最后由 jackz007 于 2021-11-15 23:52 编辑
试试这个函数呢?
double funcos(double e , double x)
{
double b , c , d , i , s , v ;
for(b = c = d = v = 1.0 , s = -1.0 , i = 2 ; d >= e ; i += 2 , s = - s) {
c *= x * x ;
b *= i * (i - 1) ;
e = c / b ;
v += s * e ;
}
return v ;
}
页:
[1]