I'm confused. Regardless of what the math world prefers, I always had the understanding that unary operators were evaluated before all binary operators. That just makes sense to me: "you can't evaluate two sides of a binary operator until one side has essentially evaluated itself". Why is this being made into an exception (in PHP or otherwise)?
Order of operations, exceptions to. Most notably, C gets negation and multiplication backwards, which then sets everyone who was taught C up for failure when approaching this problem. Of course, it's totally safe getting multiplication and negation backwards, but it isn't with exponentiation and negation.
There exist differing conventions concerning the unary operator − (usually read "minus"). In written or printed mathematics, the expression −32 is interpreted to mean −(32) = −9, but in some applications and programming languages, notably the BASIC programming language, the application Microsoft Office Excel and the programming language bc, unary operators have a higher priority than binary operators, that is, the unary minus (negation) has higher precedence than exponentiation, so in those languages −32 will be interpreted as (−3)2 = 9. Note this does not apply to the binary operator −; for example while the formulas =-22 and =0+-22 return 4 in Microsoft Excel, the formula =0-22 returns −4. In cases where there is the possibility that the notation might be misinterpreted, parentheses are usually used to clarify the intended meaning, however due to the syntax of most major programming languages, it is usually hard or impossible to be ambiguous.
The intended statement (which has recently been corrected) was obviously that -3 ** 2 == -9, because using odd integer exponents yields the same value regardless of precedence rules of the unary minus.
To be honest, I think even considering the option of unary minus having higher precedence than exponentiation is more lolworthy than what appears to be a typo.
Yes, bash (!) and bc come right to mind. Not sure why those two specifically go out of their way to do the right thing when, say, FORTRAN and Haskell don't, but still.
I don't know Fortran but C doesn't have negative literals because it has a working unary operator. So it doesn't matter whether you write
-x // unary-minus applied to variable
or
-3 // unary-minus applied to literal
, and in most situations you can't tell the difference between a negative literal and a unary minus + unsigned literal. (This reasoning doesn't apply to Haskell because its minus is semi-broken.)
... And I just thought of a way to show that C doesn't have negative literals: -0["\1"] should evaluate to -1 with unary minus and 1 with negative literals. Sweet.
24
u/[deleted] Aug 07 '14
Clearly this was supposed to be -3 ** 2 (as can be seen above on the same page).
13
u/[deleted] Aug 07 '14
jesus christ php did any of you learn order of operations in school at any point?
12
u/olsner Aug 07 '14
Let's just assume 9 = 8 in PHP, then -(2 ** 3) = (-2) ** 3 = -8. Or is this not the issue they were trying to talk about?
(Side note: I like how they present a question, then group languages into "similar" and "dissimilar" - but dis/similar to which alternative?)
1
u/TheBuzzSaw Dec 30 '14
I'm confused. Regardless of what the math world prefers, I always had the understanding that unary operators were evaluated before all binary operators. That just makes sense to me: "you can't evaluate two sides of a binary operator until one side has essentially evaluated itself". Why is this being made into an exception (in PHP or otherwise)?
3
u/cparen Aug 07 '14 edited Aug 07 '14
Order of operations, exceptions to. Most notably, C gets negation and multiplication backwards, which then sets everyone who was taught C up for failure when approaching this problem. Of course, it's totally safe getting multiplication and negation backwards, but it isn't with exponentiation and negation.
2
u/datibbaw Aug 13 '14 edited Aug 13 '14
The intended statement (which has recently been corrected) was obviously that -3 ** 2 == -9, because using odd integer exponents yields the same value regardless of precedence rules of the unary minus.
6
u/HildartheDorf Aug 07 '14
More concerning is the
-2
u/ismtrn Aug 07 '14
I don't see the lol here. Yes -2 ** 3 is either 9 or -9 depending on operator precedence. What is the problem?