Re: Power function as operator
| From: | Tjerk Meesters | Date: | Sat, 23 Nov 2013 05:45:31 +0000 |
| Subject: | Re: Power function as operator | ||
| References: | 1 2 3 4 5 6 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-70317@lists.php.net to get a copy of this message | ||
Hi,
On Sat, Nov 23, 2013 at 8:12 AM, Kris Craig <kris.craig@gmail.com> wrote:
> On Fri, Nov 22, 2013 at 3:03 PM, Sherif Ramadan <theanomaly.is@gmail.com
> >wrote:
>
> > > On Fri, Nov 22, 2013 at 3:17 PM, Niel Archer <
> spam-free@blueyonder.co.uk
> > > >wrote:
> > >
> > > > Hi,
> > > >
> > > >
> > > > > So:
> > > > > -3 ** 2 == -9
> > > >
> > > > Shouldn't this be +9
> > > >
> > > >
> > >
> >
> > To add to this note, the negation operator in PHP is left-associative,
> > whereas the patch here makes ** right-associative. So the result is
> > expected to be -9 and not 9 given the static scalar expression should be
> > evaluated as -(3**2) based on operator associativity.
> >
>
> This really bothers me. I understand that, since "-3" is basically treated
> as "(-1) * 3", we'd end-up with odd behavior like this. But can't we find
> a way around that? "-3 ** 2 == -9" would just open-up the flood gates to
> code bugs all over the place. Can't we make it so that "-3" is treated as
> "(-1 * 3)" instead of "(-1) * 3", or would that break things?
>
First of all, I don't think this operation will be used in many projects,
so I don't think there will be any "code bug floods" by having defined this
behaviour.
That's not to say it's entirely intuitive; while I was researching the
operator precedence rules I saw at least a couple of threads with confused
developers, to which the reply has always been "if it doesn't work the way
you want, you're not using enough parentheses" :)
Second, the unary minus is only deduced from the absence of a second
operand; when you write it down on paper you would put the unary minus
closer to the next operand whereas the binary minus would have ample
spacing on either side to disambiguate its intention. Unfortunately,
programming languages don't work that way and I wouldn't expect this to
change in the foreseeable future.
>
> If we're going to have a pow operator, then it needs to be intuitive
> without requiring footnotes on counter-intuitive behavior when dealing with
> a negative base, otherwise there's really no reason for having it.
>
> --Kris
>
--
--
Tjerk