SitePoint Sponsor 

User Tag List
Results 1 to 7 of 7

Aug 5, 2007, 14:18 #1
 Join Date
 Dec 2006
 Posts
 105
 Mentioned
 0 Post(s)
 Tagged
 0 Thread(s)
Control calculator expression logic
Hello I am building a PHP scientific calculator, users can perform calculation by just typing like normal mathematical expressions, e.g "(2+34^6)/33.3* 4!" so the result will be calculated accordingly, but I have a problem that, when malicious users try to put a mathematical expressions that is very long enough and takes a very heavy load of processing which might make my web server down. Such as
"9999999999999999999999999999999999999999999999999999999 * 999999999999999999999999999999"
So I thought of limiting the expressions that users can input up to 50 characters long, but this is still not a good idea, because heavy calculations does not necessarily depends on the length of the expressions, such as
//This does not make my computer load heavily
"1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1"
//But this one takes quite long and consume high processing power.
"999999999^99999"
So I want to know, is there a logic that can actually determines the result which might consume high processing which can solve my problem ? or is there a good scientific calculator library that you might want to share ? Thanks a lot.

Aug 5, 2007, 17:05 #2
 Join Date
 Dec 2005
 Posts
 262
 Mentioned
 0 Post(s)
 Tagged
 0 Thread(s)
My first suggestion would be to do this in Javascript.
You might want to try splitting the inputs for resource heavy signs (*, /, ^, % and then comparing the number on either side, and if the numbers a certain length you can either trim them with 0's or just return an error message.
For example:
"9999999999999999999999999999999999999999999999999999999 * 999999999999999999999999999999"
will return the first few digits: 9999999*999999 then just show the rest as an E character.

Aug 6, 2007, 00:44 #3
 Join Date
 Dec 2006
 Posts
 105
 Mentioned
 0 Post(s)
 Tagged
 0 Thread(s)
Thank for the reply, what method can I use to compare the number of either sides that might cause heavy calculations ? I have done the following but still not solving.
Let's say 999^999 will make my apache server hang, let the base and the power be x and y respectively, x ^ y
So if I limit the x and y to be 900 respectively, 900^900. so the the following should be OK
800^400
700^900
2^900
since x or y are equal or less than 900
But problem is this line below should be allowed too, because the result still less than 900^900 and does not make heavy load.
2^1000
But since we limit the y to be 900, this will not be calculated, which is a bad idea too. I so eager to solve this, but so far have not get a good solutions, but I believe there is a logic like Google's. Thanks.

Aug 6, 2007, 01:02 #4
 Join Date
 Apr 2004
 Location
 germany
 Posts
 4,324
 Mentioned
 0 Post(s)
 Tagged
 0 Thread(s)
I'm curious how the calculations are actually done. Are you using eval() ?

Aug 6, 2007, 01:42 #5
 Join Date
 Dec 2006
 Posts
 105
 Mentioned
 0 Post(s)
 Tagged
 0 Thread(s)
Thanks, I am using BCMath functions. http://us.php.net/manual/en/ref.bc.php

Aug 6, 2007, 01:58 #6
 Join Date
 Apr 2004
 Location
 germany
 Posts
 4,324
 Mentioned
 0 Post(s)
 Tagged
 0 Thread(s)
Yes, but how do you parse "(2+34^6)/33.3* 4!" ?

Aug 6, 2007, 02:11 #7
 Join Date
 Dec 2006
 Posts
 105
 Mentioned
 0 Post(s)
 Tagged
 0 Thread(s)
Look at, shunting yard algorithm, it's very interesting. That's how mathematical expressions can be calculated. http://en.wikipedia.org/wiki/Shunting_yard_algorithm
Bookmarks