LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Formula Node - Embedded If statements

Greetings,
I would like to "embed" an if statement within an if statement using a formula node. The structure is as follows:
 
if(x == y/2)
{
 y = 0;
 if(a < c && a > d)
{
  a = a - 1;
}
}
 
For some reason when I run my code and both of the criteria for my if statments are true the formula node does not execute the a = a - 1.
 
For example:
a = -0.5
c = 0
d = -1
 
Then 'a' should decrement to -1.5. However, when I run my code 'y' will reset to zero but 'a' will not decrement.
 
Am I allowed to embed if statements within if statement using a formula node.
 
Also, I am aware that I can accomplish this using other means but I would really prefer to use the formula node.
 
Any help is greatly appreciated.
 
Thanks.
 
 
0 Kudos
Message 1 of 17
(10,268 Views)
Please post a simple VI that demonstrates this problem.  Maybe the problem isn't in the formula node but the way it's wired up in your VI.
0 Kudos
Message 2 of 17
(10,261 Views)
Hi rchaoua,

what about using LV primitives?
(In the hidden case y and a are just wired trough...)
Be aware of (big) problems rising from testing floats for equality!!!


Message Edited by GerdW on 03-16-2008 10:59 AM
Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 3 of 17
(10,254 Views)
Thanks for the replies. I prefer not to use the LV primitives. The VI itself is wired correctly and everything works except the embedded if statement (even when the criteria for the embedded if statement is correct).

Restating my original question; can you embed an if statement within an if statement using a formula node?

Thanks.
0 Kudos
Message 4 of 17
(10,248 Views)
Yes, you can. I created a formula node just like you specified and it works. If yours is not working, then post your code as already mentioned.
0 Kudos
Message 5 of 17
(10,243 Views)
Threre is no problem with the formula as posted, you get -1.5 at the "a output" as expected.
 
Of course if you want the original "a input" control to be modified with the new value, you would need to write the "a output" to it via a local variable.

Still, I agree with GerdW that you are opening a can of worms here comparing DBLs for equality. This can very easily lead to unexpected results!
 


Message Edited by altenbach on 03-16-2008 05:03 PM
Download All
0 Kudos
Message 6 of 17
(10,226 Views)

Why is comparing DBL's for equality such a problem? What sort of unexpected results are we talking about?

How can I get around comparing the DBL's? The doubles in the second if statement are real world values, if I change the "type" from DBl would that fix the problem? If so, how do I do that?

Thanks!

0 Kudos
Message 7 of 17
(10,206 Views)

See this article regarding the problem with DBLs.  In particular read the section on accuracy problems.

http://en.wikipedia.org/wiki/Floating_point_numbers

Jim

LV 2020
0 Kudos
Message 8 of 17
(10,203 Views)
Instead of comparing for equality, you can either use Greater Than Or Equal To etc or you can use the In Range And Coerce with narrow boundaries
0 Kudos
Message 9 of 17
(10,200 Views)

Thanks for the link. I have read through it. Could the reason that my second if statement is never executed be due to the fact that I am comparing DBL's?

The wierd thing is I use the saem structure of code elsewhere in my program (the 2nd if statement) and I have no problems.

What sory of work arounds are there?

0 Kudos
Message 10 of 17
(10,199 Views)