[R] Rounding error in seq(...)

Michael Knudsen micknudsen at gmail.com
Wed Sep 30 20:40:02 CEST 2009


Hi,

Today I was flabbergasted to see something that looks like a rounding
error in the very basic seq function in R.

> a = seq(0.1,0.9,by=0.1)
> a
[1] 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9
> a[1] == 0.1
[1] TRUE
> a[2] == 0.2
[1] TRUE
> a[3] == 0.3
[1] FALSE

It turns out that the alternative

> a = (1:9)/10

works just fine. Are there any good guides out there on how to deal
with issues like this? I am normally aware of rounding errors, but it
really surprised me to see that an elementary function like seq would
behave in this way.

Thanks,
Michael Knudsen

-- 
Michael Knudsen
micknudsen at gmail.com
http://sites.google.com/site/micknudsen/




More information about the R-help mailing list