PlanetSquires Forums

Support Forums => General Board => Topic started by: Martin Francom on January 25, 2010, 05:14:21 PM

Title: need a simple math evaluator
Post by: Martin Francom on January 25, 2010, 05:14:21 PM
I need a simple math evaluator  function:

I want to pass a string such as:

    "value1 Operator1 value2 Operator2 value3 operator3"

example string   "10.25 * 0.90 + 5.00"

               or      "15.45 * 30 / 120"
the values and operators will vary,  but only simple math is required.

I need this type of function:

Function MathEval (Expression as string) as Single
    ??????
    Function = val
End Function 

Anyone have something like this in your toolbox that you would like to share?
Title: Re: need a simple math evaluator
Post by: Eddy Van Esch on January 25, 2010, 05:20:46 PM
Marty,
Did you happen to notice this recent source code on the PB forums?
http://www.powerbasic.com/support/pbforums/showthread.php?t=42644

Kind regards
Title: Re: need a simple math evaluator
Post by: Martin Francom on January 25, 2010, 06:48:44 PM
Wow.... perfect timing... Thanks I am sure that will help.
Title: Re: need a simple math evaluator
Post by: Jean-pierre Leroy on January 25, 2010, 07:02:54 PM
Hi Marty,

For your information I have just updated my evaluation functions on the PowerBASIC forum; I put a simplified version called EvalExpNumOpe that can evaluate only simple numerical / mathematical expressions; these expressions could be composed only of parentheses ( ) and usual arithmetic operators, exponentiation (^), multiplication (*), floating-point division (/), integer division (\), modulo (MOD),substraction (-), and addition (+).

Feel free to ask me if you have any question.
Jean-Pierre
Title: Re: need a simple math evaluator
Post by: Martin Francom on January 25, 2010, 07:32:38 PM
Jean-Pierre,
   Very nice code!   Thanks.

I made a FF Example project  based on your code.   This should show how easy it would be to incorporate your Math Evaluator  into a FF project.

Does your Math Evaluator protect from illegal math such as division by zero ?
Title: Re: need a simple math evaluator
Post by: Jean-pierre Leroy on January 26, 2010, 04:00:18 AM
Hi Marty,

Thanks you your complements  :)

I'm sure the FF Example project that you have done could be useful for other FF users; thanks for it.

You can see also these evaluation functions in action in the GraphicControlDemo and GraphicWindowDemo http://www.planetsquires.com/protect/forum/index.php?topic=2300.0

At this moment there is no protection against division by zero (unless done by PB itself).

PS: To complete these functions, I'm working on a syntax checker in order to validate the expression before the evaluation.

Jean-Pierre
Title: Re: need a simple math evaluator
Post by: Rolf Brandt on January 26, 2010, 05:18:44 AM
Nice piece of code, Jean-Pierre.
Title: Re: need a simple math evaluator
Post by: Eddy Van Esch on January 26, 2010, 05:31:13 AM
Indeed great code, Jean-Pierre, and thanks for sharing it with us!

If I may make a suggestion to expand its functionality. Have you considered implementing the use of variables?
Like for example:
i = Eval("a = 3 * 4")
i = Eval("b = a^2")
i = Eval("2*a - b")
That would really be the icing on the cake ...  :)
Kind regards
Title: Re: need a simple math evaluator
Post by: Martin Francom on January 26, 2010, 05:41:43 PM
Quote from: Eddy Van Esch on January 26, 2010, 05:31:13 AM
Indeed great code, Jean-Pierre, and thanks for sharing it with us!

If I may make a suggestion to expand its functionality. Have you considered implementing the use of variables?


Like for example:
i = Eval("a = 3 * 4")
i = Eval("b = a^2")
i = Eval("2*a - b")
That would really be the icing on the cake ...  :)
Kind regards


you can do that sort of thing right now.

I store a "Formula"  for  calculating prices.  I store it in my database as:

          "AWP * 1.2 + 5.00"

And then when I need to evaluate the forumla I use the PB command  REPLACE  to replace the AWP with it's value before passing the  string to Jean-Pierre's  EvalExpNum function.  I works great.

This way the user can create many diferent formulas and store them in the database and when they need to be evaluated, you just select the formula that you need to use,  replace the variables with their values and run it though   EvalExpNum  to get the result.
Title: Re: need a simple math evaluator
Post by: Eddy Van Esch on January 26, 2010, 07:08:47 PM
Quote from: Marty Francom on January 26, 2010, 05:41:43 PM
you can do that sort of thing right now.
Darn good idea, Marty.  8) Hadn't thought of that myself!
Title: Re: need a simple math evaluator
Post by: Jean-pierre Leroy on January 27, 2010, 12:32:43 PM
Marty,

Thank you for pointing the right direction for Eddy.

For your information, I have already use and implemented the evaluation of one variable X in these projects GraphicControlDemo and GraphicWindowDemo http://www.planetsquires.com/protect/forum/index.php?topic=2300.0

Regarding these evaluations functions, my syntax checker is not yet ready; so in the meantime I suggest at least to check the number of parentheses before the evaluation.

Quote
    ' check the number of parentheses
    If Tally(lSymbolicFunction,"(") <> Tally(lSymbolicFunction,")") Then
        MessageBox(HWND_FORM1, "The number of open parentheses ""("" is not equal to the number of close parentheses "")"" !" & $CrLf, JPL_ProductNameAndProductVersion(), %MB_OK Or %MB_DEFBUTTON1 Or %MB_ICONWARNING Or %MB_TASKMODAL)
        Exit Function
    End If       

Jean-Pierre
Title: Re: need a simple math evaluator
Post by: Martin Francom on February 05, 2010, 12:01:06 PM
Jean-Pierre,
   Do you have a sugestion how I could check for (and prevent) division by zero?