Dear colleagues Please could someone kindly explain the following inconsistencies I've discovered when performing logical calculations in R: 8.8 - 7.8 > 1> TRUE8.3 - 7.3 > 1> TRUEThank you, Emma Jane [[alternative HTML version deleted]]
On 12/5/2008 7:23 AM, emma jane wrote:> Dear colleagues > > Please could someone kindly explain the following inconsistencies I've discovered? when performing logical calculations in R: > > 8.8 - 7.8 > 1 >> TRUE > > 8.3 - 7.3 > 1 >> TRUESee R FAQ 7.31 (http://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-doesn_0027t-R-think-these-numbers-are-equal_003f).> Thank you, > > Emma Jane > > [[alternative HTML version deleted]] > > ------------------------------------------------------------------------ > > ______________________________________________ > R-help at r-project.org 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.-- Chuck Cleland, Ph.D. NDRI, Inc. (www.ndri.org) 71 West 23rd Street, 8th floor New York, NY 10010 tel: (212) 845-4495 (Tu, Th) tel: (732) 512-0171 (M, W, F) fax: (917) 438-0894
Dear Emma, On Fri, 5 Dec 2008 04:23:53 -0800 (PST) emma jane <emma_me_jane at yahoo.com> wrote:> Please could someone kindly explain the following inconsistencies > I've discovered__when performing logical calculations in R: > > 8.8 - 7.8 > 1 > > TRUE > > 8.3 - 7.3 > 1 > > TRUEGladly: FAQ 7.31 http://cran.at.r-project.org/doc/FAQ/R-FAQ.html#Why-doesn_0027t-R-think-these-numbers-are-equal_003f HTH. Cheers, Berwin =========================== Full address ============================Berwin A Turlach Tel.: +65 6516 4416 (secr) Dept of Statistics and Applied Probability +65 6516 6650 (self) Faculty of Science FAX : +65 6872 3919 National University of Singapore 6 Science Drive 2, Blk S16, Level 7 e-mail: statba at nus.edu.sg Singapore 117546 http://www.stat.nus.edu.sg/~statba
This comment is orthogonal to most of the others. It seems that folk often want to test for equality of "real" numbers. One important one is for convergence tests. When writing my Compact Numerical Methods book I had to avoid lots of logical tests, but wanted to compare two REALs. I found that the following approach, possibly considered a trick, is to use an offset and compare xnew + offset to xold + offset This works on the examples given to motivate the current thread with an offset of 10, for example. Motivation: Small xold, xnew compare offset with itself. Large xold and xnew are compared bitwise. Essentially we change from using a tolerance to using 1/tolerance. Perfect? No. But usable? Yes. And I believe worth keeping in mind for those annoying occasions where one needs to do a comparison but wants to get round the issue of knowing the machine precision etc. JN