Control calculator expression logic
Posted: Mon Aug 06, 2007 5:21 am
Hello I am building a PHP scientific calculator, users can perform calculation by just typing like normal mathematical expressions, e.g "(2+3-4^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 make may Apache server hang. Such as
"99999999999999 * 99999999999999 ^ 99999999999999999999999999 * 999999999999999999999999999999"
So I thought of limiting the expressions that users can input up to 30 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 even though it's long because it's a simple calculations
"1+1+2+1+2+1+1+1+1+1+3+1+3+1+1+1+1+2+1+1+1+1+3+1+1+1+2+1+1+1+1+1+1+1+1+1+1+1+1+1+2"
//But this one consume high processing power and make my Apache server hang even it's short
"999999999^99999"
I did not use eval(), but BCMath function instead. For expressions like (2+3-4^6)/33.3* 4!, I use shunting yard algorithm
So I want to know, is there a logic that can actually determines the result which might consume high processing ? Thanks a lot.
"99999999999999 * 99999999999999 ^ 99999999999999999999999999 * 999999999999999999999999999999"
So I thought of limiting the expressions that users can input up to 30 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 even though it's long because it's a simple calculations
"1+1+2+1+2+1+1+1+1+1+3+1+3+1+1+1+1+2+1+1+1+1+3+1+1+1+2+1+1+1+1+1+1+1+1+1+1+1+1+1+2"
//But this one consume high processing power and make my Apache server hang even it's short
"999999999^99999"
I did not use eval(), but BCMath function instead. For expressions like (2+3-4^6)/33.3* 4!, I use shunting yard algorithm
So I want to know, is there a logic that can actually determines the result which might consume high processing ? Thanks a lot.