<p>我写了这个程序来计算π。它给了我50个小数位。我怎样才能更精确?</p>
<pre><code># pi.py - arctan(1) * 4 = pi
from math import *
from decimal import *
err = 0.0000000000000001
def arctan(n, err):
"""Uses Gregory's formula for calculating 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
def euler(a, b, err):
"""Uses Euler's formula and fibonacci numbers."""
euler = 0
temp = 5
while (abs(temp - euler) > err):
temp = euler
euler += arctan(1/b, err)
a = b + a
b = b + a
return euler
pi = euler(1, 2, err) * 4
print(Decimal.from_float(pi))
</code></pre>