Is -2^2 equal to 4 or -4? which has precedence, the negate or the power? TI algebraic calculators do it one way while HP's algebraic calculators do it another.
I remember Stoustrup saying that this question was the reason that C++ doesn't have a double-asterisk operator. Everyone seems to give a different "obvious" answer.
I believe the most common answer is -4, due to exponentiation having a higher precedence than negation. I'm not sure it's completely standard, though. In written mathematics it would be more common to write (-2)^2 if you really meant the -2 to be exponentiated. But, it might be different if you were using a style of traditional mathematics typesetting where unary negation and subtraction are more clearly distinguished. I could imagine something like 5 - -2^2 meaning to subtract (-2)^2 from 5, if it were written in a typesetting style where the unary minus was smaller and clearly more "bound" to the 2 than the subtraction was.
I apologize. You are correct, the HP48gx is RPN only. Don't know why I thought otherwise. I had it in my head for some reason that 48 did both (it kind of does but not really) and that it gave the answer as 4.
Considering operator precedence http://en.wikipedia.org/wiki/Order_of_operations
Quote:
"Unfortunately, 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 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. [1]. In any case where there is a possibility that the notation might be misinterpreted, it is advisable to use parentheses to clarify which interpretation is intended."
Just remember PEMDAS: Parentheses first, Exponents next, Multiply/Divide, Add/Subtract. This is the order for dealing with simple math manipulations; don't trust a calculator that tells you otherwise.
Somewhat related, in the Io language, I was surprised to find that the expression "-23 abs" evaluates to "-23" not +23. The language desugars the expression to "0-(23 abs)". I think what threw me was the fact that Io uses a space rather than a dot for member selectors. No one would be surprised that C++ evaluates "-numObj.abs()" that way.
13 comments
[ 2.8 ms ] story [ 38.1 ms ] threadMiscellaneous support: a random elementary algebra textbook: http://infinity.cos.edu/algebra/Rueger%20Text/Chapter02/2.6_... and a dude from mathforum.org: http://mathforum.org/library/drmath/view/53240.html
The traditional order of operations is what the TI series does. In essence, it assumes you want the second form.
By the way, on my HP50G in algebraic mode, typing '-2^2' does give -4.