R Exponent Produces NaN

那年仲夏 提交于 2021-01-29 01:56:40

问题


I am running into an issue when I exponentiate floating point data. It seems like it should be an easy fix. Here is my sample code:

temp <- c(-0.005220092)
temp^1.1

[1] NaN

-0.005220092^1.1

[1] -0.003086356

Is there some obvious error I am making with this? It seems like it might be an oversight on my part with regard to exponents.

Thanks,

Alex


回答1:


The reason for the NaN is because the result of the exponentiation is complex, so you have to pass a complex argument:

as.complex(temp)^1.1
[1] -0.002935299-0.000953736i
# or
(temp + 0i)^1.1
[1] -0.002935299-0.000953736i

The reason that your second expression works is because unary - has lower precedence than ^, so this is equivalent to -(0.005220092^1.1). See ?Syntax.



来源:https://stackoverflow.com/questions/36348409/r-exponent-produces-nan

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