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?
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.
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 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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With