From murdoch at stats.uwo.ca  Tue Dec  4 16:20:48 2007
From: murdoch at stats.uwo.ca (Duncan Murdoch)
Date: Tue, 04 Dec 2007 10:20:48 -0500
Subject: [Rd] seq() does not create proper numbers (PR#10489)
In-Reply-To: <20071204121012.9F5632834155@mail.pubhealth.ku.dk>
References: <20071204121012.9F5632834155@mail.pubhealth.ku.dk>
Message-ID: <47557050.6000603@stats.uwo.ca>

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) (134.76.183.24)

This is not a bug.  See FAQ 7.31, "Why doesn't R think these numbers are 
equal?"

> 
> 
> # 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!!:
> x[206]
> # Why is this not equal to 0.05?
> 
> # Reason:
> x2 <- as.character(x);x2
> x2[206]# Ooooh... It's really not equal to 0.05!! How that? (compare lines 5 and
> 6!)
> 
> # Remedy:
> x3 <- round(as.numeric(x),2)
> which(x3==0.05)
> 
> # 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.

Duncan Murdoch

> # Because if one doesn't know about the demonstrated "nice" feature of the seq()
> function
> # (and it is not visible in lines 5 or 6!!!)
> # one gets mad that x[206] is not equal to 0.05 although x[206] is printed as
> 0.05!!!
> 
> # Similarly:
> y <-  seq(-0.5,.5,by=0.01)
> which(y == 0.05)# None? How that? Result should be 56!!
> y[56]
> 
> # but:
> y2 <-as.character(y)
> y2[56]
> which(y2 == 0.05)
> 
> # or rounding alternatively:
> which(round(y,2) == 0.05)
> 
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel



[Rd] seq() does not create proper numbers (PR#10489)
Duncan Murdoch