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

  • Thread starter Richard D. Moores
  • Start date
R

Richard D. Moores

Machin's Equation is

4 arctan (1/5) - arctan(1/239) = pi/4

Using Python 3.1 and the math module:

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
 
J

John Machin

Machin's Equation is

4 arctan (1/5) - arctan(1/239) = pi/4

Using Python 3.1 and the math module:



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?

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

Mark Dickinson

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
 
M

Mark Dickinson

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

Richard D. Moores

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

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top