Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
Ask HN: What is -2^2?
12 points by laujen on April 2, 2011 | hide | past | favorite | 13 comments
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 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.

Miscellaneous 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


That Math Forum post is excellent. Thanks!


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."


Thanks for addressing the issue. A lot of people are taking this as an opinion question: it's not.


I've always been taught that (-2)^2 = 4, while -2^2 = -4. This has stayed true all through college calculus, but I can't prove it.


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.


I read it as shorthand for (-1) * 2^2, so -4.


For the record, the TI-83 is -4 and the HP48 is 4. TI-83 (or now TI-84) is the dominant calculator in US math classes.


HP48 is RPN, which sidesteps precedence issues. So, here you explicitly need to decide if you want 2 NEG 2 ^ (i.e. (-2)^2) or 2 2 ^ NEG (i.e. -(2^2)).

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.


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.


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 think that precedence rules are evil.




Consider applying for YC's Fall 2025 batch! Applications are open till Aug 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: