**binfalse » R**, and kindly contributed to R-bloggers)

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:

1
2 3 4 5 6 7 8 |
> a=0.1
> b=0.3/3 > a [1] 0.1 > b [1] 0.1 > a==b [1] 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!**

1
2 |
> seq(0, 1, by=0.1) == 0.3
[1] 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**:

1
2 |
> unique(c(0.3, 0.4 – 0.1, 0.5 – 0.2, 0.6 – 0.3, 0.7 – 0.4))
[1] 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:

1
2 3 4 5 6 |
> sum=0
> for (i in 1:100) sum = sum + 0.01 > sum [1] 1 > print(sum, digits=16) [1] 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:

1
2 3 4 |
> unique(c(0.3, 0.4 – 0.1, 0.5 – 0.2, 0.6 – 0.3, 0.7 – 0.4))
[1] 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) [1] 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/

**leave a comment**for the author, please follow the link and comment on his blog:

**binfalse » R**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...