格里高利公式:
#include "stdio.h" #include "math.h" int main(void) { double pi4 = 0; double j = 0; double i = 1; while (true) { j = pow(-1, i + 1) * (1.0 / (2 * i - 1)); //求绝对值 if (fabs(j) <= 0.0001) break; else { pi4 += j; i++; } } printf("π = %f\n", pi4 * 4); return 0; }