Re: Power function as operator

From: Date: Sat, 23 Nov 2013 00:12:30 +0000
Subject: Re: Power function as operator
References: 1 2 3 4 5  Groups: php.internals 
Request: Send a blank email to internals+get-70307@lists.php.net to get a copy of this message
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? 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

« previous php.internals (#70307) next »