BBP-Algorithm in Python - Working with Arbitrary-precision arithmetic

Tim

I am writing on a term paper about the calculation of pi. While i have finished the theoretical site, i`m now struggeling implementing the BBP-Algorithm in Python.

You can find the BBP-Algorithm here: http://en.wikipedia.org/wiki/Bailey%E2%80%93Borwein%E2%80%93Plouffe_formula

And this is my Implementation in Python:

from sympy.mpmath import *
pi = mpf(0)
mp.dps = 30;
for n in range(0,500):
    pi = pi + mpf((1/16**n)*(4/(8*n+1)-  2/(8*n+4)-  1/(8*n+5)-  1/(8*n+6)) )


print(pi)

My Problem is, no matter how high i set k or how high i set the decimal places for pi i can`t get a higher precision than 16 digits.

I used mpmath for higher precision, because i encountered some problems before.

How can i improve my code, so i can get more digits ?

Willem Van Onsem

By default, python will use the standard floating points as defined by IEEE-754. This has a precision of some 12 digits and can represent numbers as lows as 2-1022, now you can resolve this problem by calling the mpf operator earlier in the process, a more readable and more precise version is thus:

from sympy.mpmath import *
pi = mpf(0)
mp.dps = 30;
for n in range(0,500):
    u = 4.0/(8*n+1)-2.0/(8*n+4)-1.0/(8*n+5)-1.0/(8*n+6)
    u = mpf(u)
    d = mpf(16.0/1.0)**n
    pi += u/d

print(pi)

This however still has problems when you calculate the u part. To do it exactly, you can use:

from sympy.mpmath import *
pi = mpf(0)
mp.dps = 50;
for n in range(0,50000):
    u = mpf(4)/mpf(8*n+1)-mpf(2)/mpf(8*n+4)-mpf(1)/mpf(8*n+5)-mpf(1)/mpf(8*n+6)
    d = mpf(16.0)**n
    pi += u/d

print(pi)

Which is correct for the first 50 digits:

3.1415926535 8979323846 2643383279 502884197 1693993751

(spaces added)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Scheme Lisp - Using eval for arbitrary arithmetic expressions

From Dev

LLVM arbitrary precision integer

From Dev

java algorithm calculation of precision

From Dev

Best algorithm for detecting interior and exterior angles of an arbitrary shape in python

From Dev

Is Python incorrectly handling this "arbitrary precision integer"?

From Dev

Arbitrary precision arithmetic with very big factorials

From Dev

Fixed precision vs. arbitrary precision

From Dev

Fast arbitrary-precision logarithms with bcmath

From Dev

Python mpmath not arbitrary precision?

From Dev

postgresql date_trunc to arbitrary precision?

From Dev

Is it possible to achieve arbitrary-precision arithmetic with no rounding issues in JavaScript?

From Dev

Maya Python... workaround for arbitrary precision when coding with maya.cmds

From Dev

arbitrary floating precision number to string

From Dev

Arbitrary precision for decimals square roots in golang

From Dev

Is there an equivalent of Python's fractions.Fraction (bignum/arbitrary-precision fraction/rational number class)?

From Dev

Calculating pi without arbitrary precision and only basic arithmetic

From Dev

awk high precision arithmetic

From Dev

"Multiplication of Arbitrary Precision Numbers" in Scheme

From Dev

Arbitrary precision arithmetic with very big factorials

From Dev

Losing precision (using GMP) when calculating pi with sequential bbp

From Dev

LLVM arbitrary precision integer

From Dev

Python base conversion algorithm not working?

From Dev

awk high precision arithmetic

From Dev

Maya Python... workaround for arbitrary precision when coding with maya.cmds

From Dev

Calculating pi without arbitrary precision and only basic arithmetic

From Dev

Working with increased precision of irrational numbers in python

From Dev

Arithmetic precision problems with large numbers

From Dev

stripe node arbitrary precision number

From Dev

Calculate Pi with arbitrary precision digit by digit

Related Related

  1. 1

    Scheme Lisp - Using eval for arbitrary arithmetic expressions

  2. 2

    LLVM arbitrary precision integer

  3. 3

    java algorithm calculation of precision

  4. 4

    Best algorithm for detecting interior and exterior angles of an arbitrary shape in python

  5. 5

    Is Python incorrectly handling this "arbitrary precision integer"?

  6. 6

    Arbitrary precision arithmetic with very big factorials

  7. 7

    Fixed precision vs. arbitrary precision

  8. 8

    Fast arbitrary-precision logarithms with bcmath

  9. 9

    Python mpmath not arbitrary precision?

  10. 10

    postgresql date_trunc to arbitrary precision?

  11. 11

    Is it possible to achieve arbitrary-precision arithmetic with no rounding issues in JavaScript?

  12. 12

    Maya Python... workaround for arbitrary precision when coding with maya.cmds

  13. 13

    arbitrary floating precision number to string

  14. 14

    Arbitrary precision for decimals square roots in golang

  15. 15

    Is there an equivalent of Python's fractions.Fraction (bignum/arbitrary-precision fraction/rational number class)?

  16. 16

    Calculating pi without arbitrary precision and only basic arithmetic

  17. 17

    awk high precision arithmetic

  18. 18

    "Multiplication of Arbitrary Precision Numbers" in Scheme

  19. 19

    Arbitrary precision arithmetic with very big factorials

  20. 20

    Losing precision (using GMP) when calculating pi with sequential bbp

  21. 21

    LLVM arbitrary precision integer

  22. 22

    Python base conversion algorithm not working?

  23. 23

    awk high precision arithmetic

  24. 24

    Maya Python... workaround for arbitrary precision when coding with maya.cmds

  25. 25

    Calculating pi without arbitrary precision and only basic arithmetic

  26. 26

    Working with increased precision of irrational numbers in python

  27. 27

    Arithmetic precision problems with large numbers

  28. 28

    stripe node arbitrary precision number

  29. 29

    Calculate Pi with arbitrary precision digit by digit

HotTag

Archive