4 arctan (1/5) - arctan(1/239) = pi/4
Using Python 3.1 and the math module:
>>> from math import atan, pi
>>> pi
3.141592653589793
>>> (4*atan(.2) - atan(1/239))*4
3.1415926535897936
>>> (4*atan(.2) - atan(1/239))*4 == pi
False
>>> abs((4*atan(.2) - atan(1/239))*4) - pi < .000000000000000001
False
>>> abs((4*atan(.2) - atan(1/239))*4) - pi < .0000000000000001
False
>>> abs((4*atan(.2) - atan(1/239))*4) - pi < .000000000000001
True
>>>
Is there a way in Python 3.1 to calculate pi to greater accuracy using
Machin's Equation? Even to an arbitrary number of places?
Thanks,
Dick Moores
Considering that my namesake calculated pi to 100 decimal places with
the computational equipment available in 1706 (i.e. not much), I'd bet
you London to a brick that Python (any version from 0.1 onwards) could
be used to simulate his calculations to any reasonable number of
places. So my answers to your questions are yes and yes.
Suggestion: search_the_fantastic_web("machin pi python")
> Is there a way in Python 3.1 to calculate pi to greater accuracy using
> Machin's Equation? Even to an arbitrary number of places?
You may be interested in Demo/scripts/pi.py in the source distribution. It
can generate pi with infinite precision, limited by available memory only.
And this thread from last month:
http://groups.google.com/group/comp.lang.python/t/e37bb8c59f2e5582/
--
Gabriel Genellina
There's no arbitrary-precision version of atan included with Python.
You could write your own (e.g., based on argument reduction + Taylor
series) for use with the decimal module, or you could use one of the
various 3rd party arbitrary-precision arithmetic packages that do
provide atan.
Mark
Here's some crude code (no error bounds, possibility of infinite
loops, ...) that computes pi to 1000 places using Machin's formula and
the decimal module. The last few digits will be bogus, and should be
ignored.
from decimal import Decimal, getcontext
def atan(x):
# reductions
reductions = 0
while 100*abs(x) > 1:
reductions += 1
x /= 1 + (1+x*x).sqrt()
# Taylor series
sum = 0
xpow = x
x2 = x*x
k = 1
while True:
term = xpow/k
oldsum = sum
sum += term
if sum == oldsum:
break
k += 2
xpow *= -x2
return sum * 2**reductions
getcontext().prec = 1000
one = Decimal(1)
print(16*atan(one/5) - 4*atan(one/239))
Great! Done in Python 3 with arctan and the decimal module. And the
first 997 digits were accurate.
Just what I was after.
I don't believe the Chudnovsky algorithm has been mentioned. It isn't
what I wanted, but it is amazing. (<http://pastebin.com/f2a77629f>)
Thanks, everyone!
Dick Moores