B4J Library [B4X] Eval (expressions evaluator)

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

Example:
B4X:
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 List) As 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.

Updates

V2.01 - Fixes an issue with sub-expressions converted to scientific notation (which is not supported).
V2.00 - adds support for custom functions.
 

Attachments

  • B4XEval.zip
    2.9 KB · Views: 826
Last edited:

andredamen

Active Member
Licensed User
Longtime User
The attached class allows you to evaluate mathematical expressions. Note that it is less powerful than the ExpressionEvaluator library: https://www.b4x.com/android/forum/threads/expressionevaluator-library.7724/#content
The advantage of this class is that you can use it with B4A, B4J and B4i and that you can modify the source code if needed.

The source code itself is interesting. It first parses the string into numbers, operators and parenthesis. It also adds parenthesis at this point based on the operators precedences.

The next step is to recursively build a tree. The last step which is the simplest one is to recursively go over the tree and evaluate it.

Currently it only supports the four basic operators (-+/*).

B4X:
Log(e.Eval("1.321/-2/-3.123 + (2 * 2 + 3) + 4"))

Consider it a beta version.

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

ilan

Expert
Licensed User
Longtime 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"??
 

ilan

Expert
Licensed User
Longtime User
You need to use a single Try / Catch block:

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?
 

imbault

Well-Known Member
Licensed User
Longtime User
The attached class allows you to evaluate mathematical expressions. Note that it is less powerful than the ExpressionEvaluator library: https://www.b4x.com/android/forum/threads/expressionevaluator-library.7724/#content
The advantage of this class is that you can use it with B4A, B4J and B4i and that you can modify the source code if needed.

The source code itself is interesting. It first parses the string into numbers, operators and parenthesis. It also adds parenthesis at this point based on the operators precedences.

The next step is to recursively build a tree. The last step which is the simplest one is to recursively go over the tree and evaluate it.

Currently it only supports the four basic operators (-+/*).

B4X:
Log(e.Eval("1.321/-2/-3.123 + (2 * 2 + 3) + 4"))

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
 

imbault

Well-Known Member
Licensed User
Longtime 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
 
Top