Re: Power function as operator
| From: | Chris London | Date: | Sat, 23 Nov 2013 05:42:44 +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-70316@lists.php.net to get a copy of this message | ||
On Fri, Nov 22, 2013 at 5:12 PM, 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?
>
Every calculator I've tried today does "-3 ** 2 == -9"
- Mac built-in calculator
- Google search calculator https://www.google.com/search?q=-3+%5E+2
- Wolfram Alpha http://www.wolframalpha.com/input/?i=-3+%5E+2
In my GMAT prep book it explains that you have to enclose the negative in
parentheses like (-3) ^ 2 to get +9.
So while it may seem counter-intuitive, that's how math and the order of
operations work.