wtfpython icon indicating copy to clipboard operation
wtfpython copied to clipboard

floating point operations and test

Open lcrmorin opened this issue 2 years ago • 1 comments

I met a nasty bug that will now wake me up at night... It concerns floating point operations and I feel like it is not really included in your list. Note that one could argue this problem is not python specific but it still caused some nasty bug and people might want to be aware of this. (This blog reminded me of the bug and helped me formulate it more clearly here). It boils down to:

1 + 2 == 3 True

(1 + 2)/10 == 3/10 True

1/10 + 2/10 == 3/10 False

.1 + .2 == .3 False

And of course :

0.3 - 0.2 - 0.1 == 0 False

lcrmorin avatar Feb 10 '23 12:02 lcrmorin

That's not a bug, that's how floating point works

koldakov avatar Apr 12 '24 18:04 koldakov