计算pi
利用公式π/4 = 1-1/3+1/5-1/7……计算π,直到最后一项的绝对值小于10的负6次方为止
程序代码:
#include<stdio.h> #include<stdlib.h> #include<math.h> int main() { int n = 1; int sign = 1; double pi = 0; double eps = 1.0 / n; //最后一项 while(fabs(eps) >= 1e-6) { pi += sign * eps; n += 2; sign *= -1; eps = sign * 1.0 / n; } pi *= 4; printf("pi = %.4f\n", pi); return 0; }
请问问题出在哪,我的结果是pi = 30.1717