将数字提高为非整数幂时出现奇怪的NaN [英] weird NaN when raising a number to a non-integer power

查看:72
本文介绍了将数字提高为非整数幂时出现奇怪的NaN的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我执行了以下代码:

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),

,R继续产生NaN.但是,R实际上计算出(4.5 * dnorm(qnorm(tau)) ^ 4 * qnorm(tau) / (2 * (qnorm(tau) ^ 2 + 1)) ^ 2)等于-0.003655336.

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.

推荐答案

您正在计算负数的立方根.尽管,如@ mra68所指出的,存在一个实数解,但是负数的非整数指数的一般情况会导致产生一个复数.由于默认情况下R假定您正在处理实数,因此它会生成NaN.

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.

尝试一下:

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

在三个满足条件x^3=k的复数x(包括虚部为零的情况)的意义上,结果不是唯一的,但是NaN的输出与一般情况一致非整数作为负实数的指数的情况.可能会争辩说,有理指数和非有理指数之间的区别可能是有用的,但在浮点计算中这几乎是不可能的.对于负数的非整数指数,我认为NaN的出现是有用的警告信号.

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.

这篇关于将数字提高为非整数幂时出现奇怪的NaN的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆