Why is math.pi slightly wrong?

inhahe inhahe at gmail.com
Thu May 22 13:26:04 EDT 2008


"Dutton, Sam" <Sam.Dutton at itn.co.uk> wrote in message 
news:mailman.1476.1211475183.12834.python-list at python.org...
> 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 searched for this on the web and in this group and couldn't find 
> anything -- but humble apologies if this has been asked before/elsewhere.
>
> Sam Dutton
>

it's gotta be a precision issue.  floating point numbers are stored on 
computers in binary.  you can't necessarily change the last digit from 1 to 
2 just by changing the last bits.  try converting a value after the decimal 
point to the same value after a decimal point in binary.  there's no telling 
how many bits it'll take.  and sometimes even, what isn't a repeating 
decimal in one base is in another base.

i see these precision issues raised all the time.
"why is my value resulting in 3.61000000001?"
the answer is because you can't store 3.61 in binary.
(i don't know specifically about that number, just an example)

(i know you can, if you use some sort of binary representation of decimal 
digits... but that's not how it's usually done.  it's stored pretty much 
like in scientific notation but in base 2.)








More information about the Python-list mailing list