Core Library: Computing Pi
Computing Pi
We implement Machin's formula for computing
Pi = 3.14159265358979323846264338327950288419716939937510582097494459...
to any desired precision. It demonstrates the ease with which
such high precision computations can be achieved using
Core library's precision mechanism and its numerical output facility.
Here is Machin's formula:
pi/4 = 4 arctan(1/5) - arctan(1/239)
= \sum_{k=0} ^ \infty (-1)k
[ 4 / ( (2k +1) 5^{2k+1} )
- 1 / ( (2k +1) 239^{2k+1} ) ]
Timing: Using this formula, a Sun Ultra 10 (440 MHz) can
compute Pi to 2000 digits (=6644 bits) in about 1.51 seconds.
(This is Core 1.3, June 2000)
In some formulas (e.g., for erf(x)), there is also a need for high precision
values of sqrt(Pi). The program also outputs sqrt(Pi).
The program here also performs automatic self-verification of the computed
values of Pi up to 250 digits, and sqrt(Pi) up to 100 digits.
Update for Core 1.7, Nov 2004
We originally implemented Brent's algorithm for Pi using Expr, and
it turned out to be much slower than using Machin's formula.
In "newPi.cpp", we re-implemented Brent's algorithm by replacing
Expr by BigFloat. This became much faster than the old Machin's
algorithm. We also reimplemented Machin's formula "newPi.cpp".
In particular, we use binary splitting to compute
arctan(1/x) where x is a bigInt. The BigFloat version
of Brent algorithm is again slower than Machin's formula.
Both are much faster than Maple.
Using the same Solaris SunBlade as above to compute to
100,000 bits (30102 digits) (timing does not include printout of digits).
| TEST | TIME |
COMMENT |
|
newPi 100000 0 | 3.35 sec |
Machin's formula using binary split |
|
newPi 100000 1 | 33.3 sec |
Brent's algorithm using bigFloat |
FILES:
-
- newPi.cpp:
CORE 1.7 program to compute Pi using
improved Machin's formula or improved Brent's algorithm.
- pi.cpp:
CORE 1.3 program to compute Pi using Machin's formula.
-
- PI2000:
2000 digits of Pi
-
- TARFILE:
Tar file for compiling and running program