报错的原因应该是第7行 while(fabs(current))>1e-6(这里表达式错了, 改为 while(fabs(current)>1e-6)( ,然后给第4行的n赋初始值,然后运算过程修改下,修改如下:
参考链接:
C语言,求pi的值_Sunshine__love的博客-CSDN博客_求pi的值c语言
#include <stdio.h>
#include <math.h>
int main(void){
int n=2;
// float current = pow(-1,n)*(2*n-1);
float current = 1;
float sum=1;
//https://blog.csdn.net/Sunshine__love/article/details/77849295
while(fabs(current)>1e-6){
current=(float)(pow(-1.0,(double)(n-1))*1/(2*n-1));
// current=pow(-1.0,(double)(n-1))*1/(2*n-1);
sum+=current;
n++;
}
printf("pi is %f",4*sum);
return 0;
}
while(fabs(current) > 1e-6) {
while (fabs(current) > 1e-6) {
}