From ggrothendieck at gmail.com  Fri Jan  6 17:09:10 2006
From: ggrothendieck at gmail.com (Gabor Grothendieck)
Date: Fri, 6 Jan 2006 11:09:10 -0500
Subject: [Rd] Multiplication (PR#8466)
In-Reply-To: <1db726800601060756i49b0c12ar93b72ad43a028901@mail.gmail.com>
References: <20060106153317.0D12BE7B9@slim.kubism.ku.dk>
	<1db726800601060756i49b0c12ar93b72ad43a028901@mail.gmail.com>
Message-ID: <971536df0601060809t147f2095l7bf6ef38f42851a1@mail.gmail.com>

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 <roger.bos at gmail.com> 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, ggrothendieck at gmail.com <ggrothendieck at gmail.com> wrote:
> >
> > On 1/6/06, Thomas Lumley <tlumley at u.washington.edu> wrote:
> > > On Fri, 6 Jan 2006, casella at stat.ufl.edu 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 at r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-devel
> >
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>



[Rd] Multiplication (PR#8466)
Gabor Grothendieck