Skip to content Skip to sidebar Skip to footer

Float Deviation In Python List

Possible Duplicate: Python float - str - float weirdness I run the following code in python on codepad.org: num = 1.6 print num list = [num] print list num2 = list[0] print num

Solution 1:

list.__str__ calls repr on its elements, where as print calls str:

>>> str(1.6)
'1.6'>>> repr(1.6)
'1.6000000000000001'

Since floating point numbers are not guaranteed to be exact (and cannot be exact for values that can't be expressed as a * 2 for integers a,b), both representations are correct, or, in other words:

>>> 1.6 == 1.6000000000000001True

Post a Comment for "Float Deviation In Python List"