Why is math.pi slightly wrong?

Mensanator mensanator at aol.com
Fri May 23 14:44:19 EDT 2008


On May 23, 12:39 am, Andrew Lee <fiacre.patr... at gmail.com> wrote:
> Mensanator wrote:
> > On May 22, 11:32 am, "Dutton, Sam" <Sam.Dut... at itn.co.uk> wrote:
> >> I've noticed that the value of math.pi -- just entering it at the interactive prompt -- is returned as 3.1415926535897931, whereas (as every pi-obsessive knows) the value is 3.1415926535897932... (Note the 2 at the end.)
>
> >> Is this a precision issue, or from the underlying C, or something else? How is math.pi calculated?
>
> > If you actually need that many digits, use a different library.
>
> >>>> import gmpy
>
> >>>> print gmpy.pi(64) # 64 bit precision
> > 3.14159265358979323846
> >>>> print gmpy.pi(128) # 128 bit precision
> > 3.141592653589793238462643383279502884197
> >>>> print gmpy.pi(16384) # 16384 bit precision
> > 3.14159265358979323846264338327950288419716939937510582097494459
> > 23....
>
> Heh!
>
> I wonder who needs that many digits?

Pi digits specifically, or that many digits
in general? I generally don't use pi, but
I routinely work with numbers having that
many digits.

>
> Certainly not number theorists (they need a LOT more).

That depends on the problem. What's important
to the number theorist is not how many digits
there are, but the fact that every single one
of them is significant. If pi is needed, the
mantissa must have as many digits as the number
to avoid loss of significance.

For example, in the Collatz Conjecture, there
is a nice closed form equation to tell you the
ith, kth Generation Type [1,2] Mersenne Hailstone:

a(i,k) = 2**(6*((i-1)*9**(k-1)+(9**(k-1)-1)/2+1)-1)-1

Which for a modest request like a(1,6) returns
a number with 53,338 decimal digits. And you
can't even compute the 11th generation without
getting an "Outrageous Exponent" exception
(the exponent exceeded 32 bits).

Now, it's true you can't count up to such a
number, but you can readily determine its Collatz
Sequence as they are logarithmic (for a(1,6) the
sequence contains 854,697 odd numbers and about
twice that many even numbers).

And since a(1,6) has only 177,149 bits, it's
easy to verify that it is, indeed, the first
of the infinite members of the 6th generation.

> Certainly not
> physicists -- they need about 30 digits to be within 1% of any
> measurement from molecules to galaxies.  Certainly not engineers, they
> need half the digits that physicists need.  Cryptographers are making a
> dire mistake if they are using PI in any computations (possible
> exception for elliptic curves -- see number theorists, above) ... so --
> other than PI-philes, who needs PI to thousands of digits?- Hide quoted text -
>
> - Show quoted text -




More information about the Python-list mailing list