改段程序,谢谢

来源:百度知道 编辑:UC知道 时间:2024/07/03 10:28:26
#include "stdio.h"
#include "conio.h"
#include "math.h"
int main(void)
{
int s;
float n,t,pi;
t=1;
pi=0;
n=1.0;
s=1;
while (fabs(t)>1e-6)
{
pi=pi+t;
n=n+2;
s=-s;
t=s/n;
}
pi=pi*4;
printf ("pi=%.6f\n",pi);
getch ();
return 0;

}

结果的最后一位有问题,还有,怎么改可以输出更多位数?

float类型改为double类型

double类型的范围是正负1.7E308(精确到15位小数)

想用更长可以用long double,范围正负1.19E4932(精确到18位小数)

我写的这个简练,和你的思路应该是一致的,你看看应该能明白,结果也是对的,在vc上调试过了.
#include <math.h>
#include <stdio.h>
void main()
{
int s=1;float n=1,t=1,pi=0;
while(fabs(t)>=1e-7)
{
pi+=t;
s=-s;
n+=2;
t=s/n;
}
printf("%.9f\n",4*pi);

}

把用float定义的改成用double来定义