When is a Number not a Number?

Consider this code snippet. I’m comparing two constants with the same value, but they’re not equal.

NaN is not equal to NaN

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

Not a Number slash Path slash Refnum node
Not a Number slash Path slash Refnum node

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

NaN in array with string conversion
NaN in array with string conversion

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

https://forums.ni.com/t5/BreakPoint/LabVIEW-Minutiae-that-may-bite-you-someday/m-p/1122234/highlight/true?profile.language=en#M12320


Subscribe to our newsletter

Did you find this post helpful? Sign up to the Lonely Ant newsletter to get more useful tips delivered directly to your mailbox

Unsubscribe at any time. I will not share your information with third parties.

* indicates required