Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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` equals `0.1` and expect `0.1==0.1` to be true, it isn’t! Just see the following:

 12345678 > a=0.1 > b=0.3/3 > a  0.1 > b  0.1 > a==b  FALSE

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!

 12 > seq(0, 1, by=0.1) == 0.3   FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE

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:

 12 > unique(c(0.3, 0.4 – 0.1, 0.5 – 0.2, 0.6 – 0.3, 0.7 – 0.4))  0.3 0.3 0.3

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:

 123456 > sum=0 > for (i in 1:100) sum = sum + 0.01 > sum  1 > print(sum, digits=16)  1.000000000000001

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:

 1234 > unique(c(0.3, 0.4 – 0.1, 0.5 – 0.2, 0.6 – 0.3, 0.7 – 0.4))  0.3 0.3 0.3 > all.equal(0.3, 0.4 – 0.1, 0.5 – 0.2, 0.6 – 0.3, 0.7 – 0.4)  TRUE

Or, if you’re dealing with integers, you should use `round` or `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].

# References

[Mon08]
David Monniaux.
The pitfalls of verifying floating-point computations.
ACM Trans. Program. Lang. Syst., 30(3):1–41, 2008.
http://hal.archives-ouvertes.fr/hal-00128124/en/