I’m currently doing some stuffs with Python and I get some strange behavior when assigning variables. For example, I set “a” to 0.1 in the ipython console :
In [1]: a = 0.1
Then I print a :
In [2]: a
Out[2]: 0.10000000000000001
In [3]: a == 0.1
Out[3]: True
In [4]: a == 0.10000000000000001
Out[4]: True
Okay, maybe it’s because of the finite representation of numbers (the last 1 is at the 16th place). But then :
In [17]: 1 == 0.10000000000000001
Out[17]: False
Do I have to be scared by this ? Because I am ! 😉
Check the Floating Point Arithmetic: Issues and Limitations part of the Python tutorials – what you describe is inherent weirdness with the float data type (nothing Python specific)
Also, be aware that
repr()(string representation) shows the float with pretty-looking rounding – your firstaexample isn’t exactly0.1, which can be seen if you print it with more precision: