【问题标题】:R expression results in NaN for no obvious reason [duplicate]R 表达式无明显原因导致 NaN [重复]
【发布时间】:2016-10-29 21:18:46
【问题描述】:

怎么会有这样的表情

> (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

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

【问题讨论】:

标签: r expression nan equation


【解决方案1】:

是因为pow在C99标准下的实现。

更不用说 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.

【讨论】:

  • 那为什么 (-10)^(-60) 会导致 1e-60?
  • 啊,我明白了。确实,相当明显。该函数根本没有为特定范围定义。
【解决方案2】:

据我所知,-50.61828^(-67.421587) 的评估结果为-(50.61828^(-67.421587))(-50.61828)^(-67.421587) 也会产生 NaN。

【讨论】:

  • 查看我编辑的问题,我复制了错误的值,但是,你是对的。但是为什么结果还是NaN呢?
  • xy 都是负数且非整数时,你如何定义x^y
猜你喜欢
  • 2021-08-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-05
  • 2013-07-20
  • 1970-01-01
相关资源
最近更新 更多