[R] question about precision, floor, and powers of two.

Prof Brian Ripley ripley at stats.ox.ac.uk
Fri Nov 4 17:15:04 CET 2005


On Fri, 4 Nov 2005, Peter Dalgaard wrote:

> Uwe Ligges <ligges at statistik.uni-dortmund.de> writes:
>
>> Dr Carbon wrote:
>>
>>> At the risk of being beaten about the face and body, can somebody explain
>>> why the middle example: log2(2^3); floor(log2(2^3)) is different than
>>> examples 1 and 3?
>>
>>
>> Because
>>
>> > log2(2^3) - 3
>> [1] -4.440892e-16
>>
>> see the R FAQ "Why doesn't R think these numbers are equal?".
>>
>> Uwe Ligges
>
> In this particular case, it is slightly odd that we can't get an exact
> answer for operations that could in principle be carried out using
> integer arithmetic, but we're actually calculating log(8)/log(2).
>
> (Curiously, the same effect is not seen on Linux or Solaris until
>
> > log2(2^29)-29
> [1] 3.552714e-15
>
> )

Yes, since there are C99/POSIX functions log2 and log10.  We ought to make 
use of them if available (they are currently on Windows).

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