[R] That dreaded floating point trap

Alexander Engelhardt alex at chaotic-neutral.de
Thu Mar 31 14:14:46 CEST 2011

> A very straightforward way to avoid this problem is to construct the
> sequence by multiplying a sequence of integers by an approriate
> constant. E.g. for your first example:
>    for( i in where ){
>       thisdata<- subset(herde, herde$mlr>= i)
>       # do stuff with thisdata..
>    }
> 'where' is a vector like 0.1*((-10):10)
> [ instead of seq(-1, 1, by=0.1) ]
> and then, when you want to change to seq(-0.8, 1, by=0.1),
> use instead 0.1*(-80,10).


this helps, thank you.
But if this code is in a function, and some user supplies a vector, I 
will still have to round it in the function, I guess.

It's weird how 0.1 is different from round(0.1, digits=1) , but I don't 
want to read that 90 page long floating point paper which was referenced 
somewhere :)


More information about the R-help mailing list