[Rd] seq() does not create proper numbers (PR#10489)
murdoch at stats.uwo.ca
Tue Dec 4 16:20:48 CET 2007
On 12/4/2007 7:10 AM, rihle at gwdg.de wrote:
> Full_Name: Rico Ihle
> Version: 2.6.1
> OS: Windows XP Professional Version 2002 Pentium(R) 4 CPU 3.2 GHz
> Submission from: (NULL) (184.108.40.206)
This is not a bug. See FAQ 7.31, "Why doesn't R think these numbers are
> # Bug in seq() function:
> x <- seq(-2,2,by=0.01)
You've specified that the step size should be some number that's close
to 0.01, but not exactly 0.01, since R doesn't know how to represent that.
> which(x==0.05)# How that??
> # although:
> x# 0.05 seems to be at position 206 of x!!:
> # Why is this not equal to 0.05?
> # Reason:
> x2 <- as.character(x);x2
> x2# Ooooh... It's really not equal to 0.05!! How that? (compare lines 5 and
> # Remedy:
> x3 <- round(as.numeric(x),2)
> # The (necessary) rounding is apparently and unfortunately NOT included in the
> seq() function!!!
> # But it should be!!!
Why should it? You didn't specify a round number as the step size.
> # Because if one doesn't know about the demonstrated "nice" feature of the seq()
> # (and it is not visible in lines 5 or 6!!!)
> # one gets mad that x is not equal to 0.05 although x is printed as
> # Similarly:
> y <- seq(-0.5,.5,by=0.01)
> which(y == 0.05)# None? How that? Result should be 56!!
> # but:
> y2 <-as.character(y)
> which(y2 == 0.05)
> # or rounding alternatively:
> which(round(y,2) == 0.05)
> R-devel at r-project.org mailing list
More information about the R-devel