Page 1 of 1
Arithmetic questions
Posted: Thu Mar 18, 2010 2:40 pm
by milan1612
I'm working on a side-project (in Java) where I need an algebraic
expression parser.
It's done by now and I'm running various tests on it now. One of them involves
testing this
expression: -1^-2 (no brackets on purpose).
While my understanding tells me that it should get evaluated as (-1)^(-2) = 1,
Google Calculator tells me that it should be -(1^(-2)) = -1.
I've read that the negation operator applies to the right-most operand (1 in this case)
and the operand needs to be "bracketised" if this behaviour is not intended.
What do you mean? Who's wrong, me or Google?

Re: Arithmetic questions
Posted: Thu Mar 18, 2010 2:47 pm
by Kaeru Gaman
Google is wrong, as usual.
feel free to let your
parser do it the right way.
drop a note into the readme and add "love it or leave it"...

Re: Arithmetic questions
Posted: Thu Mar 18, 2010 2:54 pm
by gnasen
google is right, because without any brackets, multiplication goes for addition. The power in math is just applied multiplcation, so it goes like this:
-(1^-2) where -2 is one expression, so its in the end: -(1^(-2)) and this is what google tells you.
Re: Arithmetic questions
Posted: Thu Mar 18, 2010 2:59 pm
by blueznl
gnasen wrote:google is right, because without any brackets, multiplication goes for addition. The power in math is just applied multiplcation, so it goes like this:
-(1^-2) where -2 is one expression, so its in the end: -(1^(-2)) and this is what google tells you.
It depends if you see the minus character as an operator, or as part of the value. There is no formal standard as far as I know, so I'd always group them...
(-1) ^ (-2)
I personally prefer the interpretation above over this one:
0 - (1 ^ (-2))
... because in the second one the minus character is interpreted differently, first as an operator and then as part of the value.
Well, it's the programmer who decides, after all

Re: Arithmetic questions
Posted: Thu Mar 18, 2010 3:16 pm
by Rook Zimbabwe
WHOOOSSSSSHHHH!!!!!
This is the sound of this argument going straight over my numerically dyslexic head!!!

Re: Arithmetic questions
Posted: Thu Mar 18, 2010 3:24 pm
by gnasen
c-a*...*a = c+(-a*...*a) = c+((-1) * (a*...*a)) = c+(-1) * (a*...*a)
here we go: c-a^b = c+(-1) * a^b,
so -1^-2 = 0+(-1)*(1^-2) = 0+(-1) * 1 = -1
Its the same if you see a-b as an operator or as part of the value, because a-b is just defined as a+(b^-1) where b^-1 is the additive inverse of b. Further the associativity allows us to see -a*...*a as the same like -(a*...*a)
Re: Arithmetic questions
Posted: Thu Mar 18, 2010 3:25 pm
by milan1612
Okay, after seeing that Wolfram Alpha does exactly like Google does I lowered
the priority of unary minus to match multiplication and division.
It seems to be a convention widely used, so one should obey to that.
Thanks for your answers, they conviced me

Re: Arithmetic questions
Posted: Thu Mar 18, 2010 3:46 pm
by Trond
Actually, Google MUST be doing SOMETHING wrong. Because -5/2 turns into (-5)/2 while -5^2 turns into -(5^2). If the unary minus is treated as an operator, it should be given the priority of addition, according to wikipedia. Not that it matters, since the result is the same, but in theory it's wrong.
Re: Arithmetic questions
Posted: Thu Mar 18, 2010 4:36 pm
by gnasen
I dont get your point: -5/2 = (-5)/2 = -(5/2). You can tread it as you want.
Re: Arithmetic questions
Posted: Thu Mar 18, 2010 8:00 pm
by Trond
gnasen wrote:I dont get your point: -5/2 = (-5)/2 = -(5/2). You can tread it as you want.
It's not elegant to use the wrong method even if the answer is right.
Re: Arithmetic questions
Posted: Thu Mar 18, 2010 8:12 pm
by Michael Vogel
All
math books I read in the fractional part of the last hundred years had identical rules: a unary "-" is connected to its nearest term, what means "-1" is "-1" and not "X - 1" independendly what comes behind...
If a
parser is not able to decode such things, it won't work either for the "-1" nor the "-2" in the "-1^-2"
expression (the program Eigenmath is an example for such a simple
parser). In such a case the
expression has to be modified to nothing else but "(-1)^(-2)"
Michael