[R] logical inconsistency

nashjc at uottawa.ca nashjc at uottawa.ca
Sat Dec 6 23:50:39 CET 2008

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


    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.


More information about the R-help mailing list