R expression results in NaN for no obvious reason [duplicate]

荒凉一梦 提交于 2019-11-28 10:08:59

问题


This question already has an answer here:

  • How to calculate any negative number to the power of some fraction in R? 2 answers

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.


回答1:


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.



回答2:


From what I can tell, -50.61828^(-67.421587) is evaluating as -(50.61828^(-67.421587)). (-50.61828)^(-67.421587) also results in NaN.



来源:https://stackoverflow.com/questions/38061082/r-expression-results-in-nan-for-no-obvious-reason

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!