05-16-2016 08:48 AM
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.
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.
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;
Solved! Go to Solution.
05-16-2016 08:54 AM
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
05-16-2016 09:05 AM
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!
05-16-2016 09:24 AM
What Johnsold mentioned, LabVIEW has very compact solutions for many common tasks
05-16-2016 02:34 PM
Thank you for the input. I did have the "select" example in the code but it good to see other examples as well.
05-17-2016 02:05 AM
What's wrong about the Sign function which does exactly what you tried to implement?
05-17-2016 05:32 AM
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.
05-17-2016 05:44 AM
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…
05-17-2016 12:21 PM
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.
05-17-2016 12:44 PM
The sign function is on the numeric palette.