From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to campare number within a range?

Hi, what I want to do is take a number, lets say 1.5 and campare itself to the range from 1.4-1.6, if it is within this range, then it'll return true, else false.

 

An easy way to do this is to have a case structure with a case selector of integers. However, the case structure can only take whole numbers (in my knowledge, such as from 1 to 3), so i cant do something like go into case 1.4 to 1.6 if the number passing into the case is within that range.

 

I'm sure there's an easy solution but I haven't thought of it, any help would be greatly appreciated.

 

 

Thank you.

 

0 Kudos
Message 1 of 11
(4,823 Views)

The easiest way is to use the In Range and Coerce VI (Comparison palette).

 

You can also set case structures to ranges like this:  1.5 .. 2.0 (case selected when input is 1.5 < x < 2.0)

LabVIEW Pro Dev & Measurement Studio Pro (VS Pro) 2019 - Unfortunately now moving back to C#, .NET, Python due to forced change to subscription model by NI. 8^{
Message 2 of 11
(4,821 Views)

@NIquist wrote:

 

You can also set case structures to ranges like this:  1.5 .. 2.0 (case selected when input is 1.5 < x < 2.0)


That only works with integers, a case structure converts floating point numbers to I32.

 

0 Kudos
Message 3 of 11
(4,816 Views)

Oh Yeah, you're right.  I guess floats would be tricky to use to select cases due to rounding issues.  It would be a nice feature if it could be logically supported though.

LabVIEW Pro Dev & Measurement Studio Pro (VS Pro) 2019 - Unfortunately now moving back to C#, .NET, Python due to forced change to subscription model by NI. 8^{
0 Kudos
Message 4 of 11
(4,808 Views)

Well Niquist is half right- use inrange and coerce since Case structures only work on integer data types (bools, strings, U32 etc) and dont work with Floats.  (Would be nice sometimes but, thats what IR&C is for).  Typing floating point litterals in the case selector tells the case to expect strings and breaks the vi unless a string is wired.  

untitled.PNG

Scanning a float to string doesn't help much either since the "." char value (46) is less than every number value (48-57) and "+" is less than "-"-

 

 


"Should be" isn't "Is" -Jay
0 Kudos
Message 5 of 11
(4,804 Views)

You can also simply multiply the float by 10 (if you're just at one decimal point precision) and then use integer values. Just be sure to use the numeric conversion functions to get an integer so you can wire it to the case structure.

0 Kudos
Message 6 of 11
(4,797 Views)

In case you were not aware, IR&C can be adapted to check multiple ranges as well.

 

CheckRanges.png

 

Notice that the Radix of the Case structure is set to Binary.  I use this when the ranges do not overlap.  Otherwise you can handle the Boolean array element by element.

Message 7 of 11
(4,790 Views)

Darrin- That belongs in the micro-nugget thread.  Criminey, Neither the help nor the shipping example hint at the ability to compare scalars to arrays of paired limits.  Where did you discover that?


"Should be" isn't "Is" -Jay
0 Kudos
Message 8 of 11
(4,776 Views)

Thanks for the replies... The multiply by 10 techique works to an extent. When you're sending in 13.6 into the case, it'll round up and go into case 14-16 instead of the default case (only two cases) which doesn't work for me.

 

I thought of a solution though, what I did was compare the number (1.5) to 1.4 using greater than function and again comparing 1.5 to 1.6 using less than function THEN anding the results which gets sent to a boolean case. So far, this solves what I'm looking for.

 

Thanks again.

0 Kudos
Message 9 of 11
(4,771 Views)

@BurningH34t : You have described the longhand (borderline RG) version of the In Range? boolean output of the In Range and Coerce function.  Which leads to Jeff's question.  If you write out this longhand version of IR&C, it is easy to understand why it works as expected with arrays.

 

CheckRangesLonghand.png

 

Simply change the comparisons to include either or both limits. This is what is "under the hood" of IR&C apparently.  So of course my "discovery" was a result of careful consideration and in no way involved simply wiring things up to see what would happen...Smiley Surprised

0 Kudos
Message 10 of 11
(4,763 Views)