| Makes perfect sense . NaN is a special type indicating one can't reason about it normal way. It is an unknown or value that can't be represented. When comparing, think of it like comparing two bags of unknown amount of apples. One bag has NaN count of apples Other bag has NaN count of apples Do the two bags have equal number of apples? I wish all languages used nulls the way SQL does. |
If NaNs were meant to represent unknown quantities, then they would return false for all comparisons. But NaN != NaN is true. Assuming that two unknowns are always different is just as incorrect as assuming that they're always the same.
I'd also push back on the idea that this behavior makes sense. In my experience it's a consistent source of confusion for anyone learning to program. It's one of the clearest violations of the principle of least astonishment in programming language design.
As others have noted, it makes conscientious languages like Rust do all sorts of gymnastics to accommodate. It's a weird edge case, and imo a design mistake. "Special cases aren't special enough to break the rules."
Also, I think high level languages should avoid exposing programmers to NaN whenever possible. Python gets this right: 0/0 should be an error, not a NaN.