[R] Bug in "seq" (or a "feature") ?

Duncan Murdoch murdoch at stats.uwo.ca
Mon Aug 10 23:26:59 CEST 2009


Tal Galili wrote:
> (I use R 2.9.1 with win XP)
>
> If I run this code:
> seq(-0.1,.9, by = .05)[seq(-0.1,.9, by = .05) <= 0.5]
> I get this output:
> [1] -0.10 -0.05 0.00 0.05 0.10 0.15 0.20 0.25 0.30 0.35 0.40 0.45
> Why is 0.50 not in the results ?
> (It seems that it gives a slightly bigger number then 0.5 but I don't
> understand why it does that)
>
>
> Where as if I try:
> seq(-0.1,.9, by = .05)[seq(-0.1,.9, by = .05) <= 0.4]
> and get:
> [1] -0.10 -0.05 0.00 0.05 0.10 0.15 0.20 0.25 0.30 0.35 0.40
> Then 0.40 WILL be in the results.
>   

Neither a bug nor a feature, just the way things are.  None of -0.1, 0.9 
or 0.05 are numbers that can be represented exactly in R, so it's not 
too surprising that when you add a dozen copies of the last one to the 
first one, you don't get exactly 0.5 (which, being a power of 2, is 
exactly representable.  See FAQ 7.31.

Duncan Murdoch




More information about the R-help mailing list