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

Duncan Murdoch murdoch at stats.uwo.ca
Fri Nov 4 17:21:13 CET 2005


On 11/4/2005 10:58 AM, 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

In Windows 2.2.0 and R-devel, I get

 > log2(2^3) - 3
[1] 0
 > log2(2^28) - 28
[1] 0
 > log2(2^29) - 29
[1] 3.552714e-15

I suspect the difference is that the machines with the inaccurate answer 
have had some DLL or video driver set their precision to 53 bits instead 
of the default 64.

Maybe we need to be even more defensive against such changes.

Duncan Murdoch
> 
> )
>  
>> > 
>> >>log2(2^2); floor(log2(2^2))
>> > 
>> > [1] 2
>> > [1] 2
>> > 
>> >>log2(2^3); floor(log2(2^3))
>> > 
>> > [1] 3
>> > [1] 2
>> > 
>> >>log2(2^4); floor(log2(2^4))
>> > 
>> > [1] 4
>> > [1] 4
>> > 
>> > 
>> > DrC
>> > 
>> > 	[[alternative HTML version deleted]]
>> > 
>> > ______________________________________________
>> > R-help at stat.math.ethz.ch mailing list
>> > https://stat.ethz.ch/mailman/listinfo/r-help
>> > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>> 
>> ______________________________________________
>> R-help at stat.math.ethz.ch mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>> 
>




More information about the R-help mailing list