[R] R: to the power

Thomas Lumley tlumley at u.washington.edu
Sat Jul 16 17:17:33 CEST 2005


On Sat, 16 Jul 2005, Bernardo Rangel Tura wrote:

> At 10:11 12/7/2005, allan_sta_staff_sci_main_uct at mail.uct.ac.za wrote:
>
>> hi all
>>
>> why does R do this:
>>
>> (-8)^(1/3)=NaN
>>
>> the answer should be : -2
>

Yes and no.

The problem is that the reciprocal of 3 is not exactly representable as a 
floating point number (it has an infinite binary expansion 
.010101010101...)

So the R expression 1/3 actually returns a number slightly different from 
one-third. It is a fraction with denominator a power of two (probably 
2^53).  Now, -8 to power that is a fraction with denominator a power of 2 
is not a real number, so, NaN.

It would be nice if R could realize that you meant the cube root of -8, 
but that requires either magical powers or complicated and unreliable 
heuristics.  The real solution might be a function like
   root(x,a,b)
to compute  x^(a/b), where a and b could then be exactly representable 
integers. If someone wants to write one....

 	-thomas




More information about the R-help mailing list