Consider this code snippet. I’m comparing two constants with the same value, but they’re not equal.
This is a bug, right? What am I missing?
The NaN value here means ‘Not a Number’. And it turns out NaN is never equal to anything
even NaN
NaN is also not greater than any number, or less than any number. The only ‘true’ result you will get with NaN is not equal – it is not equal to anything and everything
This is because, as the name suggests, it’s not actually a number.
The details of NaN behaviour are covered in IEEE 754, which is the standard that describes how computers should handle floating point numbers. You can find out more on its Wikipedia page here https://en.wikipedia.org/wiki/IEEE_754
A NaN will typically be returned when an numeric operation generates a result that should be a complex number, not a real. Examples of this are taking the square root of a negative number, or its logarithm
Once you have a NaN value, it will tend to propagate through your code, because any operation that gets a NaN input will output NaN
If you need to test for a NaN value, LabVIEW has node called Not a Number/Path/Refnum which will return True when a NaN value is wired into it
In order to compare two arrays that may contain NaN elements, you can convert the values to strings and compare them that way. There are other reasons why it’s a bad idea to test floating point numbers for exact equality, but that’s a story for another time
If you’ve been ever been tripped up by a NaN or some other kind of unexpected behaviour, this thread on NI’s forums is a great resource