R Exponent产生NaN [英] R Exponent Produces NaN

查看:51
本文介绍了R Exponent产生NaN的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对浮点数据求幂时遇到问题.似乎应该很容易解决.这是我的示例代码:

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.

谢谢

亚历克斯

推荐答案

使用NaN的原因是因为求幂的结果很复杂,因此您必须传递一个复杂的参数:

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

第二个表达式起作用的原因是因为一元-的优先级低于 ^ ,所以这等效于-(0.005220092 ^ 1.1).参见?语法.

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.

这篇关于R Exponent产生NaN的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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