[R] Odp: ^ operator

(Ted Harding) Ted.Harding at manchester.ac.uk
Mon Nov 16 14:41:30 CET 2009


On 16-Nov-09 13:13:27, Liviu Andronic wrote:
> On 11/16/09, Ted Harding <Ted.Harding at manchester.ac.uk> wrote:
>> Not in this case (see below), though of course in general "-" takes
>>  precedence over "^", so, for example, in the expression
>>
>>   -2^(1/3)
>>
>>  the "-" is applied first, giving (-2); and then "^" is applied
>>  next, giving (-2)^(1/3). There is a work-round (see below).
>>
> Hmm.. I may be doing something wrong, but from here it looks to be the
> opposite.
>> -2^(1/3); -(2)^(1/3); -(2^(1/3));
> [1] -1.2599
> [1] -1.2599
> [1] -1.2599
>> (-2)^(1/3)
> [1] NaN
> 
> The results don't change when switching from the unary minus.
>> 0-2^(1/3); 0-(2)^(1/3); 0-(2^(1/3));
> [1] -1.2599
> [1] -1.2599
> [1] -1.2599

Correct!!! I was inadvertently put on the wrong foot by Pietr
Pikal's comment about precedence, and as a result what I wrote
about precedence of "^" relative to "-" was on the wrong foot
throughout, and should be ignored. My apologies for any confusion
this may have caused to anybody.

In any case, this is not relevant to Carol White's query about
taking the cube root (or indeed any fractional power) of a
negative number. This can only be done (as Carol intended it)
by using the form sign(x)*(abs(x)^power).

As I tried to point out, there is a distinction between an
expression which the user may enter as x <- -1.234, and then
x^(1/3), expecting -(1.234^(1/3)), and the cube root of the
negative number x.

Ted.

> It seems to me that in this example "^" is applied first, and "-"
> second. There is also this fortune entry.
>> fortune("unary")
> 
> Thomas Lumley: The precedence of ^ is higher than that of unary minus.
> It may be surprising,
> [...]
> Hervé Pagès: No, it's not surprising. At least to me... In the
> country
> where I grew up, I've
> been teached that -x^2 means -(x^2) not (-x)^2.
>    -- Thomas Lumley and Hervé Pagès (both explaining that operator
> precedence is working
>       perfectly well)
>       R-devel (January 2006)
> 
> 
> Liviu
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 16-Nov-09                                       Time: 13:40:06
------------------------------ XFMail ------------------------------



More information about the R-help mailing list