R表达导致NaN没有明显的原因 [英] R expression results in NaN for no obvious reason

查看:137
本文介绍了R表达导致NaN没有明显的原因的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何表达

 > (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)^( - 在数学上合理的( - 8)^(1/3)= -2 在R中不起作用:

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

引用自?^

 用户有时会对返回的值感到惊讶,例如
为什么'(-8)^(1/3)'是'NaN'。对于双输入,R在所有平台上使用IEC
60559算术,以及'^'运算符的C系统
函数'pow'。相关标准
在许多角落定义了结果。特别地,上述示例中的结果
是由C99标准规定的。在许多
Unix系统上,命令man pow在大量角落中给出了
值的详细信息。

我在Ubuntu LINUX上,所以可以帮助获得人力的相关部分打印在这里:

 如果x是小于0的有限值,y是有限非整数,发生
域错误,并返回NaN。


How can it be that the expression

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

results in

[1] NaN

while

> -50.61828^(-67.421587)

which should basically have the same outcome, gives me

[1] -1.238487e-115

This is driving me crazy, I spent hours searching for the Error. "-2", in this case, is a Parameter of the function. I really can't think of a solution. Thanks for your help!

EDIT:

I see that when I add brackets

> (-50.61828)^(-67.421587)

it also results in

[1] NaN

...but that does not solve my Problem.

解决方案

It is because of the implementation of pow under C99 standard.

Let alone OP's example: (-50.61828)^(-67.421587), the mathematically justified (-8)^(1/3) = -2 does not work in R:

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

Quoted from ?"^":

 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.

I am on Ubuntu LINUX, so can help get relevant part of man power printed here:

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

这篇关于R表达导致NaN没有明显的原因的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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