I recently got very close to the floating point trap, again, so here is a little tribute with some small examples!
Because Gnu R is very nice in suppressing these errors, all examples are presented in R.
Those of you that are ignorant like me, might think that
0.1 and expect
0.1==0.1 to be true, it isn’t! Just see the following:
You might think it comes from the division, so you might expect
seq(0, 1, by=0.1) == 0.3 contains exactly one vale that is
TRUE!? Harrharr, nothing like that!
Furthermore, what do you think is the size of
unique(c(0.3, 0.4 - 0.1, 0.5 - 0.2, 0.6 - 0.3, 0.7 - 0.4))!? Is it one? Not even close to it:
Your machine is that stupid, that it isn’t able to save such simple numbers ;)
And another example should show you how these errors sum up:
As you can see, R tells you that you summed up to exactly one, suppressing the small numerical error. This error will increase with larger calculations! So be careful with any comparisons.
To not fail the next time, for example use the R build-in function
all.equal for comparison:
Or, if you’re dealing with integers, you should use
as.integer to make sure they really are integers.
I hope I could prevent some of you falling into this floating point trap! So stop arguing about numerical errors and start caring for logical fails ;-)
Those of you interested in further wondering are referred to [Mon08].
- David Monniaux.
The pitfalls of verifying floating-point computations.
ACM Trans. Program. Lang. Syst., 30(3):1–41, 2008.