If I type the following into either the Python 2.7.1 shell or the 3.3.2 shell:
a = 0.1
b = 0.1
(a + b) == 0.2
it returns the value true
. From stackoverflow and this video from MIT, I was under the impression that this would return false
, since there are small errors when trying to represent 0.1 exactly in a computer. (Isn't it binary after all?) So I guess Python must be either a) doing non-floating point arithmetic or b) rounding before doing a test for equality.
Which is it and which versions of Python have this behavior?
None of them. It's "luck". The floating point representation+arithmetic gives the same value for the numbers you picked.
You can use the
decimal
module to show you this: