News for hackers. Stuff that matters.

Ask HN: What is -2^2?

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.
( 13 comments | 12 points | on HN )
Re:Ask HN: What is -2^2? by _delirium on Friday April 01, 2011 @10:19PM (#2398904)
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

Re:Ask HN: What is -2^2? by laujen on Friday April 01, 2011 @10:39PM (#2398933)
That Math Forum post is excellent. Thanks!
Re:Ask HN: What is -2^2? by hector_ka on Friday April 01, 2011 @11:23PM (#2399019)
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."
Re:Ask HN: What is -2^2? by zbanks on Saturday April 02, 2011 @12:38AM (#2399171)
Thanks for addressing the issue. A lot of people are taking this as an opinion question: it's not.
Re:Ask HN: What is -2^2? by hucker on Friday April 01, 2011 @10:18PM (#2398901)
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.
Re:Ask HN: What is -2^2? by uncle_ruckus on Friday April 01, 2011 @11:38PM (#2399050)
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.
Re:Ask HN: What is -2^2? by phaedrus on Saturday April 02, 2011 @01:59PM (#2400511)
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.
Re:Ask HN: What is -2^2? by Bo102010 on Friday April 01, 2011 @10:31PM (#2398920)
I read it as shorthand for (-1) * 2^2, so -4.
Re:Ask HN: What is -2^2? by laujen on Friday April 01, 2011 @10:40PM (#2398934)
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.
Re:Ask HN: What is -2^2? by yaks_hairbrush on Friday April 01, 2011 @11:21PM (#2399016)
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.

Re:Ask HN: What is -2^2? by laujen on Saturday April 02, 2011 @11:14AM (#2400061)
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.
Re:Ask HN: What is -2^2? by Animus7 on Friday April 01, 2011 @10:12PM (#2398894)
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.
Re:Ask HN: What is -2^2? by serichsen on Saturday April 02, 2011 @12:00PM (#2400171)
I think that precedence rules are evil.