Google Calculator in PHP?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Google Calculator in PHP?

Post 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.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

you're looking for an infix algebra parser.
http://www.math.wpi.edu/IQP/BVCalcHist/calc5.html might be a good start
User avatar
DuFF
Forum Contributor
Posts: 495
Joined: Tue Jun 24, 2003 7:49 pm
Location: USA

Post by DuFF »

Google calculator also has some easter eggs. Check this out :P
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post 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:
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
User avatar
DuFF
Forum Contributor
Posts: 495
Joined: Tue Jun 24, 2003 7:49 pm
Location: USA

Post by DuFF »

PEMDAS man! :P

Order of operations:
Parentheses
Exponents
Multiplication/Division
Addition/Subtraction
User avatar
devork
Forum Contributor
Posts: 213
Joined: Fri Aug 08, 2003 6:44 am
Location: p(h) developer's network

Post by devork »

calculations has to be with precedence
PEMDAS
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post 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.
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post 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 ;)
Cruzado_Mainfrm
Forum Contributor
Posts: 346
Joined: Sun Jun 15, 2003 11:22 pm
Location: Miami, FL

Post 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
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

Google wrote:answer to life, the universe and everything = 42
lol!
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

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