SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Zealot
    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+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 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.

  2. #2
    SitePoint Addict
    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 (*, /, ^, &#37 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.

  3. #3
    SitePoint Zealot
    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.

  4. #4
    SitePoint Wizard stereofrog's Avatar
    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() ?

  5. #5
    SitePoint Zealot
    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

  6. #6
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, but how do you parse "(2+3-4^6)/33.3* 4!" ?

  7. #7
    SitePoint Zealot
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •