Post Reply 
negative number raised to even power
05-07-2015, 02:01 PM
Post: #63
RE: negative number raised to even power
(05-07-2015 12:25 PM)Dave Britten Wrote:  I'm of the opinion that the negation sign is a part of the number itself, not an operation, and thus -2^2 = 4. Yes, you can say that -2 = 0 - 2, but you can also say that 2.5 = 2 + .5, and 2.5^2 isn't 2.25, is it?

In CS terms, fully parsing a numeric literal is atomic, and precedes any operations on that number.

From the parser point of view:
Giving unary minus high precedence to make it "stick" to the number quickly gives -2^2=4. Lowering its precedence with respect to the power will make -2^2=-(2^2).

But what happens when the literal is not a number? In the first case:
-X^2 = (-X)^2 = X^2
And in the second case -X^2=-(X^2)

While the result of -2^2 could be debatable, I think 100% of the readers here will agree that the result of -X^2 is not X^2.
On an algebraic parser, it seems (humanly) reasonable that:
-X^2+3*X+4 ≠ X^2+3*X+4
-X^2+3*X+4 = -1*X^2+3*X+4
-X^2+3*X+4 = 4+3*X-X^2

In Excel (used here as an example of a parser that does -2^2=4), none of the three equations above is true.
In general, a simple parser that was designed only for numeric purposes (like a non-algebraic calculator or Excel), might choose to use -2^2=4, but the more advanced algebra the device/software does, it will be forced to use -2^2=-4.
I don't know of any compiler or language interpreter that would fail the 3 expressions above (using compilers/interpreters as the main example of what's state of the art in Computer Science parsers). I checked BASIC, Lua, Python and Haskell (C, Java, JavaScript are out of the question since they don't have power operator).
From that point of view, Computer Science seems to agree that the way to go is -2^2=-4.
While in my opinion there is no right or wrong answer, everybody (including CS) is moving towards -2^2=-4, as it allows for a more natural algebraic manipulation.
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
negative number raised to even power - DrD - 04-25-2015, 11:24 AM
RE: negative number raised to even power - Claudio L. - 05-07-2015 02:01 PM



User(s) browsing this thread: 1 Guest(s)