Why is math.pi slightly wrong?

Jerry Hill malaclypse2 at gmail.com
Thu May 22 13:12:30 EDT 2008


On Thu, May 22, 2008 at 12:32 PM, Dutton, Sam <Sam.Dutton 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?

I believe this is an issue with the precision of IEEE floating point
numbers[1].  Those are the types of numbers used by the hardware
floating point processor on your CPU.  I'm not an expert, by any
stretch of the imagination, but I believe you have two choices to
represent pi as an IEEE double precision float:

3.1415926535897931 (stored as 0x400921FB54442D18) or
3.1415926535897936 (stored as 0x400921FB54442D19)

1: http://en.wikipedia.org/wiki/IEEE_floating-point_standard

-- 
Jerry



More information about the Python-list mailing list