I have written a little program that works out Pi using atan. I can get it to give me about 50 decimal places, but I really don't understand the "Decimal" module. If anyone can give me some tips on how to get a more precise result, I'd appreciate it.

Here's my code:

Code: Select all

```
# pi.py - arctan(1) * 4 = pi
from math import *
from decimal import *
err = 0.0000000000000001
def arctan(n, err):
"""Uses Gregory's formula for calcuating atan."""
temp = n
atan = 0
i = 3
while (abs(atan - n) > err):
atan = n
n = n - (pow(temp, i)/i) + ((pow(temp, i + 2)) / (i + 2))
i += 4
return n
# set up the start of the fibonacci series
a = 1
b = 2
atan = 0
temp = 5
# this calculates pi/4 using Euler's formula and fibonacci numbers
while (abs(temp - atan) > err):
temp = atan
atan += arctan(1/b, err)
a = b + a
b = b + a
atan += arctan(1/b, err)
pi = atan * 4
print(Decimal.from_float(pi))
```

Code: Select all

`3.141592653589793560087173318606801331043243408203125`

mark