LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Formula Node code wrong?

Solved!
Go to solution

Hi guys, I know this is a very simple problem but it left me scratching my head wondering what's wrong. The code is displayed as below:

warning.png

The code should call in certain values at different conditions but doesn't seem to work the way as desired, I am not certain what the problem is. Thank you all ahead of time for the help!

0 Kudos
Message 1 of 13
(5,860 Views)

What happens if your input value is 100.000000000001, or  -0.00000000000000000001?

 

You work with floating point values and computers can't represent those values with indefinite precision as that would require an indefinite amount of memory for a single floating point value. Then you go and calculate something with those less than perfectly precise numbers and the small errors add up during your calculation. What may look like a clear 100.0000000000000000000000000 will end up to be something like 100.000000000000002!

 

Besides the equal comparison in the LabVIEW formula node is a double equal sign ==, the single equal sign you used is an assignment. So after the first comparison if the value is between 0 and 100 non inclusive you assign 100 to x and that is always true since the result of that statement is the value of x after the assignment which you just set to 100. And anything not 0 is considered true in the formule node when a boolean result is expected.

 

Then you assign 0 to x and that is of course always false! So you could replace the code in the formula node currently with a constant number 1.

 

Basically you should also use the else and make it something like this instead:

 

if (x < 100 && x > 0)
   y = 0;
else if (x >= 100)
  y = 1;
else if (x <= 0)
  y = 2;

This still has the problem that your result from previous calculations could be something like 99.999999999996754 and is therefore technically 100 but your computer comparison will categorize it into the class y = 0.

Rolf Kalbermatter  My Blog
DEMO, Electronic and Mechanical Support department, room 36.LB00.390
Message 2 of 13
(5,849 Views)

Ofcourse that code would be very easy to do in pure G, but i assume you're just trying out the formula node.

/Y

G# - Award winning reference based OOP for LV, for free! - Qestit VIPM GitHub

Qestit Systems
Certified-LabVIEW-Developer
Message 3 of 13
(5,840 Views)

"=" refers to assignment operator, use "==" for comparison.

bp
Message 4 of 13
(5,833 Views)

Ah, classic C-mistake, and as long as the variable is able to be set, it's true. 😄

/Y

G# - Award winning reference based OOP for LV, for free! - Qestit VIPM GitHub

Qestit Systems
Certified-LabVIEW-Developer
0 Kudos
Message 5 of 13
(5,825 Views)

Additionally, The output of your Formula Node is a float that then gets passed into a Case Statement.  Seeing how you are only assigning y the value of integers, why not make the output y an I32?  That way the case statement can handle it better and doesn't need to coerce the input value.

Help the Community (and future reviewers) by marking posts as follows:
If it helped - KUDOS
If it answers the issue - SOLUTION
0 Kudos
Message 6 of 13
(5,777 Views)

I might be missing something, as I never use the formula node. But my quick experiments this morning when formulating my answer didn't show an option to change the data type of a formula node terminal.

Rolf Kalbermatter  My Blog
DEMO, Electronic and Mechanical Support department, room 36.LB00.390
0 Kudos
Message 7 of 13
(5,759 Views)

Just declare

int y;

in the formula body

Paolo
-------------------
LV 7.1, 2011, 2017, 2019, 2021
Message 8 of 13
(5,754 Views)

@pincpanter wrote:

Just declare

int y;

in the formula body


Cool! thanks a lot

Rolf Kalbermatter  My Blog
DEMO, Electronic and Mechanical Support department, room 36.LB00.390
0 Kudos
Message 9 of 13
(5,749 Views)

From the watermark, you are using the student and home edition of LabVIEW, so presumably you want to learn something. As Yamaeda already mentioned, all this would be trivial to do without the formula node and only using graphical code. While it is commendable that you want to try out the formula node, it is not somethings that should be done here unless the teacher forces you to do so. As an exercise, I would strongly recommend to also code a graphical version for comparison.

 

While we are at it, the abort function ("stop") has virtually no business in any serious program, so try to find a solution that does not use it. I am sure there is one!

 

Can you show us the rest of the program? What is it supposed to do and solve? How does the user interact with it? Where does x come from?

Message 10 of 13
(5,734 Views)