为什么要加fabs?
#include "stdio.h"
#include "math.h"
main()
{ int s=1;
float n=1.0,t=1.0,pi=0;
while(fabs(t)>1e-4){这一句里面为什么要加fabs?
pi=pi+t;
n+=2.0;
s=-s;
t=s/n;
}
pi*=4;
printf("%f\n",pi);
}
参考答案:取绝对值嘛 t有可能为负
#include "stdio.h"
#include "math.h"
main()
{ int s=1;
float n=1.0,t=1.0,pi=0;
while(fabs(t)>1e-4){这一句里面为什么要加fabs?
pi=pi+t;
n+=2.0;
s=-s;
t=s/n;
}
pi*=4;
printf("%f\n",pi);
}
参考答案:取绝对值嘛 t有可能为负