[R] Problems raised to 1/3 power and NaN
Bock, Michael
MBock at arcadis-us.com
Tue Apr 27 18:27:18 CEST 2004
I see the light! I have gotten response re precedence :
i.e (-2)^2 = 4 and -2^2 = -4 (^ evaluated before -)
I understood precedence and still could not see the problem but as Peter states:
Powers of negative numbers are not defined for non-integral exponents.
You need something like
cuberoot <- function(x)sign(x)*abs(x)^(1/3)
I though you could take the cubed root of negative numbers (basic algebra) but perhaps this does not hold true for computer math due to rounding of the exponents.
I am on my way! . Thanks 10^6
Mike
-----Original Message-----
From: Peter Dalgaard [mailto:p.dalgaard at biostat.ku.dk]
Sent: Tuesday, April 27, 2004 12:17 PM
To: Bock, Michael
Cc: R-help at stat.math.ethz.ch
Subject: Re: [R] Problems raised to 1/3 power and NaN
"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