11-01-2019 12:37 AM
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.
11-01-2019 01:05 AM
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...
11-01-2019 02:25 AM
11-01-2019 02:47 AM - edited 11-01-2019 02:47 AM
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.
11-01-2019 10:26 AM - edited 11-01-2019 12:44 PM
Yes, the behavior is correct and can be explained by the this more explicit (but equivalent!) code for AND and OR array elements.
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.
11-01-2019 10:42 AM
@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.
11-01-2019 12:31 PM
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
11-01-2019 08:39 PM
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.