實驗5-10 使用函式求餘弦函式的近似值 (15分)
阿新 • • 發佈:2019-02-20
http://pta.patest.cn/pta/test/13/exam/3/question/468
#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 ) {
double sum=0,d=100;
int i=0,j;
//注意此處為坑,必須為double型不能為整型,整型精度不夠
double cj;
int count=0;
while(d>e) {
count++;
if(count%2!=0) {
cj=1;
for(j=1; j<=i; j++) {
cj=cj*j;
}
d=pow(x,i)/cj;
sum=sum+d;
i+=2;
} else {
cj=1;
for(j=1; j<=i; j++) {
cj=cj*j;
}
d=pow(x,i)/cj;
sum=sum-d;
// d=pow(x,i)/cj;
i+=2;
//d=pow(x,i)/cj;
}
}
return sum;
}