[R] Problems raised to 1/3 power and NaN

Peter Dalgaard p.dalgaard at biostat.ku.dk
Tue Apr 27 18:17:00 CEST 2004


"Bock, Michael" <MBock at arcadis-us.com> writes:

> > Wq
> [1] -10.72508
> > Wq <- Wq^(1/3)
> > Wq
> [1] NaN
> > z <- -10.72508^(1/3)
> > z
> [1] -2.205296
> 
> 
> as you can see if Wq = -10.72508,  Wq^(1/3) is NaN but
> z<- -10.72508^(1/3) returns a number.

Hint:
> -2^(1/2)
[1] -1.414214
> sqrt(-2)
[1] NaN
Warning message:
NaNs produced in: sqrt(-2)
> sqrt(-2+0i)
[1] 0+1.414214i

Powers of negative numbers are not defined for non-integral exponents.
You need something like

cuberoot <- function(x)sign(x)*abs(x)^(1/3)

-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907




More information about the R-help mailing list