LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

If I have floating values such as 6.3, 6.7, 6.9, 7.1, 7.2 how do I write a case statement to handle that

Solved!
Go to solution

How do I write a case statement If I want a case for x < 1.5;   a case for 1.5 <= x <= 3.7;  case for  3.7 < x < 7.2.....etc.   My input is a floating number.

 

Thank you.

0 Kudos
Message 1 of 13
(3,273 Views)

If it's a single decimal point you could multiply the value by 10 and convert to integer.

 

Or, you could create an array of Booleans from each of your comparisons and find the first True. Case structure would be driven by the index.

 

Or, you could create a bit-weighted value based on each of the comparisons. The value is used to drive the case structure.

 

Take your pick.

Message 2 of 13
(3,263 Views)

See attached. It seems to work. Is this what you meant? I'll try and figure out your other suggestions.

 

Thank you.

0 Kudos
Message 3 of 13
(3,256 Views)
Solution
Accepted by topic author chuck72352

You don't seem to have set rules on inclusiveness, but I often utilize the In Range and Coerce function in these cases.

 

FloatingCaseExample.png

Message 4 of 13
(3,248 Views)

Thank you!

0 Kudos
Message 5 of 13
(3,244 Views)

You get simpler code using a single sorted 1D array of thresholds and "threshold array".

 

See this old post for details.

 

 

 

Note: for some reason, it does not work with the -inf (why not???), so use a reasonable real lower limit instead, e.g. -100000.

There are probably other workarounds.

 

 

Message 6 of 13
(3,229 Views)

Nice method with the Threshold function. I was not aware of the limitation with -Inf. Odd.

 

But it seems to me that the simplest is to simply multiply the number by 10 and set the ranges in the case structure based on the integer values.

Message 7 of 13
(3,218 Views)

Yes, its a more generic method, especially if the boundaries are not divisible by 10. 😉 I was comparing it to Darin's method, which requires a set of two matched array diagram constants, which can lead to more accidental errors.

 

I will write up an idea that threshold array should be able to deal with Infs correctly. As you will see, there are a few more worms in that particular can. 😉 Stay tuned.

Message 8 of 13
(3,207 Views)

I had long abandoned the threshold method not because of the -inf problem (I seem to remember getting NaN), but because the IR&C gives me control over inclusiveness and handles NaN quite gracefully (I use that as a sentinel value many times).  The times 10 method also chokes on NaN.

 

For those error prone individuals out there.  Smiley Wink

 

FloatingCaseExample_v2.png

 

 

 

Message 9 of 13
(3,196 Views)

@smercurio_fc wrote:

Nice method with the Threshold function. I was not aware of the limitation with -Inf. Odd.



Actually, my code operates correctly as long as the first element is smaller than all other elements in the array. We don't need any special handling.

 

Maybe NaN is not a bug if the array starts with -Inf, because the interpolated index for any number between the second element and -inf will be infinitely close to 1, thus a result of zero can never be obtained (try a first element of -1e50 and you'll always get 1 unless you go to very huge negative numbers).

 

The way threshold array is defined, the behavior should be obvious, the problem is assigning a fractional index.

 

It is unexpected that an input equal to the second element also results in NaN. That might be a bug. (see image).

 

 

I probably won't post an idea, maybe a bug report after some more thinking... 😉

0 Kudos
Message 10 of 13
(3,182 Views)