[R] logical inconsistency

Peter Dalgaard p.dalgaard at biostat.ku.dk
Sun Dec 7 01:28:04 CET 2008

nashjc at uottawa.ca wrote:
> 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.

Hmm. Echos of some early battles with R's qbeta() in this. I don't think 
it can be recommended.

The problem is that you can end up in a situation where xnew=xold-1ulp 
and xnewnew is xnew+1ulp. I.e. in two iterations you're back at xold.

Even in cases where this provably cannot happen, modern optimizers may 
make it happen anyway...

    O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
   c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
  (*) \(*) -- University of Copenhagen   Denmark      Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)              FAX: (+45) 35327907

More information about the R-help mailing list