[R] sqrt(-x) vs. -x^0.5

Sarah Goslee sarah.goslee at gmail.com
Thu Mar 22 00:17:01 CET 2012


It's order of operations, and a good reason to always use
parentheses: which is evaluated first, the unary minus or
the raising-to-powers?

(-4)^0.5
-(4^0.5)

sqrt(-4)
-sqrt(4)

Sarah

On Wed, Mar 21, 2012 at 7:09 PM, Mike Williamson <this.is.mvw at gmail.com> wrote:
> Hi Everyone,
>
>    I did a search through the archives and did not find an answer,
> although I must admit it is a hard search to do ( ^0.5 is tough to
> explicitly search for ).
>
>    I am sure there is some mathematically accurate reason to explain the
> following, but I guess I either never learned it or have since forgotten it.
>
>    In 'R', when I type (for instance):
>
> sqrt(-4)
>
>    I get
>
> NaN
>
>    but if I type in:
>
> -4 ^ 0.5
>
>    I get
>
> -2
>
>    I presume this is on purpose and correct, but it's the first time I've
> come across any theoretical difference between ^0.5 and sqrt.  What is the
> theoretical difference / meaning between these two operations?
>
>                              Thanks!
>                                   Mike
>

-- 
Sarah Goslee
http://www.functionaldiversity.org



More information about the R-help mailing list