chris.bodily@autolivasp.com
2005-Nov-01 16:15 UTC
[R] Unexpected result from binary greater than operator
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 ? Thanks so much, Chris I'm running the precompiled R-2.2.0 binary for Windows on WinXP Pro SP1.>Sys.getlocale()"LC_COLLATE=English_United States.1252;LC_CTYPE=English_United States.1252;LC_MONETARY=English_United States.1252;LC_NUMERIC=C;LC_TIME=English_United States.1252">version_ platform i386-pc-mingw32 arch i386 os mingw32 system i386, mingw32 status major 2 minor 2.0 year 2005 month 10 day 06 svn rev 35749 language R
Duncan Murdoch
2005-Nov-01 16:22 UTC
[R] Unexpected result from binary greater than operator
On 11/1/2005 11:15 AM, chris.bodily at autolivasp.com wrote:> Can someone please help me understand why R returns TRUE for (4.2 - 4.1) > > 0.1 ?Rounding error. See the FAQ item 7.31 Why doesn't R think these numbers are equal? The only numbers that can be represented exactly in R's numeric type are integers and fractions whose denominator is a power of 2. Other numbers have to be rounded to (typically) 53 binary digits accuracy. As a result, two floating point numbers will not reliably be equal unless they have been computed by the same algorithm, and not always even then. For example R> a <- sqrt(2) R> a * a == 2 [1] FALSE R> a * a - 2 [1] 4.440892e-16 The function all.equal() compares two objects using a numeric tolerance of .Machine$double.eps ^ 0.5. If you want much greater accuracy than this you will need to consider error propagation carefully. For more information, see e.g. David Goldberg (1991), ?What Every Computer Scientist Should Know About Floating-Point Arithmetic?, ACM Computing Surveys, 23/1, 5?48, also available via http://docs.sun.com/source/806-3568/ncg_goldberg.html Duncan Murdoch
Sundar Dorai-Raj
2005-Nov-01 16:26 UTC
[R] Unexpected result from binary greater than operator
chris.bodily at autolivasp.com wrote:> 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 ? > > Thanks so much, > Chris > > > I'm running the precompiled R-2.2.0 binary for Windows on WinXP Pro SP1. > > >>Sys.getlocale() > > "LC_COLLATE=English_United States.1252;LC_CTYPE=English_United > States.1252;LC_MONETARY=English_United > States.1252;LC_NUMERIC=C;LC_TIME=English_United States.1252" > > >>version > > _ > platform i386-pc-mingw32 > arch i386 > os mingw32 > system i386, mingw32 > status > major 2 > minor 2.0 > year 2005 > month 10 > day 06 > svn rev 35749 > language R >Seems like this is coming up about once a week. See FAQ 7.31. http://cran.r-project.org/doc/FAQ/R-FAQ.html Hint: print(4.2 - 4.1, digits = 16) --sundar
Peter Dalgaard
2005-Nov-01 16:28 UTC
[R] Unexpected result from binary greater than operator
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
Hi. Easy. Regardless of OS and R version answer is binary representation of fractions.> (4.2-4.1)>.1[1] TRUE but> (4.3-4.2)>.1[1] FALSE> print(4.3-4.2, digits=20)[1] 0.099999999999999645 You can not expect precise results using not precise computers. Use e.g. round> round(4.3-4.2)>.1[1] FALSE HTH Petr On 1 Nov 2005 at 9:15, chris.bodily at autolivasp.com wrote: To: r-help at stat.math.ethz.ch From: chris.bodily at autolivasp.com Date sent: Tue, 1 Nov 2005 09:15:50 -0700 Subject: [R] Unexpected result from binary greater than operator> 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 ? > > Thanks so much, > Chris > > > I'm running the precompiled R-2.2.0 binary for Windows on WinXP Pro > SP1. > > >Sys.getlocale() > "LC_COLLATE=English_United States.1252;LC_CTYPE=English_United > States.1252;LC_MONETARY=English_United > States.1252;LC_NUMERIC=C;LC_TIME=English_United States.1252" > > >version > _ > platform i386-pc-mingw32 > arch i386 > os mingw32 > system i386, mingw32 > status > major 2 > minor 2.0 > year 2005 > month 10 > day 06 > svn rev 35749 > language R > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.htmlPetr Pikal petr.pikal at precheza.cz
Look at ?all.equal and ?identical as well as searching the archives for those terms. You'll find many an illuminating thread on precision, floating point arithmetic and other wonders. minX <- 4.2 min0 <- 4.1 sigmaG <- 0.1 Diff <- minX-min0 all.equal(Diff, sigmaG) identical(Diff, sigmaG) HTH, Andy