It's a good day when I get to use the phrase 'the NaN can of worms' in a techblog entry. NaNs are weird and dangerous things and that's before you find out that there are multiple types of them.

It turns out that if you ever accidentally add a NaN as a key in a Go map, you've created a cursed thing. The only way to get the value back is to iterate the entire map, and you can't delete the key/value pair. If you add a NaN as a key more than once you get an especially cursed map, because of course you wind up with multiple entries where the key is NaN.

(Python has somewhat similar problems in a quick test, so it's not just Go. NaNs are a can of worms.)

Follow

@cks
Makes you wonder why we use NaNs instead of returning an error or something

@RustyStriker I suspect the original reason was to have defined results for floating point operations that weren't mathematically defined, and perhaps also to allow computations to continue without lots of error checking in the middle. In the modern era, exceptions are rather expensive at the CPU level so it probably helps FP units to not have to explode if you do '0/0' or some other NaN-producing operation.

Sign in to participate in the conversation
Qoto Mastodon

QOTO: Question Others to Teach Ourselves
An inclusive, Academic Freedom, instance
All cultures welcome.
Hate speech and harassment strictly forbidden.