in reply to Re: Operator Precedence (unary negation and exponentiation)
in thread Operator Precedence (unary negation and exponentiation)

So your argument is that subtraction and negation are the same thing. Perl does not agree with you. They are not next to each other in the precedence table.

Negation is ranked above multiplication while subtraction is ranked below. This means that
-2*3 is evaluated as (-2)*3
while
0-2*3 is evaluated as 0-(2*3)
I cannot think of any practical circumstance where this would lead to a different answer!

I have just been chatting to a mathematician about this and we agreed that the reason we would expect -2**3 to mean (-2)**3 is that we do not think of unary negation as a operation at all. Rather, we think of "-2" as a number.

  • Comment on Re: Re: Operator Precedence (unary negation and exponentiation)

Replies are listed 'Best First'.
Re: Re: Re: Operator Precedence (unary negation and exponentiation)
by pdcawley (Hermit) on Jan 19, 2003 at 02:17 UTC
    Hold on just a moment. One minute you're arguing that Perl's operator precedence should be altered so that it agrees with your flawed understanding the normal order of operations, the next you're arguing that I'm wrong because that's not how Perl's operator table looks? You really can't have it both ways you know.

    Go check any mathematical reference on this, here is pretty much the first explanation that fell off a google search for 'normal order of operation'.

      Actually I wasn't arguing either of those things (though I insist on my right to hold contradictory opinions!)

      I asked why Perl's order of operations does not match my intuition. The answer "negation = subtraction" might explain my failure of intuition but does not explain anything about Perl because Perl does not treat them as the same.

      Thank you for the link. The essay made sense - though it still feels counter-intuitive to me.

        One of the most important parts of learning to do maths (and, as a mathematician I could, if pressed, put up a reasonable argument that programming is just another branch of maths) properly is the process of training your intuition -- usually by checking your intuition at the door and working through stuff step by step until you learn why your intuition was correct (or, rarely, incorrect). Probability theory is the classic area where almost every non-trivial result seems counter intuitive.

        Coming back to this particular thing, negation is not quite the same as subtraction, it is a shorthand for it, and there is a right and a wrong time to expand it. For instance, it's perfectly valid to write 3 * -2. If Perl didn't treat negation as somehow 'special' then that would be a syntax error.