R 表达式导致 NaN 没有明显的原因

R expression results in NaN for no obvious reason

怎么会是这个表达式呢

> (exp(17.118708 + 4.491715 * -2)/-67.421587)^(-67.421587)

结果

[1] NaN

> -50.61828^(-67.421587)

基本上应该有相同的结果,给了我

[1] -1.238487e-115

这快把我逼疯了,我花了好几个小时寻找错误。在这种情况下,“-2”是函数的参数。我真的想不出解决办法。感谢您的帮助!

编辑:

我在添加括号时看到了

> (-50.61828)^(-67.421587)

它也会导致

[1] NaN

...但这并不能解决我的问题。

据我所知,-50.61828^(-67.421587) 的计算结果为 -(50.61828^(-67.421587))(-50.61828)^(-67.421587) 也会导致 NaN。

是因为在C99标准下实现了pow

更不用说OP的例子:(-50.61828)^(-67.421587),数学上合理的(-8)^(1/3) = -2在R中不起作用:

(-8)^(1/3)
# [1] NaN

引自?"^":

 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.

我在 Ubuntu LINUX,所以可以帮助打印这里 man power 的相关部分:

   If x is a finite value less than 0, and y is  a  finite  noninteger,  a
   domain error occurs, and a NaN is returned.