Precedence rules are tricky, in general, and the usual
advice with most programming languages is to liberally use
parentheses when in doubt.  Its actually not that surprising
in this case but consider 0-1:3 and -1:3 which give different
results since one uses binary minus and the other uses
unary minus and the order of precedence from highest to
lowest is unary minus, : and binary minus.  If one used
parentheses in these cases it would be clear even without
detailed knowledge of the precedence rules (which likely
no one can remember anyways).

On 1/6/06, roger bos <[EMAIL PROTECTED]> wrote:
> How do people even notice stuff like this.  You would never hard-coding
> (-2)^2 or -2^2 anyway. The part being squared would be a variable, in which
> case it works correctly:
>
> > a<- -2
> > a
> [1] -2
> > a^2
> [1] 4
>
> Sometimes it seems that people go looking for bugs... and therefore see bugs
> all around them.
>
> On 1/6/06, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
> >
> > On 1/6/06, Thomas Lumley <[EMAIL PROTECTED]> wrote:
> > > On Fri, 6 Jan 2006, [EMAIL PROTECTED] wrote:
> > >
> > > > hi - in version 2.1 the command
> > > >
> > > > >-2^2
> > > >
> > > > gives
> > > >
> > > > -4
> > > >
> > > > as the answer.  (-2)^2 is evaluated correctly.
> > >
> > > So is -2^2.  The precedence of ^ is higher than that of unary minus. It
> > > may be surprising, but it *is* documented and has been in S for a long
> > > time.
> >
> > See ?Syntax
> >
> > ______________________________________________
> > R-devel@r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-devel
> >
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-devel@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

______________________________________________
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel

Reply via email to