LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Run the "And Array Element.vi" don't get expect result

For the And Array Elements.vi, if the input is a empty array, the and array results is TRUE.It takes a long time to find this bug in my program.

0 Kudos
Message 1 of 8
(3,057 Views)

The help states that an empty array will return True. See more info on the reasons why in this thread: https://forums.ni.com/t5/LabVIEW/Why-AND-Array-function-gives-true-for-empty-array-elements/m-p/5191...

 




Certified LabVIEW Architect
Unless otherwise stated, all code snippets and examples provided
by me are "as is", and are free to use and modify without attribution.
Message 2 of 8
(3,042 Views)

Adding to Mike's reply, Refer this link too.

 

-Rahul

Hit KUDOS for Thanks

Message 3 of 8
(3,017 Views)

This recently caught me by surprise too - fortunately, it was while I was writing a test using VI Tester to check the behaviour of my VI that I found out it was wrong (because I didn't handle the empty case).

Checking for "AND NOT" Empty Array is one option. Compound Arithmetic and an Inverted input can reduce the BD space required.

Example_VI.png


GCentral
Message 4 of 8
(3,013 Views)

Yes, the behavior is correct and can be explained by the this more explicit (but equivalent!) code for AND and OR array elements.

 

EmptyAND.png

 

If the array is empty, the FOR loop is skipped and the shift register value is returned unchanged.

(Of course in reality the "OR array elements" version can terminate early at the first TRUE, etc. not shown)

 

If an empty array input is a possibility, you need to program around it, e.g. as cbutcher already showed above. Often you can make changes upstream that this cannot happen.

 

Message 5 of 8
(2,950 Views)

@altenbach wrote:

(Of course in reality the "OR array elements" version can terminate early at the first TRUE, not shown)


The AND Array Elements can also terminate early at the first FALSE.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
Message 6 of 8
(2,936 Views)

And on a related note, the SUM array elements and PRODUCT OF array elements show analogous behavior.  (My understanding / interpretation found here).

 

The SUM of the elements in an empty array is 0.  Fine, no problem.

 

But the PRODUCT of the elements in an empty array is 1!  The reasoning is analogous to that for Boolean arrays.

 

 

-Kevin P

ALERT! LabVIEW's subscription-only policy came to an end (finally!). Unfortunately, pricing favors the captured and committed over new adopters -- so tread carefully.
Message 7 of 8
(2,917 Views)

It looks like this thread has pretty thoroughly handled the specific question at hand here.

 

Though, it's worth providing advice on how to handle this as you come across it in your programming career.  There's a corner case for this function.  Whether the function returned true or false for the empty array, it's pretty apparent your code isn't designed to handle an empty array.  There's a bug there regardless.

 

I could see reasonable arguments for expecting either a true or a false from that case.  I'd expect to find the desired result in the help documentation (in this case, that happens).  If it wasn't, there isn't really a way for me to say what the "expected" behavior is.  But, it's generally best to not call it unexpected without having a help documentation say it should do one thing and showing it does another.  Unexpected behavior in programming is when the thing does something it shouldn't.  It's not when it does something other than what you were hoping.

Message 8 of 8
(2,888 Views)