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

Michael Knudsen micknudsen at gmail.com
Wed Sep 30 21:21:28 CEST 2009


On Wed, Sep 30, 2009 at 8:40 PM, Michael Knudsen <micknudsen at gmail.com> wrote:

>> 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

A friend of mine just pointed out a possible solution:

> a=seq(0.1,0.9,by=0.1)
> a = seq(0.1,0.9,by=0.1)
> a[3]==0.3
[1] FALSE
> all.equal(a[3],0.3)
[1] TRUE

The all.equal function checks if two objects are "nearly" equal.

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




More information about the R-help mailing list