Re: [R] strange behaviour with equality after simple subtraction

From: Benilton Carvalho <bcarvalh_at_jhsph.edu>
Date: Fri 26 Jan 2007 - 17:15:27 GMT

In addition to Mike's comment:

 > x<-c(0.1,0.9)
 > 1-x[2]
[1] 0.1
 > x[1]==1-x[2]
[1] FALSE
 > all.equal(x[1], 1-x[2])
[1] TRUE b

On Jan 26, 2007, at 11:40 AM, Mike Prager wrote:

> 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@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.



R-help@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. Received on Sat Jan 27 04:28:32 2007

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Fri 26 Jan 2007 - 18:30:30 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.