TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Ask HN: What is -2^2?

12 pointsby laujenabout 14 years ago
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.

9 comments

_deliriumabout 14 years ago
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.<p>Miscellaneous support: a random elementary algebra textbook: <a href="http://infinity.cos.edu/algebra/Rueger%20Text/Chapter02/2.6_Exponents%20and%20Order%20of%20Operations.pdf" rel="nofollow">http://infinity.cos.edu/algebra/Rueger%20Text/Chapter02/2.6_...</a> and a dude from mathforum.org: <a href="http://mathforum.org/library/drmath/view/53240.html" rel="nofollow">http://mathforum.org/library/drmath/view/53240.html</a>
评论 #2398933 未加载
hector_kaabout 14 years ago
Considering operator precedence <a href="http://en.wikipedia.org/wiki/Order_of_operations" rel="nofollow">http://en.wikipedia.org/wiki/Order_of_operations</a> 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."
评论 #2399171 未加载
huckerabout 14 years ago
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.
uncle_ruckusabout 14 years ago
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.
phaedrusabout 14 years ago
Somewhat related, in the Io language, I was surprised to find that the expression "-23 abs" evaluates to "-23" <i>not</i> +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.
Bo102010about 14 years ago
I read it as shorthand for (-1) * 2^2, so -4.
laujenabout 14 years ago
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.
评论 #2399016 未加载
Animus7about 14 years ago
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.
serichsenabout 14 years ago
I think that precedence rules are evil.