利用 pi/4=1-1/3+1/5-1/7...公式求pi的近似值,当某一项的绝对值小于10-6为止

我是这样写的
#include<stdio.h>
int main()
{
int i,flag=1;
double s=0;
for(i=1;1.0/(2*i-1)>=(1e-6);i++)
{
s=s+flag*1.0/(2*i-1);
flag=-flag;
}
printf("%.9lf",s*4);
return 0;
}
最后的结果为3.141590654.
想知道为什么不准确。谢谢

define N 1e-5
main()

int i,f
double t,s
f=1
s=0
i=1
t=1.0
while (fabs(t)>=N)

s=s+t
f=-f
t=f*1.0/(2*i+1);
i++;
s=s*4
printf("圆周率的近似值为:%f\n",s);
温馨提示:答案为网友推荐,仅供参考
第1个回答  2020-11-30
把9lf改成6lf
相似回答