Page 1 of 1

Google Calculator in PHP?

Posted: Wed Oct 22, 2003 9:51 pm
by nigma
If any of you haven't used Google's calculator go to google and type in something like:
2 + 2 * 3

You will not only get the result, but, google will also rewrite your question inserting parenthesis to show the order of operations. So the previous problem would be re-written as follows:
2 + (2 * 3)

Would anyone be willing to provide an example of how something like this would be done? Pseudocode would be great, I just want to figure out the logic behind something like that, I asume you would use some sort of REGX but not 100% sure.

Posted: Wed Oct 22, 2003 11:46 pm
by volka
you're looking for an infix algebra parser.
http://www.math.wpi.edu/IQP/BVCalcHist/calc5.html might be a good start

Posted: Wed Oct 22, 2003 11:54 pm
by DuFF
Google calculator also has some easter eggs. Check this out :P

Posted: Thu Oct 23, 2003 12:05 am
by Gen-ik
2 + 2 * 3 should be seen as (2 + 2) * 3 not, as Google puts it, 2 + (2 * 3)

From a calculators point of you if you type in 2 + 2 * 3 it will give you 12... which is (2 + 2) * 3....... so Google ain't that smart :wink:

Posted: Thu Oct 23, 2003 12:14 am
by volka
From a calculators point of you if you type in 2 + 2 * 3 it will give you 12
not mine, not even the windows calculator
multiplication has a higher precedence than addition.

Posted: Thu Oct 23, 2003 3:21 am
by twigletmac
Gen-ik wrote:2 + 2 * 3 should be seen as (2 + 2) * 3 not, as Google puts it, 2 + (2 * 3)

From a calculators point of you if you type in 2 + 2 * 3 it will give you 12... which is (2 + 2) * 3....... so Google ain't that smart :wink:
Have to disagree totally, as volka says multiplication and division should always happen before addition and subtraction. Mathematically the Google calculator is therefore correct in what it is doing.

Mac

Posted: Thu Oct 23, 2003 10:10 am
by DuFF
PEMDAS man! :P

Order of operations:
Parentheses
Exponents
Multiplication/Division
Addition/Subtraction

Posted: Thu Oct 23, 2003 10:34 am
by devork
calculations has to be with precedence
PEMDAS

Posted: Thu Oct 23, 2003 10:38 am
by nigma
Thanks for the help guys, apreciate quick and accurate responses!

OAN: Genik, How long has it been since you went through middle school math ;) All said with sarcasm and a good heart in mind.

Posted: Thu Oct 23, 2003 12:48 pm
by Gen-ik
nigma wrote:Thanks for the help guys, apreciate quick and accurate responses!

OAN: Genik, How long has it been since you went through middle school math ;) All said with sarcasm and a good heart in mind.
Too long by the sounds of it :) Anyway my math can't be that bad if I can program video games ;)

Posted: Thu Oct 23, 2003 4:01 pm
by Cruzado_Mainfrm
i know u guys have said this but i can't miss it :D
it's PEMDAS

2 + 2 * 3 = 8 NOT 12

Posted: Thu Oct 23, 2003 6:16 pm
by m3mn0n
Google wrote:answer to life, the universe and everything = 42
lol!

Posted: Thu Oct 23, 2003 6:40 pm
by evilmonkey
or...

BEDMAS

Brackets
Exponents
Division/multiplication
Addition/subtration

I remeber my gr 7 (long time ago, but still) teacher used to follow this to a "T". He thought that in any given mathematical equation, division must be done before multiplication and addition before subtraction. So to him 9*2/3 is 9(2/3) rather than (9*2)/3.