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

jim holtman jholtman at gmail.com
Sun Jul 19 00:28:49 CEST 2009


First of all, read FAQ 7.31 to understand that 1/3 is not
representable in floating point.  Also a^b is actually exp(log(a) * b)
and log(-8) is not valid (NaN).

You expression is not really taking the cube root; it is taking values
to the 1/3 power.  If you want to cube root function, then try:

> cubeRoot <- function(x) sign(x) * exp(log(abs(x)) / 3)
> cubeRoot(8)
[1] 2
> cubeRoot(-8)
[1] -2
>
>


On Sat, Jul 18, 2009 at 6:04 PM, Dave DeBarr<Dave.DeBarr at microsoft.com> 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
>
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?




More information about the R-help mailing list