![]() |
![]() |
In this case the power series is used with x = 1/5 and x = 1/239, getting much more rapid convergence.
![]() |
The following computation is given as a comparison with the averaging formula that computes pi to 16 digits with 94 operations on doubles. The computation below uses 39 operations on doubles to get 15 digits of pi.
C program to calculate pi |
---|
int main() { double x0, x1, x2, x3, x0s; double t0, t1, t2, t3; double p; // * / + - x0 = 1./57; x0s = x0*x0; // 1 1 0 0 x1 = x0*x0s; x2 = x1*x0s; // 2 0 0 0 x3 = x2*x0s; // 1 0 0 0 t0 = x0 - x1/3 + x2/5 - x3/7; // 0 3 1 2 x0 = 1./239; x0s = x0*x0; // 1 1 0 0 x1 = x0*x0s; x2 = x1*x0s; // 2 0 0 0 t1 = x0 - x1/3 + x2/5; // 0 2 1 1 x0 = 1./682; x0s = x0*x0; // 1 1 0 0 x1 = x0*x0s; x2 = x1*x0s; // 2 0 0 0 t2 = x0 - x1/3 + x2/5; // 0 2 1 1 x0 = 1./12943; x1 = x0*x0*x0; // 2 1 0 0 t3 = x0 - x1/3; // 0 1 0 1 p = 176*t0+28*t1-48*t2+96*t3; // 4 0 2 1 printf("pi: %20.16f\n", p); // Totals: 16 12 5 6 // * / + - // Grand total: 39 } % ./greg pi: 3.1415926535897900 |
![]() |
Here is a very similar C program that uses 43 operations on doubles to compute pi correct to 16 digits.
C program to calculate pi |
---|
int main() { double x0, x1, x2, x3, x4, x5, x6, x7, x8, x0s; double t0, t1; double p; // * / + - x0 = 1./7; x0s = x0*x0; // 1 1 0 0 x1 = x0*x0s; x2 = x1*x0s; // 2 0 0 0 x3 = x2*x0s; x4 = x3*x0s; // 2 0 0 0 x5 = x4*x0s; x6 = x5*x0s; // 2 0 0 0 x7 = x6*x0s; x8 = x7*x0s; // 2 0 0 0 t0 = x0 - x1/3 + x2/5 - // 0 2 1 2 x3/7 + x4/9 - x5/11 + // 0 3 2 1 x6/13 - x7/15 + x8/17; // 0 3 1 1 x0 = 3./79; x0s = x0*x0; // 1 1 0 0 x1 = x0*x0s; x2 = x1*x0s; // 2 0 0 0 x3 = x2*x0s; x4 = x3*x0s; // 2 0 0 0 t1 = x0 - x1/3 + x2/5 - // 0 2 1 2 x3/7 + x4/9; // 0 2 1 0 p = 20*t0 + 8*t1; // 2 0 1 0 printf("pi: %20.16f\n", p); // Totals: 16 14 7 6 // * / + - // Grand total: 43 } % ./greg pi: 3.1415926535897931 |