LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Upper Limit In Range and Coerce

I found a bug with the In Range and Coerce function, and I tried searching but I didn't see if anyone posted the issue before. Anyways, the Lower Limit is inclusive for the In Range indicator and the Upper Limit is not:

 

Using U32, set the Upper limit to 5 and lower limit to 1. When x = 1, In Range is True, and when x = 5 In Range is False.

 

Took a little bit of time to figure out why my code wasn't working... but just fyi if no one is aware.

0 Kudos
Message 1 of 14
(5,317 Views)

Right Click on the Coerce function and Include Upper Limit.

Message 2 of 14
(5,315 Views)

O, crazy. So, that's what those diamond icons mean. I was just looking for the limits to be treated consistently... why aren't they the same by default?

Message 3 of 14
(5,310 Views)

Probably so if you could wire several in parallel to get different ranges.  So 0 to 1,  1 to 2, 2 to 3.  You wouldn't want the value such as 1 to yield a true for two different ranges.  So it gives a <1 for the upper limit of a smaller range, and >= 1 for the lower limit of the next higher range.

Message 4 of 14
(5,299 Views)

That's kind of a unique situation... I was thinking more likely to see if an array index is valid. In my mind inclusive is intuitive, but I guess I wouldn't have found out about the setting if it was intuitive.

0 Kudos
Message 5 of 14
(5,294 Views)

Actually I would argue that the non-inclusive is perfect for checking an array index:

 

CoerceExample.png

Message 6 of 14
(5,285 Views)

It would be great if the coerced value were always in range.  The code above returns a coerced value of the array size, not array size - 1.

0 Kudos
Message 7 of 14
(4,707 Views)

@mbb25 wrote:

It would be great if the coerced value were always in range.  The code above returns a coerced value of the array size, not array size - 1.


You are replying into a 5 year old thread.  Why would you expect it to return array size - 1?

0 Kudos
Message 8 of 14
(4,698 Views)

@RavensFan wrote:

@mbb25 wrote:

It would be great if the coerced value were always in range.  The code above returns a coerced value of the array size, not array size - 1.


You are replying into a 5 year old thread.  Why would you expect it to return array size - 1?


It is still a valid question. If the upper limit is not included, why would it output a coerced value that is out of range? While maybe a bit unexpected, the current behavior is fully documented in the help:

 

"The function always coerces x to the exact values of upper limit or lower limit, regardless of whether you select Include upper limit or Include lower limit from the shortcut menu."

 

So you simply need to work around it, e.g. using the boolean output to switch. (It gets even messier when dealing with fractional DBL values, where boundaries can be a bit blurry due to limitations of the representation.)

 

 

Message 9 of 14
(4,687 Views)

That kind of makes sense.  And the highest integer that it could be coerced to and still be In Range would be the non-included upper limit minus 1.

 

But if that was a double precision number going in there, the what would be the expected highest double to output and still be in range?  Upper limit minus machine epsilon?

 

I think the current behavior of that In Range and Coerce is perfectly logical and the fact the behavior is documented is fine.

 

In Range and Coerce is kind of an odd function anyway in that it has two outputs *Coerced number" and "In Range", but there are many instances, (and probably most of the time for me), that you only use it for one of those purposes and not the other.

0 Kudos
Message 10 of 14
(4,673 Views)