[R] (-8)^(1/3) == NaN?

(Ted Harding) Ted.Harding at manchester.ac.uk
Sun Jul 19 00:54:13 CEST 2009


On 18-Jul-09 22:04:57, Dave DeBarr wrote:
> Why does the expression "(-8)^(1/3)" return NaN, instead of -2?
> 
> This is not answered by
> http://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-are-powers-of-negative-
> numbers-wrong_003f
> 
> Thanks,
> Dave

Because R does not try to evaluate (-8)^(1/3), but (-8)^x, where x is
a very close approximation to 1/3 but is not exactly 1/3 (which is
impossible in a finite binary representation).

But even if it could exactly represent 1/3, R would still need to
have a special "look-up" for certain fractional powers (1/3, 1/5, ... )
to enable it to recognise that these are odd-integer-roots of negatgive
numbers, and therefore can be evaulated as -(nth_root(abs(x))).

It doesn't help, either, to try to do it in complex numbers, since
(-8) will then be seen as 8*exp(i*pi) whose cube root will be found as

2*exp(i*pi/3) = 2*(cos(pi/3) + i*sin(pi/3)) = 2*(1/2 + i*sqrt(3)/2):

  (complex(1,-8,0))
  # [1] -8+0i

  complex(1,-8,0)^(1/3)
  # [1] 1+1.732051i

  (8*exp(complex(1,0,pi)))^(1/3)
  # [1] 1+1.732051i

  sqrt(3)
  # [1] 1.732051

I'm not sure what best to suggest for your situation. Basically, if it
is in a context where it can only be

  (negative number)^(1/(odd integer))

then you are better off modifying the logic of your program so as
to ensure the result you want.

Hoping this helps,
Ted.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 18-Jul-09                                       Time: 23:54:11
------------------------------ XFMail ------------------------------




More information about the R-help mailing list