Pow operator precedence
Stewart Gordon
smjg_1998 at yahoo.com
Fri Jan 13 10:54:01 PST 2012
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.
Stewart.
More information about the Digitalmars-d
mailing list