所以我写了这段代码来找出PI估计值和固定理论值之间的相对误差(3.14159)。但每当我运行程序时,我都会在命令行中得到这个:“5个项后pi约为-1。#INF00”,相对误差值相同。我不确定是什么问题
int main(void)
{
float i, n, error;
float term = 0;
printf("Enter the number of terms: ");
scanf("%f", &n);
for(i=0; i<=n; i++)
{
term=term + pow(-1.0, i+1)/(i*i);
}
error=fabs(term-3.141593)/(3.141593);
printf("The actual value of pi is %f\n", 3.141593);
printf("After %0.0f terms pi is apporximately %f\n", n, term);
printf("The relative error is %f %", error);
return 0;
}