Evaluate a String as a Mathematical Expression

I have a table storing many different kinds of test results. Each test result is of a specific type of test, and each type of test has an equation associated with it to create the actual data we desire. So we’re storing some raw value that the operator generates through testing, say 10, and then there is an equation this raw value must be run through, say (x + 3) / 10 to generate the actually desired test value, in this case 1.3. Of course in reality there are some powers and things and the equations are more complicated, but I have everything worked out to where I can substitute values into the equation and generate a string that evaluates as an expression if I were to simply type it into the expression window. That is, the syntax is correct. Is there any way to evaluate a string as a mathematical function?

hello @Ethan, thanks for posting!!

just to confirm I’m understanding this correctly, would you like to store the calculated result into a Table Field?? if so, an expression like this would work well:

which would then store the result into the Table:

did I understand your question correctly??

finally, this support article: Technical Details of the Expression Editor | Tulip Help Center - Support for Building Manufacturing Apps has many useful references on how to to use various functions available.

Thanks for the reply Gio, but I’m looking at sort of the next level above that.

Let’s say I have a table of Tests, where each test has an associated equation. The user is going to take some measurement that will be input into the raw value field on the table. I would like Tulip to be able to automatically take the equation string, sub in the raw value for x, and then fill in the calculated value field.

In the trigger below, I’d like to evaluate the “substitutedEquation” variable as a mathematical expression, after performing all of the string operations on it. Then store that in the table record.

Hey Ethan!

I tested this a bit myself and I see the struggle with executing an expression that’s stored as text in a variable. I thought this may be possible with some TEXTTONUMBER() wrappers to convert, but even then I hit some roadblocks. Just out of curiosity, how many different equations do you think you’d be using? I’m wondering if it’s realistic to just label the equation in the table, and use conditional logic in the app to determine which expression to evaluate.

Thanks!
Grant

1 Like

Hey Grant, I have 5 equations right now, so it is certainly possible to use conditional logic, and it looks like I’ll just implement it like that for now. We’ll definitely be adding more equations in the future and I try to avoid using conditional logic too much because of the lack of nested if statements in Tulip, so keep me posted if this ever becomes a feature!

1 Like

Python has a function name eval is able to meet Ethan’s need.
but Tulip does not have the similar function.

1 Like

thanks for sharing @mcc6025, if you’d like feel free to add this to Product suggestions - Tulip Community!!

thanks

Thanks mcc, good to know there is a functional equivalent in the greater programming world!

1 Like