logoalt Hacker News

adamzochowskiyesterday at 8:32 PM4 repliesview on HN

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.


Replies

caditinpiscinamyesterday at 8:59 PM

Respectfully, I disagree.

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.

show 1 reply
glkindlmannyesterday at 9:00 PM

I like this justifiation of NaN != NaN; it emphasizes that NaN has representional intent, more than just some bit pattern.

We take for granted that (except for things like x86 extended precision registers) floating point basically works the same everywhere, which was the huge victory of IEEE 754. It easy to lose sight of that huge win, and to be ungrateful, when one's first introduction to IEEE 754 are details like NaN!=NaN.

paulddraperyesterday at 9:50 PM

It makes no sense.

1/0 is an error (SIGFPE). log(-5) is a value (NaN).

---

I suppose you could have this "no reflexive equality" sentinel, but it applied so randomly in languages as to be eternally violate the principle of least astonishment.