标签:pre double include int turn printf 精确 color 语言
终止条件: 某一项: 1 / n+2 < 10 ^ -6;
1e-6即0.000001,
代码如下:
1 #include<stdio.h> 2 #include <math.h> 3 4 int main() { 5 double quarter = 0; 6 double PI = 0; 7 int sign = -1; 8 double term = 1.0; 9 for ( ; 1.0 / term > 1e-6; ) { 10 // pow(10, -6) == 1e-6 11 sign = -1 * sign; 12 quarter = quarter + sign * (1.0 / term); 13 term = term + 2; 14 } 15 PI = quarter * 4; 16 printf("%.14f\n" ,PI); 17 return 0; 18 }
这里能算出精确到小数后14位,后面都是0.
结果:
3.141590653589692
标签:pre double include int turn printf 精确 color 语言
原文地址:https://www.cnblogs.com/m-yb/p/11537888.html