[R] Problem with <= (less than or equal): not giving the expected result
Rainer Johannes
Johannes.Rainer at eurac.edu
Tue Apr 5 13:34:27 CEST 2016
Dear All,
I have the following problem:
I have a function in which I check if the difference between values is smaller or equal to a certain threshold. I however realized that I might get there some unexpected results:
> abs(1 - 0.95) >= 0.05
[1] TRUE
## So that’s fine, but:
> abs(1 - 0.95) <= 0.05
[1] FALSE
Apparently, abs(1 - 0.95) is not equal to 0.05, which I find however quite disturbing.
Along these lines:
> abs(0.95 - 1) > 0.05
[1] TRUE
> abs(0.95 - 1) < 0.05
[1] FALSE
I guess that has to do with the floating point representation of the data?
Is there something I miss or is there any solution to this?
Thanks for any help!
cheers, jo
I tried this on different R-version (including 3.2.3 and 3.3.0 alpha); The R-version I used for the code above is:
> sessionInfo()
R version 3.0.2 (2013-09-25)
Platform: x86_64-unknown-linux-gnu (64-bit)
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
[7] LC_PAPER=en_US.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
More information about the R-help
mailing list