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

Thomas Lumley tlumley at u.washington.edu
Fri Nov 4 18:02:19 CET 2005


On Fri, 4 Nov 2005, Uwe Ligges wrote:

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


This is a less satisfactory answer than usual, because both 2^3 and 
log(2^3) are integers and thus exactly representable in the R numeric 
type.  You could reasonably expect log(8) to be exactly 2, just as sqrt(4) 
is exactly 2.

The problem is that we compute all logarithms via the natural log, and 
this introduces the problem of limited precision.

 	-thomas




More information about the R-help mailing list