C语言
#include <stdio.h>#include <math.h>
int main()
{
int sign = 1,count;
double pi = 0.0,n = 1.0,term = 1.0;
for(count = 0;fabs(term)>=1e - 6;count++)
{
pi +=term;
n += 2;
sign = -sign;
term = sign / n;
}
printf("pi = %10.2f\n",pi);
printf("fabs(t)>=1e-6时,循环次数为%d\n",count);
for(count = 0;fabs(term)>=1e - 8;count++)
{
pi +=term;
n += 2;
sign = -sign;
term = sign / n;
}
printf("pi = %10.2f\n",pi);
printf("fabs(t)>=1e-8时,循环次数为%d\n",count);
}
这个程序编译的错误是 error C2021: expected exponent value, not ' ',
有大神能够告诉这个程序错在哪里吗? 本帖最后由 sunrise085 于 2020-4-27 09:52 编辑
1e-8和1e-6中间为什么会有空格?
一百二十三能写成123?
pi的结果,你忘记乘以4了
另外printf的时候只显示两位小数看不出效果的。
#include <stdio.h>
#include <math.h>
int main()
{
int sign = 1,count;
double pi = 0.0,n = 1.0,term = 1.0;
for(count = 0;fabs(term)>= 1e-6;count++)
{
pi +=term;
n += 2;
sign = -sign;
term = sign / n;
}
printf("pi = %.6lf\n",pi*4);
printf("fabs(t)>=1e-6时,循环次数为%d\n",count);
for(count = 0;fabs(term)>=1e-8;count++)
{
pi +=term;
n += 2;
sign = -sign;
term = sign / n;
}
printf("pi = %.8lf\n",pi*4);
printf("fabs(t)>=1e-8时,循环次数为%d\n",count);
}
页:
[1]