This question already has answers here:
Closed 4 years ago.
I executed the following code:
tau <- 0.25
h <- 0.6 * n ^ (-1 / 5) * (4.5 * dnorm(qnorm(tau)) ^ 4 * qnorm(tau) /
(2 * (qnorm(tau) ^ 2 + 1)) ^ 2) ^ (1/5),
and R keeps producing NaN
. However, R actually computes (4.5 * dnorm(qnorm(tau)) ^ 4 * qnorm(tau) / (2 * (qnorm(tau) ^ 2 + 1)) ^ 2)
to be equal to -0.003655336
.
The weird thing is when I did the following
k <- -0.003655336
k ^ (1 / 3)
NaN
was produced again.
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 NaN
in 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.