Re: [Rd] BUG in " == " ? (PR#9065)

From: Dimitris Rizopoulos <dimitris.rizopoulos_at_med.kuleuven.be>
Date: Fri 07 Jul 2006 - 11:49:13 GMT

> On Fri, 2006-07-07 at 11:50 +0200, eric.durand@imag.fr wrote:
>> Hello,
>> here is the version of R that I use :
>>
>> > version
>> _
>> platform i486-pc-linux-gnu
>> arch i486
>> os linux-gnu
>> system i486, linux-gnu
>> status
>> major 2
>> minor 3.1
>> year 2006
>> month 06
>> day 01
>> svn rev 38247
>> language R
>> version.string Version 2.3.1 (2006-06-01)
>>
>> And here is one of the sequences of isntruction that returns an
>> abberation :
>>
>> > x<-seq(0,1,by=0.01)
>> > x[71]
>> [1] 0.7
>> > which(x == 0.7)
>> numeric(0)
>> > x[71] == 0.7
>> [1] FALSE
>>
>> Or another version of (maybe) the same bug :
>>
>> > x <- 70
>> > x == 70
>> [1] TRUE
>> > x <- x*0
>> > x <- 70
>> > x == 70
>> [1] TRUE
>> > x<-x*0.01
>> > x
>> [1] 0.7
>> > x == 0.7
>> [1] FALSE
>>
>> It seems completely strange ... any help would be greatly
>> appreciated :)
>>
>> Regards,
>> Eric Durand
>
> Not a bug. Floating point precision - see
> http://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-doesn_0027t-R-think-these-numbers-are-equal_003f
>
> E.g.:
>
> x <- seq(0,1,by=0.01)
> x[71]
> which(x == 0.7) # FALSE
> all.equal(0.7, x[71]) # TRUE
>
> all.equal() should be used for this type of comparison. See
> ?all.equal
>
> You could achieve what you wanted by something like this [ if you
> have a
> real need to use which() ]:
>
> x <- c(0.7, x, 0.7) # more 0.7's
> lims <- range(0.7 + .Machine$double.eps^0.5, 0.7 -
> .Machine$double.eps^0.5)
> which(x >= lims[1] & x <= lims[2])
>
> Not sure if this is a good way to do it or not?
>
> If you need to do this kind of comparison with which(), having a
> v.all.equal() do a vectorised version of all.equal() would be
> useful,
> returning a vector of TRUE/FALSE for each comparison. Maybe this
> already
> exists somewhere?
>
> E.g.:
>
> v.all.equal <- function(x, y) {
> apply(as.matrix(x), 1, function(x, y) {
> retval <- as.logical(all.equal(x, y))
> retval[is.na(retval)] <- FALSE
> retval
> }, y)
> }
>
> which(v.all.equal(x, 0.7)) # 1 72 103

in this case I think it's simpler to do something like:

which(as.logical(sapply(x, all.equal, target = 0.7)))

Best,
Dimitris



Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Address: Kapucijnenvoer 35, Leuven, Belgium

Tel: +32/(0)16/336899
Fax: +32/(0)16/337015
Web: http://med.kuleuven.be/biostat/
     http://www.student.kuleuven.be/~m0390867/dimitris.htm



> Again, maybe this is a bad thing...? In which case, I guess I'm
> about to
> get told so... ;-)
>
> G
> --
> %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
> Gavin Simpson [t] +44 (0)20 7679 0522
> ECRC & ENSIS, UCL Geography, [f] +44 (0)20 7679 0565
> Pearson Building, [e] gavin.simpsonATNOSPAMucl.ac.uk
> Gower Street, London [w] http://www.ucl.ac.uk/~ucfagls/cv/
> London, UK. WC1E 6BT. [w] http://www.ucl.ac.uk/~ucfagls/
> %~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
>
> ______________________________________________
> R-devel@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm



R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Fri Jul 07 21:47:59 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Fri 07 Jul 2006 - 14:24:45 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.