B4J Library [B4X] Eval (expressions evaluator)

Discussion in 'B4J Libraries & Classes' started by Erel, Jun 7, 2015.

  1. Erel

    Erel Administrator Staff Member Licensed User

    The attached class allows you to evaluate mathematical expressions with support for custom functions.
    It is compatible with B4A, B4J and B4i.

    Example:
    Code:
    Sub AppStart (Args() As String)
       
    Dim e As B4XEval
       e.Initialize(Me, 
    "Eval")
       
    Log(e.Eval("1 + Min(2, Max(-4, 1), 6)"))
       
    Log(e.Eval("-(2+5)*(7-3) + Sin(15 + 15) + Cos(30)"))
       
    Log("Error? " & e.Error)
       
    Log(e.Eval("-(2+5)*-(7-3)"))
       
    Log(e.Eval("-((-7-3))"))
       
    Log(1.321/-2/3.123 + (2 * 2 + 3) + 4)
       
    Log(e.Eval("1.321/-2/3.123 + (2 * 2 + 3) + 4"))
    End Sub

    'custom functions implementation
    Sub Eval_Function (Name As String, Values As ListAs Double
       
    Select Name 'it will be lower case
         Case "min"
           
    Dim d As Double = Values.Get(0)
           
    For Each n As Double In Values
             
    If n < d Then d = n
           
    Next
           
    Return d
         
    Case "max"
           
    Dim d As Double = Values.Get(0)
           
    For Each n As Double In Values
             
    If n > d Then d = n
           
    Next
           
    Return d
         
    Case "sin"
           
    Return SinD(Values.Get(0))
         
    Case Else
           
    Log("Invalid function: " & Name)
           
    Dim e As B4XEval = Sender
           e.Error = 
    True
           
    Return 0
       
    End Select
    End Sub
    Note that if you are using it with B4A then call Eval after Activity_Create completes (you can add Sleep(0) instead). Otherwise the Function event will not be raised.

    Version 2.00 is attached - adds support for custom functions.
     

    Attached Files:

    Last edited: Aug 24, 2017
    Mashiane, Pendrush, MarcoRome and 7 others like this.
  2. andredamen

    andredamen Member Licensed User

    Thanks!
     
  3. andredamen

    andredamen Member Licensed User

    In your code you use brackets. I give it a try but brackets are not supported. I that so?
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    Brackets should be supported. Which expression have you tried?
     
  5. andredamen

    andredamen Member Licensed User

    e.Initialize
    DezeSom = "(2+5)*(7-3)" ' should be 28 but gives by eval 49
    res=e.Eval(DezeSom)
    Msgbox(res, DezeSom)
     
  6. Erel

    Erel Administrator Staff Member Licensed User

    There was a bug in the way the unary minus operator was handled. I've uploaded a new version. It should fix this issue.
     
  7. andredamen

    andredamen Member Licensed User

    Where can I find this new version?
     
  8. Erel

    Erel Administrator Staff Member Licensed User

    It is attached to the first post.
     
  9. andredamen

    andredamen Member Licensed User

    Fine! It works now. Thanks.
     
  10. ilan

    ilan Expert Licensed User

    Perfect!, exactly what i need :)
     
  11. ilan

    ilan Expert Licensed User

    hi,

    this class is awesome, i have only one problem with it.

    if the string is wrong i am getting a crash.
    i try to put everything in try...catch but then i am getting an endless loop and then a crash.

    how can i stop the loop and tell to exit the request with a string "Syntax Error"??
     
  12. Erel

    Erel Administrator Staff Member Licensed User

    You need to use a single Try / Catch block:
    Code:
    Try
         
    Log(e.Eval(";',';,-(2+5)*(7-3)"))
       
    Catch
         
    Log(LastException)
       
    End Try
    Note that in debug mode you may still see the exception in the logs. However the program will continue correctly.
     
  13. ilan

    ilan Expert Licensed User

    thank you erel, actually i did it and it didn't worked (b4i). with b4j it worked. after uninstalling the app and build it again it worked also in b4i.
    i had yesterday another problem that a reinstalled solved it. it is hard to know if i have a mistake in my code or my code is not working because i
    need to uninstall and install again my app. with b4a i did not have such experience...

    do you have maybe tips when a uninstall could solve my problem and save me lot of headache with searching for a mistake in my code?
     
  14. Erel

    Erel Administrator Staff Member Licensed User

    Click on the Clean project if you think that the installed app is "corrupted".

    The only case where uninstalling an app is required if is you want to delete the existing files.
     
  15. imbault

    imbault Well-Known Member Licensed User

    Erel, I'd like to expand this class, can you just give the way to add a sample function like ABS() in your code, it would be a great beginning

    Thanks
     
  16. Erel

    Erel Administrator Staff Member Licensed User

    Adding support for functions is possible but it is not a simple change. Currently too busy with other stuff to make the required changes. Sorry.
     
  17. imbault

    imbault Well-Known Member Licensed User

    Hi, is there any chance to support functions in this class, it would be very helpfull...

    I need to evaluate math expressions in an iOS App, including functions

    Thanks
     
  18. Erel

    Erel Administrator Staff Member Licensed User

    Done. See the first post.
     
    jmon, Toky Olivier, Rantor777 and 3 others like this.
  19. Rantor777

    Rantor777 Member Licensed User

    Great!, Now iOS Support, working with B4J, should give it a try in next months to B4i.

    Thanks @Erel
     
    Toky Olivier likes this.
  20. imbault

    imbault Well-Known Member Licensed User

    Excellent, and as always, very easy to extend
    Congrats again and again @Erel !!!

    Thanks a lot
     
    Toky Olivier likes this.
Loading...