Arithmetic questions

For everything that's not in any way related to PureBasic. General chat etc...
milan1612
Addict
Addict
Posts: 894
Joined: Thu Apr 05, 2007 12:15 am
Location: Nuremberg, Germany
Contact:

Arithmetic questions

Post 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? :lol:
Windows 7 & PureBasic 4.4
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Re: Arithmetic questions

Post 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"... ;)
oh... and have a nice day.
gnasen
Enthusiast
Enthusiast
Posts: 282
Joined: Wed Sep 24, 2008 12:21 am

Re: Arithmetic questions

Post 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.
pb 5.11
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6172
Joined: Sat May 17, 2003 11:31 am
Contact:

Re: Arithmetic questions

Post 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 :-)
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB - upgrade incoming...)
( The path to enlightenment and the PureBasic Survival Guide right here... )
User avatar
Rook Zimbabwe
Addict
Addict
Posts: 4322
Joined: Tue Jan 02, 2007 8:16 pm
Location: Cypress TX
Contact:

Re: Arithmetic questions

Post by Rook Zimbabwe »

WHOOOSSSSSHHHH!!!!!

This is the sound of this argument going straight over my numerically dyslexic head!!!

:D
Binarily speaking... it takes 10 to Tango!!!

Image
http://www.bluemesapc.com/
gnasen
Enthusiast
Enthusiast
Posts: 282
Joined: Wed Sep 24, 2008 12:21 am

Re: Arithmetic questions

Post 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)
Last edited by gnasen on Thu Mar 18, 2010 3:26 pm, edited 1 time in total.
pb 5.11
milan1612
Addict
Addict
Posts: 894
Joined: Thu Apr 05, 2007 12:15 am
Location: Nuremberg, Germany
Contact:

Re: Arithmetic questions

Post 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 :)
Windows 7 & PureBasic 4.4
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Re: Arithmetic questions

Post 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.
gnasen
Enthusiast
Enthusiast
Posts: 282
Joined: Wed Sep 24, 2008 12:21 am

Re: Arithmetic questions

Post by gnasen »

I dont get your point: -5/2 = (-5)/2 = -(5/2). You can tread it as you want.
pb 5.11
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Re: Arithmetic questions

Post 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.
User avatar
Michael Vogel
Addict
Addict
Posts: 2816
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: Arithmetic questions

Post 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
Post Reply