LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

formula node bug?

Solved!
Go to solution

Hello,

 

   I can't figure out what I'm doing wrong with the formula node.  I tried this on both LabView 2011 and Labview 2013 version and got the same results.  Basically I want to past any numbers with a decimal to the formula node.  If the number is more negative than -1 then set the value to -1 and if the number is more positive than 1 then set the number to +1.  Anything else just return the input to the output.  

 

formula node.png

 

 

This arithmetic works correctly when I use the LabView discrete "Select" function.  In the formula node I have this code:

 

if(Xin < -1.0)
   Xout = -1;
else
   Xout = Xin;

if(Xin > 1.0)
   Xout = 1;
else
   Xout = Xin;

Where Xin is the input node and Xout is the output node. It works well for anything over +1.  If you pass 1.5, you get 1.  If you pass 0.5 you get 0.5.  It even works for negative number greater than -1.  So passing -0.5 you get -0.5 etc.   The problem shows up when passing anything more negative than -1.  If I pass -1.5, I get -1.5 as the answer.  The Labview discrete select function method gives me the correct answer as -1 when passing -1.5.

 

formula node front panel.png

 

I even tried to make sure that the input is casted to float just in case it got mixed up as an integer.  But the results are the same.  So either I don't understand the formula node syntax or there's something else going on.

 

float32 X =  Xin;
if(X < -1.0)
   Xout = -1;
else
   Xout = Xin;

if(X > 1.0)
   Xout = 1;
else
   Xout = Xin;
0 Kudos
Message 1 of 12
(4,829 Views)
Solution
Accepted by topic author patrick.chaopricha

Consider the X in= -1.5 case. 

 

The first test sets Xout to -1.

 

Then the second test runs and sets Xout to Xin = -1.5.

 

Last test wins.

 

In LabVIEW primitives using the In Range & Coerce function might simplify things a bit and it would be very clear what you were trying to do.

 

Lynn

Message 2 of 12
(4,826 Views)

Ah, that makes a lot of sense!  I should probably use (else if) for the second test.  Using primitive case, this wasn't a problem since the result of the first test is passed to the 2nd test so the Xin value is actually the Xout from the previous test.  

if(Xin < -1.0)
   Xout = -1;
else if(Xin > 1.0)
   Xout = 1;
else
   Xout = Xin;

 Or I could also do 2 formula nodes (if the test gets complicated enough).

 

Thanks!

Message 3 of 12
(4,820 Views)

What Johnsold mentioned, LabVIEW has very compact solutions for many common tasks

in range.png

Message 4 of 12
(4,799 Views)

Thank you for the input.  I did have the "select" example in the code but it good to see other examples as well.

0 Kudos
Message 5 of 12
(4,750 Views)

What's wrong about the Sign function which does exactly what you tried to implement? Smiley Very Happy

 

sign.png

Rolf Kalbermatter
My Blog
Message 6 of 12
(4,730 Views)

Hmm .. sign function would work.  I don't remember seeing it in my pallette though.  I'll have to go back and look. I was using version 2011 and 2013.  That would be the simplest way to do it.

 

I was trying to implement something to basically force the spiral radius calculation to behave correctly.  I have found other ways to do it without using a formula node now though.

0 Kudos
Message 7 of 12
(4,720 Views)

Hi Rolf,

 

the Sign() function isn't correct in this case as the requirement was:

If the number is more negative than -1 then set the value to -1 and if the number is more positive than 1 then set the number to +1.  Anything else just return the input to the output.

 

InRangeAndCoerce is suitable here…

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 8 of 12
(4,712 Views)

I also don't have the Sign function in 2011 version of LabView.  I wasn't able to check the 2013 version.  Is the output can only be -1, 0, and 1 with the Sign fuction?  If so then GerdW is correct that it doesn't meet my criteria since I wan the original number to come through if it's not greater than either signs.

 

I attach the picture from my 2011 LabView Comparison Pallette.  Maybe I can access the Sign Function in a different way?  It might be handy for other uses.

comparison.png

 

 

0 Kudos
Message 9 of 12
(4,672 Views)

The sign function is on the numeric palette.

Message 10 of 12
(4,662 Views)