I spent an hour today trying to figure out why
return abs(val-desired) <= 0.1
was occasionally returning False
, despite val
and desired
having an absolute difference of <=0.1
. After some debugging, I found out that -13.2 + 13.3 = 0.10000000000000142
. Now I understand that CPUs cannot easily represent most real numbers, but this is an exception, because you can subtract 0.00000000000000142
and get 0.1
, so it can be represented in Python.
I am running Python 2.7 on Intel Core architecture CPUs (this is all I have been able to test it on). I'm curious to know how I can store a value of 0.1
despite not being able to apply arithmetic to particular floating point values. val
and desired
are float
values.