How to calculate any negative number for the power of some fraction in R?

In the following code:

(-8/27)^(2/3) 

I got the result of NaN , despite the fact that the correct result should be 4/9 or .444444...

So why does it return NaN? And how can I return the correct value?

+7
r
source share
2 answers

As stated in help("^") :

Users are sometimes surprised at the return value, for example, why '(-8) ^ (1/3) is NaN. For dual inputs, R uses IEC 60559 on all platforms along with the C function' pow system for operator ^. Relevant standards define the result in many In particular, the result in the above example is approved by the standard C99. On many Unix-like systems, the "man pow" command provides information about the value in a large number of angular cases.

So, you need to perform the operations separately:

 R> ((-8/27)^2)^(1/3) [1] 0.4444444 
+7
source share

Here's the operation in the complex area that R supports:

  (-8/27+0i)^(2/3) [1] -0.2222222+0.3849002i 

Test:

 > ((-8/27+0i)^(2/3) )^(3/2) [1] -0.2962963+0i > -8/27 # check [1] -0.2962963 

In addition, complex pairing is also the root:

 (-0.2222222-0.3849002i)^(3/2) [1] -0.2962963-0i 

To the question, what is the third root of -8/27:

 polyroot( c(8/27,0,0,1) ) [1] 0.3333333+0.5773503i -0.6666667-0.0000000i 0.3333333-0.5773503i 

The average value is the real root. Since you say -8/27 = x ^ 3, you are really asking for a solution to the cubic equation:

  0 = 8/27 + 0*x + 0*x^2 + x^2 

The polyroot function needs these 4 coefficient values ​​and will return complex and real roots.

+7
source share

All Articles