Is there any reason to use NaN instead of missing?

NaN and missing denote semantically different things. Idiomatically, NaN is an indication of an arithmetic failure (eg 0.0*Inf) or is sometimes used to poison computations. The math rules for NaN mostly makes sense if one interprets NaN as “this is number, but I don’t know which number it should be” (hence NaN^0.0==1.0, since any number to the zero power is one - although 0^0 is sometimes debated). missing refers to a value that never existed in the first place or was derived from such a number.

Sometimes, one doesn’t need this semantic distinction, in which case NaN can often serve the same role as missing (mostly as a poison value).

There will rarely be a performance benefit to using missing, but it can be make the code much more sensible in places. That said, the performance penalty is usually not huge (although can be significant in micro-benchmarks). I wouldn’t avoid missing for performance reasons if you’re actually trying to represent missing values.

As for quiet qNaN versus signaling sNaN, Julia does not make any distinction between the two. As far as I understand, Julia will never produce a sNaN in typical operation. Calling reinterpret is the primary way I’d expect them to arise in Julia. That said, most arithmetic operations on a sNaN will produce a qNaN, so they don’t tend to propagate very far.

6 Likes