Re: [R] R: to the power

From: Thomas Lumley <tlumley_at_u.washington.edu>
Date: Sun 17 Jul 2005 - 01:17:33 EST

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

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



R-help@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 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