in reply to Operator Precedence (unary negation and exponentiation)

How can I put this?

Ah yes, you're wrong. -2**2 is -4, (-2)**2 is 4.

Assume that -2**2 = 4, now add zero to both sides and you have 0 - 2**2 = 0 + 4, reduce the exponent and you have 0 - 4 = 0 + 4 (exponentiation definitely binds tighter than subtraction), which reduces to -4 = 4 which is obviously not true.

Replies are listed 'Best First'.
Re: Re: Operator Precedence (unary negation and exponentiation)
by Elliott (Pilgrim) on Jan 18, 2003 at 15:56 UTC
    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.

      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.