Re: [R] R: to the power

From: Thomas Lumley <>
Date: Sun 17 Jul 2005 - 01:17:33 EST

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

> At 10:11 12/7/2005, 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

to compute x^(a/b), where a and b could then be exactly representable integers. If someone wants to write one....

         -thomas mailing list PLEASE do read the posting guide! Received on Sun Jul 17 01:20:41 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:33:46 EST