Why is math.pi slightly wrong?

Andrew Lee fiacre.patrick at gmail.com
Fri May 23 01:39:31 EDT 2008


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?


Certainly not number theorists (they need a LOT more). 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?



More information about the Python-list mailing list