[R] a==0 vs as.integer(a)==0 vs all.equal(a,0)

Prof Brian Ripley ripley at stats.ox.ac.uk
Tue Mar 8 11:42:29 CET 2005


On Tue, 8 Mar 2005, Duncan Murdoch wrote:

> On Tue, 8 Mar 2005 09:03:43 +0000, Robin Hankin
> <r.hankin at soc.soton.ac.uk> wrote :
>
>> hi
>>
>>
>> ?integer says:
>>
>>      Note that on almost all implementations of R the range of
>>      representable integers is restricted to about +/-2*10^9: 'double's
>>      can hold much larger integers exactly.
>>
>>
>> I am getting very confused as to when to use integers and when not to.
>> In my line
>> I need exact comparisons of large integer-valued arrays, so I often use
>> as.integer(),
>> but the above seems to tell me that doubles might  be better.
>>
>> Consider the following R idiom of Euclid's algorithm for the highest
>> common factor
>> of two positive integers:
>>
>>   gcd <- function(a, b){
>>     if (b == 0){ return(a)}
>>     return(Recall(b, a%%b))
>>   }
>>
>> If I call this with gcd(10,12), for example, then  a%%b is not an
>> integer, so the first
>> line of the function, testing b for being zero, isn't legitimate.
>
> When you say it isn't legitimate, you mean that it violates the advice
> never to use exact comparison on floating point values?
>
> I think that's just advice, it's not a hard and fast rule.  If you
> happen to know that the values being compared have been calculated and
> stored exactly, then "==" is valid.  In your function, when a and b
> are integers that are within some range (I'm not sure what it is, but
> it approaches +/- 2^53), the %% operator should return exact results.
> (Does it do so on all platforms?  I'm not sure, but I'd call it a bug
> if it didn't unless a and/or b were very close to the upper limit of
> exactly representable integers.)

It is supposed to do so up to (but not including)
.Machine$double.base ^ .Machine$double.digits,
normally 2^53, irrespective of sign.  (These are computed at run-time, 
so one can be pretty confident about them, at least if your FPU is 
bug-free.)

> Do you know of examples where a and b are integers stored in floating
> point, and a %% b returns a value that is different from as.integer(a)
> %% as.integer(b)?

Yes (see the NEWS for R-devel), but only for large integers where the 
second is NA.

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595




More information about the R-help mailing list