martin sikora
2007-Jan-26 16:05 UTC
[R] strange behaviour with equality after simple subtraction
hello, today while trying to extract data from a list for subsequent analysis, i stumbled upon this funny behavior on my system:> x<-c(0.1,0.9)> 1-x[2][1] 0.1> x[1][1] 0.1> x[1]==1-x[2][1] FALSE> x[1]>1-x[2][1] TRUE> x<-c(0.3,0.7)> x[1][1] 0.3> x[2][1] 0.7> 1-x[2][1] 0.3> x[1]==1-x[2][1] FALSE but:> x<-c(0.4,0.6)> x[1][1] 0.4> x[2][1] 0.6> 1-x[2][1] 0.4> x[1]==1-x[2][1] TRUE any ideas why this might be? in my data i have values that are calculated like above, e.g. y=x if x<=0.5, else y=1-x now because of that behaviour whenever i want to get values e.g. <0.1 from my data, i will also get all values of y=0.1 that came from the subtraction. i'm using R 2.4.0 on windows XP; this is my sessionInfo():> sessionInfo()R version 2.4.0 (2006-10-03) i386-pc-mingw32 locale: LC_COLLATE=Spanish_Spain.1252;LC_CTYPE=Spanish_Spain.1252;LC_MONETARY=Spanis h_Spain.1252;LC_NUMERIC=C;LC_TIME=Spanish_Spain.1252 attached base packages: [1] "methods" "stats" "graphics" "grDevices" "utils" "datasets" "base">thanks a lot martin [[alternative HTML version deleted]]
Mike Prager
2007-Jan-26 16:40 UTC
[R] strange behaviour with equality after simple subtraction
"martin sikora" <martin.sikora at upf.edu> wrote:> today while trying to extract data from a list for subsequent analysis, i > stumbled upon this funny behavior on my system: > > > x<-c(0.1,0.9) > > > 1-x[2] > > [1] 0.1 > > > x[1] > > [1] 0.1 > > > x[1]==1-x[2] > > [1] FALSE > > > x[1]>1-x[2] > > [1] TRUE >Not at all strange, an expected property of floating-point arithmetic and one of the most frequently asked questions here.> print(0.1, digits=17)[1] 0.1> print(1 - 0.9, digits=17)[1] 0.09999999999999998>A simple description of the issue is at http://docs.python.org/tut/node16.html In most cases, it suffices to test for approximate difference or relative difference. The former would look like this if (abs(x[1] - x[2]) < eps)) ... with "eps" set to something you think is an insignificant difference, say 1.0e-10. -- Mike Prager, NOAA, Beaufort, NC * Opinions expressed are personal and not represented otherwise. * Any use of tradenames does not constitute a NOAA endorsement.
Bert Gunter
2007-Jan-26 17:16 UTC
[R] strange behaviour with equality after simple subtraction
FAQ on R 7.31. ?all.equal ?identical Have you read these? Bert Gunter Genentech Nonclinical Statistics South San Francisco, CA 94404 -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Mike Prager Sent: Friday, January 26, 2007 8:41 AM To: r-help at stat.math.ethz.ch Subject: Re: [R] strange behaviour with equality after simple subtraction "martin sikora" <martin.sikora at upf.edu> wrote:> today while trying to extract data from a list for subsequent analysis, i > stumbled upon this funny behavior on my system: > > > x<-c(0.1,0.9) > > > 1-x[2] > > [1] 0.1 > > > x[1] > > [1] 0.1 > > > x[1]==1-x[2] > > [1] FALSE > > > x[1]>1-x[2] > > [1] TRUE >Not at all strange, an expected property of floating-point arithmetic and one of the most frequently asked questions here.> print(0.1, digits=17)[1] 0.1> print(1 - 0.9, digits=17)[1] 0.09999999999999998>A simple description of the issue is at http://docs.python.org/tut/node16.html In most cases, it suffices to test for approximate difference or relative difference. The former would look like this if (abs(x[1] - x[2]) < eps)) ... with "eps" set to something you think is an insignificant difference, say 1.0e-10. -- Mike Prager, NOAA, Beaufort, NC * Opinions expressed are personal and not represented otherwise. * Any use of tradenames does not constitute a NOAA endorsement. ______________________________________________ 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.html and provide commented, minimal, self-contained, reproducible code.
Seemingly Similar Threads
- pamer.fnc y la nueva versiĆ³n de R
- Problem with package installation
- [LLVMdev] shared bitcode modules / dynamic linking.
- Unexpected behabiour of min, tapply and POSIXct/POSIXlt classes?
- Caret train with glmnet give me Error "arguments imply differing number of rows"