[R] Power functions?

Marc Schwartz marc_schwartz at comcast.net
Sat Jan 3 23:04:00 CET 2009


on 01/03/2009 02:32 PM rkevinburton at charter.net wrote:
> I had a question about the basic power functions in R.
> 
> For example from the R console I enter:
> 
> -1 ^ 2 [1] -1
> 
> but also
> 
> -1^3 [1] -1
> 
> -0.1^2 [1]  -0.01
> 
> Normally pow(-1, 2) return either -Infinity or NaN. Has R taken over
> the math functions? If so I would think that -1^2 is 1 not -1 and
> -0.1^2 is 0.01 not -0.01.
> 
> Thank you.
> 
> Kevin

Kevin,

See R FAQ 7.33 Why are powers of negative numbers wrong?

http://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-are-powers-of-negative-numbers-wrong_003f


For the issue of returning -Inf or NaN, I suspect that you are thinking
about negative numbers being raised to non-integer powers. For example,
taking into consideration the enlightenment in the above FAQ:

> (-1) ^ (1 / 2)
[1] NaN

> (-2) ^ (1/2)
[1] NaN

> sqrt(-2)
[1] NaN
Warning message:
In sqrt(-2) : NaNs produced

> (-2) ^ 2.5
[1] NaN


HTH,

Marc Schwartz




More information about the R-help mailing list