[R] Unexpected result from binary greater than operator

Peter Dalgaard p.dalgaard at biostat.ku.dk
Tue Nov 1 17:28:35 CET 2005


chris.bodily at autolivasp.com writes:

> Hi All,
> 
> I recently encountered results that I did not expect, exhibited by the
> following code snippet:
> 
> test <- function() {
>     minX <- 4.2
>     min0 <- 4.1
>     sigmaG <- 0.1
>     Diff <- minX-min0
>     print(c(Diff=Diff,sigmaG=sigmaG))
>     cat("is Diff > sigmaG?:", Diff > sigmaG,"\n")
>     cat("is (4.2 - 4.1) > 0.1?:",(4.2 - 4.1) > 0.1,"\n")
>     cat("is 0.1 > 0.1?:", 0.1>0.1,"\n")
> }
> 
> When I execute the above function I get the following:
> > test()
>   Diff sigmaG
>    0.1    0.1
> is Diff > sigmaG?: TRUE
> is (4.2 - 4.1) > 0.1?: TRUE
> is 0.1 > 0.1?: FALSE
> 
> Can someone please help me understand why R returns TRUE for (4.2 - 4.1) >
> 0.1 ?

Section 7.31 in the FAQ should help:

http://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-doesn_0027t-R-think-these-numbers-are-equal_003f
 


-- 
   O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark          Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)                  FAX: (+45) 35327907




More information about the R-help mailing list