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 debug[email protected] to delete if infringement.

edited at

- Prev: How do I use the latest version of Zend Framework 2.3.5 using Composer PHP?
- Next: Ajax with Rails, appending div not working

*1*### Scheme Lisp - Using eval for arbitrary arithmetic expressions

*2*### LLVM arbitrary precision integer

*3*### java algorithm calculation of precision

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

*5*### Is Python incorrectly handling this "arbitrary precision integer"?

*6*### Arbitrary precision arithmetic with very big factorials

*7*### Fixed precision vs. arbitrary precision

*8*### Fast arbitrary-precision logarithms with bcmath

*9*### Python mpmath not arbitrary precision?

*10*### postgresql date_trunc to arbitrary precision?

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

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

*13*### arbitrary floating precision number to string

*14*### Arbitrary precision for decimals square roots in golang

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

*16*### Calculating pi without arbitrary precision and only basic arithmetic

*17*### awk high precision arithmetic

*18*### "Multiplication of Arbitrary Precision Numbers" in Scheme

*19*### Arbitrary precision arithmetic with very big factorials

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

*21*### LLVM arbitrary precision integer

*22*### Python base conversion algorithm not working?

*23*### awk high precision arithmetic

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

*25*### Calculating pi without arbitrary precision and only basic arithmetic

*26*### Working with increased precision of irrational numbers in python

*27*### Arithmetic precision problems with large numbers

*28*### stripe node arbitrary precision number

*29*### Calculate Pi with arbitrary precision digit by digit

## Comments