[R] surprising difference in log()

Prof Brian Ripley ripley at stats.ox.ac.uk
Tue Jun 26 19:01:53 CEST 2007


On Tue, 26 Jun 2007, Fausto Galli wrote:

>
> Hello everybody
>
> My collegue and I noticed a strange behaviour of R on different
> platforms. It's a simple computation, but results are rather different.
>
> On Windows XP:
>
> > floor(log(8,2))
> [1] 3
>
> which is what one should expect.
> Here's instead the result with Mac OS X (same version, 2.5.0
> (2007-04-23))
>
> > floor(log(8,2))
> [1] 2
>
> Is it a "bug" in R or in the operating system?
> Anyway, it's quite a surprising one.

It is a minor problem in the OS.  Given that log(8, 2) is a floating point 
calculation, it is unreasonable to expect it in general to be exact.  For 
those OSes that have log2, log2(8) is used and one might reasonably expect 
that to be exactly 3.  So either MacOS X is lacking log2 or its log2 is 
not exact.

Taking 'floor' of a floating-point computation without a fuzz is unwise.

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