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

Gabor Grothendieck ggrothendieck at gmail.com
Wed Sep 30 21:41:29 CEST 2009


See:

http://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-doesn_0027t-R-think-these-numbers-are-equal_003f

On Wed, Sep 30, 2009 at 2:40 PM, Michael Knudsen <micknudsen at gmail.com> wrote:
> 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/
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>




More information about the R-help mailing list