本题要求实现一个函数,用下列公式求cos(x)的近似值,精确到最后一项的绝对值小于e: cos(x)=x 0 /0!−x 2 /2!+x 4 /4!−x 6 /6!+⋯
#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 fun(int h){ if(h==1||h==0)return 1; else return h*fun(h-1); } double funcos(double e, double x){ double sum=1,p=0,m=1; while(1){ p=p+2; m*=-1; sum+=m*(pow(x,p)/fun(p)); if(pow(x,p)/fun(p)<e) break; } return sum; }

浙公网安备 33010602011771号