帮忙看一下为什么会输出NaN

来源:百度知道 编辑:UC知道 时间:2024/05/16 01:27:21
计算1-1/3+1/5-……+(-1)^k*1/(2k+1)
#include<stdio.h>
main()
{int k;
float a,s;
for(s=0,k=0;k<=1000;k++)
{a=pow(-1,k)*1.0/(2*k+1);
s+=a;
}
printf("the result is %f\n",s);
getch();
}
但输出NaN,请问为什么?
二楼抱歉,改后的程序还是输出NaN,用djgpp和win-tc都不行,如果两位还有耐心,请再帮忙想想(当然好的会加分呀)

调用函数问题吧!
#include<stdio.h>
#include<math.h>
int main()
{
float i=1.0,k=1.0;
double t=1.0,pi=0;
do
{
pi+=t;
i+=2;
k=-k;
t=k/i;
}
while(fabs(t)>=1e-6);
pi*=4;
printf("pi=%f\n",pi);
getch();
return 0;
}

帮你的程序修改了下:
#include<stdio.h>
#include<math.h>
main()
{int k;
float a,s=0;
for(k=0;k<=1000;k++)
{a=pow(-1,k)*1.0/(2*k+1);
s+=a;
}
printf("the result is %f\n",s);
getch();
}

没有问题,vc6.0结果是

the result is 0.785648

Press any key to continue