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]]
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?
The correct mathematical "answer" is really one (or perhaps all three?) of three complex numbers that are the solutions to x^3+8=0. Here is one of the others: > as.complex(-8)^(1/3) [1] 1+1.732051i I suspect there is a reason why R is willing to produce this particular solution and not the other two after being told to use the complex range, but I don't know the reason. You can get all three with: > polyroot(c(8,0,0,1)) [1] 1+1.732051i -2+0.000000i 1-1.732051i -- David. On Jul 18, 2009, at 6:04 PM, 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, > DaveDavid Winsemius, MD Heritage Laboratories West Hartford, CT
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, > DaveBecause 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 ------------------------------
It' true, but if you type -8^(1/3) returns -2, and if you type -8^1/3 it returns -2.66666, maybe there are some rules about parenthesis... regards V?ctor ________________________________ De: r-help-bounces at r-project.org en nombre de Dave DeBarr Enviado el: s?b 18/07/2009 05:04 Para: r-help at r-project.org Asunto: [R] (-8)^(1/3) == NaN? 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.
On Sun, Jul 19, 2009 at 12:28 AM, jim holtman<jholtman at gmail.com> wrote:> 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). >If this is so, why would the following evaluate as expected?> (-8)^(3)[1] -512 Liviu
If the power that a number is being raised to is integer, then is does evaluate honoring the unary minus.> (-2) ^ 5 #integer power[1] -32> (-2) ^ 5.1[1] NaN>-8^(1/3) is parsed as -(8^(1/3)) according to operator precedence. On Sun, Jul 19, 2009 at 4:49 PM, Liviu Andronic<landronimirc at gmail.com> wrote:> On Sun, Jul 19, 2009 at 12:28 AM, jim holtman<jholtman at gmail.com> wrote: >> 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). >> > > If this is so, why would the following evaluate as expected? >> (-8)^(3) > [1] -512 > > Liviu >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?
On Sun, 19 Jul 2009, jim holtman wrote:> If the power that a number is being raised to is integer, then is does > evaluate honoring the unary minus. > >> (-2) ^ 5 #integer power > [1] -32 >> (-2) ^ 5.1 > [1] NaN >>Yes. 3 is representable exactly as a whole number, so (-2)^3 exists, but (1/3) is represented as a fraction whose denominator is 2^54, an even number, so (-8)^(1/3) does not exist (as a real number). More generally, since all floating point numbers are represented as fractions whose denominator is a power of 2, the only way a floating point number can be a legitimate exponent for a negative base is if it represents a whole number. -thomas> > -8^(1/3) > > is parsed as -(8^(1/3)) according to operator precedence. > > On Sun, Jul 19, 2009 at 4:49 PM, Liviu Andronic<landronimirc at gmail.com> wrote: >> On Sun, Jul 19, 2009 at 12:28 AM, jim holtman<jholtman at gmail.com> wrote: >>> 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). >>> >> >> If this is so, why would the following evaluate as expected? >>> (-8)^(3) >> [1] -512 >> >> Liviu >> > > > > -- > Jim Holtman > Cincinnati, OH > +1 513 646 9390 > > What is the problem that you are trying to solve? > > ______________________________________________ > 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. >Thomas Lumley Assoc. Professor, Biostatistics tlumley at u.washington.edu University of Washington, Seattle