need a simple math evaluator

Started by Martin Francom, January 25, 2010, 05:14:21 PM

Previous topic - Next topic

Martin Francom

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?

Eddy Van Esch

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
Eddy

Martin Francom

Wow.... perfect timing... Thanks I am sure that will help.

Jean-pierre Leroy

#3
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

Martin Francom

#4
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 ?

Jean-pierre Leroy

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

Rolf Brandt

Rolf Brandt
http://www.rbsoft.eu
http://www.taxifreeware.com
I cook with wine, sometimes I even add it to the food.
(W. C. Fields)

Eddy Van Esch

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
Eddy

Martin Francom

#8
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.

Eddy Van Esch

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!
Eddy

Jean-pierre Leroy

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

Martin Francom

Jean-Pierre,
   Do you have a sugestion how I could check for (and prevent) division by zero?