Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

In the following code:

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

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

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

like image 862
Blaszard Avatar asked Oct 17 '13 21:10

Blaszard


People also ask

How do you calculate negative powers?

Negative Exponent Rule 1: For every number “a” with negative exponents “-n” (i.e.) a-n, take the reciprocal of the base number and multiply the value according to the value of the exponent number. Here, the base number is 4 and the exponent is -3. Hence, the value of 4-3 is 1/64.

How do you raise something to a power in R?

In R, for the calculation of power we can simply use power operator ^ and this will be also used in case of generating a power sequence. For example, if we want to generate a power sequence from 1 to 5 of 2 then we can use the code 2^(1:5) this will result 2 4 8 16 32.

What is the rule for a fractional base raised to a negative exponent?

What is negative exponent? A negative exponent helps to show that a base is on the denominator side of the fraction line. In other words, the negative exponent rule tells us that a number with a negative exponent should be put to the denominator, and vice versa.


1 Answers

As documented in help("^"):

Users are sometimes surprised by the value returned, for example why ‘(-8)^(1/3)’ is ‘NaN’. For double inputs, R makes use of IEC 60559 arithmetic on all platforms, together with the C system function ‘pow’ for the ‘^’ operator. The relevant standards define the result in many corner cases. In particular, the result in the example above is mandated by the C99 standard. On many Unix-alike systems the command ‘man pow’ gives details of the values in a large number of corner cases.

So you need to do the operations separately:

R> ((-8/27)^2)^(1/3)
[1] 0.4444444
like image 164
Joshua Ulrich Avatar answered Sep 29 '22 11:09

Joshua Ulrich