Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to get many places of pi from Machin's Equation?

2 views
Skip to first unread message

Richard D. Moores

unread,
Jan 9, 2010, 6:31:49 AM1/9/10
to pytho...@python.org
Machin's Equation is

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

John Machin

unread,
Jan 9, 2010, 9:12:36 AM1/9/10
to

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")

Gabriel Genellina

unread,
Jan 9, 2010, 9:42:16 AM1/9/10
to pytho...@python.org
En Sat, 09 Jan 2010 08:31:49 -0300, Richard D. Moores <rdmo...@gmail.com>
escribiᅵ:

> 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

Mark Dickinson

unread,
Jan 9, 2010, 10:31:18 AM1/9/10
to
On Jan 9, 11:31 am, "Richard D. Moores" <rdmoo...@gmail.com> wrote:
> 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?

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

Mark Dickinson

unread,
Jan 9, 2010, 10:57:05 AM1/9/10
to
On Jan 9, 11:31 am, "Richard D. Moores" <rdmoo...@gmail.com> wrote:
> Machin's Equation is
>
> 4 arctan (1/5) - arctan(1/239) = pi/4
> [...]

>
> 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?

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))

Richard D. Moores

unread,
Jan 9, 2010, 11:32:48 AM1/9/10
to Mark Dickinson, pytho...@python.org
> --
> http://mail.python.org/mailman/listinfo/python-list
>

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

0 new messages