稍微变动的地方,在于偶尔发现阶乘会因为太大而溢出,所以换成了long long
#include <stdio.h>
#include <math.h>
#define pi 3.1415926
double mysin(double x)
{
long long num = 1, fuhao = 1, jiecheng = 1;
long double sum = x, each;
long double y = x;
do
{
fuhao *= -1;
y *= x * x;
jiecheng *= (num + 1) * (num + 2);
each = fuhao * y / jiecheng;
sum += each;
num += 2;
} while (fabs(each) >= 1e-7);
return sum;
}
int main()
{
printf("%.7f\n", sin(pi));
printf("%.7f", mysin(pi));
return 0;
}