[Rd] Multiplication (PR#8466)
Gabor Grothendieck
ggrothendieck at gmail.com
Fri Jan 6 17:09:10 CET 2006
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
>
More information about the R-devel
mailing list