Inaccurate Logarithm in Python

Posted by Avihu Turzion on Stack Overflow See other posts from Stack Overflow or by Avihu Turzion
Published on 2009-05-31T12:48:26Z Indexed on 2010/06/15 4:42 UTC
Read the original article Hit count: 319

Filed under:
|
|

I work daily with Python 2.4 at my company. I used the versatile logarithm function 'log' from the standard math library, and when I entered log(2**31, 2) it returned 31.000000000000004, which struck me as a bit odd.

I did the same thing with other powers of 2, and it worked perfectly. I ran 'log10(2**31) / log10(2)' and I got a round 31.0

I tried running the same original function in Python 3.0.1, assuming that it was fixed in a more advanced version.

Why does this happen? Is it possible that there are some inaccuracies in mathematical functions in Python?

© Stack Overflow or respective owner

Related posts about python

Related posts about math