weird NaN when raising a number to a non-integer power [duplicate]

可紊 提交于 2019-12-02 13:02:42

You are calculating the cube root of a negative number. Although, as pointed out by @mra68, a real solution exists, the general case of non-integer exponents of negative numbers results in a complex number. Since by default R assumes that you are dealing with real numbers, it produces NaN.

Try this:

k <- -0.003655336
k <- as.complex(k)
k ^ (1 / 3)
#[1] 0.0770216+0.1334053i

The result is not unique in the sense that there are three complex numbers x satisfying the condition x^3=k (including the case where the imaginary component is zero), but the NaN output is consistent with the general case case of non-integer numbers as exponents of negative real numbers. It may be argued that a distinction between rational and non-rational exponents could be useful, but in floating-point calculations this is hardly possible. I would consider the occurrence of NaNin the case of non-integer exponents of negative numbers as a useful warning sign.

The 5-th root of a negative number is still possible. Also my pocket calculator does it. Since (-1)^5=-1, you can do it this way:

> tau <- 0.25

> a <- (4.5 * dnorm(qnorm(tau)) ^ 4 * qnorm(tau) / (2 * (qnorm(tau) ^ 2 + 1)) ^ 2)

> sign(a)*abs(a)^ (1/5)
[1] -0.3255258
> 

Please edit your question. And you gave yourself the answer. Is basic math. You can't give a power of a negative number to a power of a decimal number. It is an indetermination.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!