Why is math.pi slightly wrong?

Nick Craig-Wood nick at craig-wood.com
Fri May 23 06:30:09 EDT 2008


Jerry Hill <malaclypse2 at gmail.com> wrote:
>  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)

Just in case you were wondering how you get those hex numbers (like I
was!)

  >>> import math
  >>> import struct
  >>> struct.pack(">d", math.pi).encode("hex")
  '400921fb54442d18'
  >>> struct.unpack(">d", "400921FB54442D18".decode("hex"))
  (3.1415926535897931,)
  >>> struct.unpack(">d", "400921FB54442D19".decode("hex"))
  (3.1415926535897936,)
  >>> 

-- 
Nick Craig-Wood <nick at craig-wood.com> -- http://www.craig-wood.com/nick



More information about the Python-list mailing list