编写程序c5-2-10.c,实现的功能是:按下面公式,求π的近似值(保留四位小数),直到最后一项中的分数小于10-6为止。
答案没错
这是精度问题
你把那个循环条件改成10的负8次方结果就是3.1415
你看看这个
#include<stdio.h>
#include<math.h>
main()
{
int s;
float n,t,pi;
t=1.0;
pi=0;
n=1.0;
s=1;
while(fabs(t)>=1e-6)
{
pi=pi+t;
n+=2.0;
s=-s;
t=s/n;
}
pi=pi*4;
printf("pi=%f\n",pi);
}