Alex Thompson
Alex Thompson

Reputation: 506

R Exponent Produces 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.

Thanks,

Alex

Upvotes: 2

Views: 724

Answers (1)

Hong Ooi
Hong Ooi

Reputation: 57686

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.

Upvotes: 6

Related Questions