#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 factorial(int x)
{
double sum = 1;
for(int i = 1; i <= x; i++)
{
sum *= i;
}
return sum;
}
double funcos( double e, double x )
{
double res = 1;
int k = 1;
double sum = 0;
for(int i = 0; res >= e; i += 2)
{
res = pow(x, i)/factorial(i);
sum += res*k;
k *= -1;
}
return sum;
}
当 factorial 函数返回值为double时, 结果正确
当 factorial 函数返回值为int时, 结果不对, 这是为什么?