logoalt Hacker News

paulddrapertoday at 8:15 PM3 repliesview on HN

NaN == NaN is truly a perversion of equality.

It makes little sense that 1/0 is SIGFPE, but log(-5) is NaN in C.

And the same is true for higher level languages, and their error facilities.

What a mess.


Replies

adamzochowskitoday at 8:32 PM

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.

show 4 replies
Aardwolftoday at 8:23 PM

Python is extra annoying though with refusing to support division through zero the way other programming languages with IEEE floats do (i.e. output inf or nan instead of throwing an exception), even though it has no problem doing things like float('inf') / float('inf') --> nan. It specifically does it for division through zero as if it wants to be a junior grade calculator just for this one thing. They could at least have fixed this when breaking backwards incompatibility from python2 to 3...

show 3 replies
nitwit005today at 8:47 PM

There's no non-confusing option for comparisons. You have two invalid values, but they aren't necessarily the same invalid value. There are multiple operations that can produce NaN.

It's a sentinel value for an error. Once you have an error, doing math with the error code isn't sensible.

show 1 reply