Skip to content

jxu/PiCalc

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PyPi

Several algorithms for arbitrary precision calculation of pi. Plus, how could I pass up that name? (Edit: Old name was taken by the package index. It doesn't apply anymore, anyway.) Based on Nick Craig-Wood's python implementations, but with my own gmpy2 and C++ changes. Includes an experimental timing program to verify certain functions. Rewritten in C++ and with gmp for speed.

To do:

  • Better documentation
  • Multi-threading: a possibility?
  • Code profiling

Algorithms

Details in the code.

Machin: Uses the formula pi/4 = 4*arctan(1/5) - arctan(1/239). Features gmpy2's hardware level algorithm and an accelerated formula by Euler for arctan.

Chudnovsky: Uses a very fast rapidly convergent algorithm. Includes a regular implementation with gmpy2 and a binary-splitting method with gmpy2.

Gauss-Legendre: Uses a second order convergence algorithm based on the arithmatic-geometric mean. Only log2(n) iterations are needed.

Borwein: Just for fun, PyPi includes Borwein's 1984 algorithm with quartic convergence. Not as fast as Gauss-Legendre though, probably because of the 4 divisions per iteration.

Benchmarks

Python

Digits >>>          10,000        100,000        1,000,000    10,000,000    100,000,000
---------------------------------------------------------------------------------------
Machin gmpy2        0.06 s        1.7 s          30.5 s       542.9 s       -
Machin Taylor       0.4 s         38.9 s         -            -             -
Machin Euler        0.3 s         34.4 s         -            -             -
Chudnovksy regular  0.01 s        0.7 s          79.7 s       -             -
Chudnovsky BS       0.004 s       0.1 s          1.6 s        28.9 s        432.3 s
Gauss-Legendre      0.01 s        0.4 s          5.8 s        101.5 s       -
Borwein             0.05 s        1.4 s          22.6 s       327.8 s       -

C++

Digits >>>          10,000        100,000        1,000,000    10,000,000    100,000,000
---------------------------------------------------------------------------------------
Chudnovsky BS       0.03 s        0.09 s         1.3 s        21.2 s        331.1 s

###Speed Optimizations (Python)

  • Replaced gmpy2.log2 with math.log2 (less precision)
  • Replaced gmpy2 integer square root multiplied by scale**2 with mpfr square root
    • 33% less time
  • Use mpz instead of int exponentiation for scale
    • 50% less time

About

Generate digits of pi

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published