LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How can I perform Modulus (remainder) in Evaluate Formula VIs?

Solved!
Go to solution

I need to let the user enter in a custom formula via text string.  (So Formula Node and Expression Node will not help me). The formula will then be evaluated via Eval Multi-Variable Scalar.vi.

 

The user's formula will often need the modulus operator (usually shown as % sometimes as mod(x,y), where we calculate the remainder after division of one number by another).  This help file says that the mod(x,y) is not available, but I see % in the list here.  Please tell me that modulus should be supported, and this is just a bug!

0 Kudos
Message 1 of 10
(5,301 Views)

Try it and find out.

0 Kudos
Message 2 of 10
(5,287 Views)

Hey D,

 

It doesn't look like % works as modulus in the Eval Multi-Variable Scalar VI. It appears to give you a wrong variable error.

 

Ty Prather
Technical Marketing Specialist
National Instruments
0 Kudos
Message 3 of 10
(5,246 Views)

Your title talks about Evaluate Formula VIs bt your link is about the Formula Node. That are two completely different things. The Evaluate Formule Vis are written in pure LabVIEW diagram code and parse a string to do some formula evalution. It's written somewhat chaotic and supports a certain functionality that is pretty limited but since it is in pure LabVIEW code it could be extended by you. You can checkout what it can do by driling down into the subVIs and checking out the implementation. If you had done this, you would have seen that it does not support the modulus operator out of the box, but it is possible to modify it to do so. However I have to admit that its implementation is pretty dirty, inefficient and limited.

 

The Formula Node evaluates a script by passing the entire string to a LabVIEW internal parser/evaluator engine. It supports a larger set of functionality but requires the formula/script to be known at the time of the VI editing and therefore can't be changed at runtime.

Rolf Kalbermatter
My Blog
0 Kudos
Message 4 of 10
(5,220 Views)

So it is a bug!  Can you set a CAR?

0 Kudos
Message 5 of 10
(5,196 Views)

@ Rolfk,  wow.  That's some hairy looking innards.

 

I'm surprised that there are two parsers, doing essentially the same thing.  Especially since you would expect the "Eval Formula Node.vi" to be able to evaluate what is in a Formula Node structure, and not run the less-capable "Evaluate Formula VIs" code.

 

 

0 Kudos
Message 6 of 10
(5,192 Views)

@D* wrote:

@ Rolfk,  wow.  That's some hairy looking innards.

 

I'm surprised that there are two parsers, doing essentially the same thing.  Especially since you would expect the "Eval Formula Node.vi" to be able to evaluate what is in a Formula Node structure, and not run the less-capable "Evaluate Formula VIs" code.

 

 


It's for historical reasons. Originally the Formula Node in LabVIEW was pretty limited with a syntax that was neither C like, nor anything else, no support for arrays nor many other things. Then someone developed a Math Toolkit and one part of it was the Evaluate Formula Parser library. At some point around LabVIEW 6.0 the Formula Node got a complete work over where it was implementing a mostly C like syntax with most mathematical operators like in C. The Evaluate Formula Parser was together with many other things integrated into LabVIEW from the separate Toolkit too. Of course NI could go and throw the entire Evaluate Formula Parser library completely outt of the window and reimplement it in a cleaner and more functional way BUT....

 

There are many applications that have been developed in the last 15 years or so that depend on the Evaluate Formula Parser functionality as it is now, even if it is less than perfect. Changing this library now would be causing a lot of problems for those people when they upgrade. Adding yet another formula parser to LabVIEW is also not ideal, as you have already three which come with LabVIEW if you also count the MathScript node.

 

So, yes it is not ideal as it is, but changing it wouldn't make the situation necessarily better for many existing users, so spending resources for this is not a high priority.

 

I did at some point reimplement the Evaluate Formula Parser completely with a more efficient parser and evaluator component, but the actual diagram code didn't get easier to understand and I really doubt that someone else than me could easily extend it with new functionality. Formula parsing is a pretty dirty business if you want to make the parser follow the normal user expectations of formula expression and operator precedence rather than force him into some more obscure formula expressions like strict RPN notation or similar.

Rolf Kalbermatter
My Blog
Message 7 of 10
(5,169 Views)

Just for your reference: An early version of my Expression Evaluator was posted here. It doesn't support the % modulo operator either out of the box but it would be fairly easy to hack that into the code. You would need to extend the CALC Token Type.vi to recognize this operator and create an operator code and the CALC Three Register Calculation.vi to perform the actual operation for the according operator code.

Rolf Kalbermatter
My Blog
Message 8 of 10
(5,123 Views)

I'm happy to keep along old code that preseves backward compatibility.  (e.g. look at Vision Development Module, the older versions of everything still exist, but the latest greatest tools appear for new designs).  But we shouldn't be slaves to the past. As you mentioned there are other, better formula parsers built into LabVIEW (math script node and formula node), and it would be wonderful to have an API for accessing these.  Thanks for your code.  I'll take a look, and maybe see if there is a easy opensource function eval library.

0 Kudos
Message 9 of 10
(5,060 Views)
Solution
Accepted by topic author D*

I suggest using muParser instead of trying to re-write the entire formula parsing library.

It is open source and it is pretty easy to add functions, constants, and operators if needed.

I've released a LabVIEW API for it here: https://lavag.org/files/file/295-lv-muparser/

muParserDemo2.PNG

Message 10 of 10
(4,236 Views)