Floating numbers

Thomas Jollans thomas at jollybox.de
Thu Aug 12 17:24:48 EDT 2010


On Thursday 12 August 2010, it occurred to Bradley Hintze to exclaim:
> Hi all.
> 
> Is there a way I can keep my floating point number as I typed it? For
> example, I want 34.52 to be 34.52 and NOT 34.5200000002.

The conversion from decimal to binary and vice versa is inexact -- but they're 
the same number internally:
 
Python 2.6.6rc1+ (r266rc1:83691, Aug  5 2010, 17:07:04) 
[GCC 4.4.5 20100728 (prerelease)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 34.52
34.520000000000003
>>> 34.520000000000003 == 34.52
True
>>> 


Python 3 is a bit smarter when printing, so you don't really notice what's 
going on:

Python 3.1.2 (release31-maint, Jul  8 2010, 09:18:08) 
[GCC 4.4.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 34.52
34.52
>>> 34.520000000000003
34.52
>>> 




More information about the Python-list mailing list