LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Comparing NaN

I need to compare two big arrays of double which may contain items set to not-a-number (NaN).
When both arrays contain NaN items, the comparison should yield "equal" if all NaN elements are placed at the same index position and, of course, if all numeric items (elements that are not NaN) are equal.
Unfortunately this is not the standard behavior: if you compare two NaNs, LabVIEW yields "different" with the standard operator.
I have tried "flattening" to string both arrays before the comparison in order to achieve the expected behaviour. However in this case the performance is very poor (350ms to compare 1MB of data).
Finally I had to implement an external DLL function that merely yields the result of the "memcmp" C function. In this way comparing 1M
b of data requires at the most 100ms.
Isn't there a more elegant way in LabVIEW to obtain the same result?
0 Kudos
Message 1 of 5
(3,244 Views)
Here is the comparison opperation that you desire:

((x == y) OR ((x == NaN) AND (y == NaN))) - where OR and AND are element-wise operations.

I have attached a LabVIEW implementation.

Good luck,

Jim
Message 2 of 5
(3,244 Views)
> I need to compare two big arrays of double which may contain items set
> to not-a-number (NaN).
> When both arrays contain NaN items, the comparison should yield
> "equal" if all NaN elements are placed at the same index position and,

The equals node in LV act the way it does because that is the way IEEE
defines it. The string comparison is a good idea, but most of the time
is being spent converting the data into a string and possibly swapping
bytes.

Another approach, that will probably be faster is to do a simple
comparison first, but when it returns false, look at the elements that
are FALSE and see if both are NaNs. If there aren't that many NaNs, it
should go pretty quick. To get the more detailed equals data, popup on
the equals node and turn
on Compare Aggregates. This will now return an
array of Booleans instead of a single one. You can either loop through
or search for FALSE. The index returned can be used to index the input
elements and see if they are both NaN using the IsNan function.

Of course if you want to use a DLL and C code to do the comparison, that
will work too and will probably be even faster.

Greg McKaskle
0 Kudos
Message 3 of 5
(3,244 Views)
Greg,

> To get the more detailed equals data, popup on
> the equals node and turn on Compare Aggregates.
> This will now return an array of Booleans instead
> of a single one.

Actually it's the other way around:

Compare Elements >>> Array Output
Compare Aggregates >>> Scalar Output (Default Setting)

Yeah I know... it's late in TX, right now 😉

Cheers,

-Jim
0 Kudos
Message 4 of 5
(3,244 Views)
Thank you for your answer. However I think I will use my DLL function since it is the fastest possible way to perform the comparison.
However I think it is not a bad idea if a new version of LabVIEW implements a function that yields the size in bytes of the data that flows through a wire and another function that performs the C-like "memcmp" operation of two data buffers.
0 Kudos
Message 5 of 5
(3,244 Views)