On 13 January 2012 20:54, Stewart Gordon <smjg_1998@yahoo.com> wrote:
On 13/01/2012 13:47, Manu wrote:
<snip>
Some people expect this:
(-10 ^^ 2)
To be 100 instead of -100
<snip>
I'm fairly amazed it's not the other way around... what's the logic behind this?
It matches standard mathematical notation. -x² means -(x²) not (-x)².
This actually makes most sense when you consider that:
(a) -2x² means -2(x²), because exponentiation beats multiplication. With the precedence you're suggesting, removal of the 2 would completely change the expression.
(b) 42 - x² means 42 - (x²). With the precedence you're suggesting, removal of the 42 would completely change the expression.
Both these rules play a significant part in how we write polynomial expressions. Look at these:
x³ - x² + 3
- x² + 3
-4x² + 3
In all these, the coefficient of x² is negative. It would be confusing if it were positive only in the second one.
It might help to think of -Exp as syntactic sugar for 0-Exp.
I think there's one very important point to realise in all your examples though...
We're NOT writing -4x² + 3. We write -4 * x ^^ 2 + 4. That's not a polynomial expressions, it's source code.
I don't know about you, but the visual similarity is just not there for me. I can't see C/D/Java/whatever code as a direct transcription of mathematical notation no matter how hard I squint at it.
I personally have the presumption that unary operators have a higher precedence than binary operators... period. I wouldn't give that a second thought, and that trumps all other logic for me.
Secondary to that, when looking at that statement and deciding which of the * or ^^ might have higher precedence, I would probably only then consider that '^^' *may* have higher precedence than '*', but still not certain, and I wouldn't be surprised (possibly assume) it had the same, ie. left to right, as with * and /.