[R] Creating logical value from the difference of two absolut e values

Liaw, Andy andy_liaw at merck.com
Fri Nov 19 02:02:01 CET 2004


It's imprecision of floating point representation of real numbers:

> print(abs(.7 - .5) - abs(.3 - .5), digit=20)
[1] -5.551115123125783e-17

one of the first things to learn about programming: be extremely careful
testing for equality of floating point numbers.

Andy


> From: Nathan Leon Pace, MD, MStat
> 
> Hi,
> 
> Using R 2.0.1 on Mac g5 running Mac OS X 10.3.6.
> 
> I would expect that
> 
> abs(.7 - .5) >= abs(.3 - .5) should be returned TRUE.
> 
> Instead
> 
>  > www <- abs(.7 - .5) >= abs(.3 - .5)
>  > www
> [1] FALSE
> 
> Is this a result of floating point or the implementation of abs or 
> something else?
> 
> In a function I need to compare two absolute values - each 
> being of the 
> form |variable - constant|.
> 
> Any suggestions for implementing this correctly?
> 
> Nathan
> 
> ______________________________________________
> 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
> 
>




More information about the R-help mailing list