Remix.run Logo
AndriyKunitsyn 2 days ago

Here's one that's not a myth: IEEE-754 floats are the only "primitive types" that allow "a == a" to not be true.

I.e., two floats that are _identical_ to each other (even when it's _the same_ variable, on the same memory address) can be not _equal_ to each other, specifically if it's NaN. This is dictated by IEEE-754, and this is true for all programming languages I know, and to this day, this makes zero sense to me, but apparently this is useful for some reason.

zahlman 2 days ago | parent [-]

>but apparently this is useful for some reason.

It lets you easily test whether a value is NaN without needing a library function call. (Even if the language wanted to provide a NaN literal, there's more than one NaN value, so that wouldn't actually work!)

AndriyKunitsyn 2 days ago | parent [-]

This test is better with a function call, because it clearly shows the intent. Perf-wise, this function call will be inlined by any decent compiler. https://godbolt.org/z/chhM85vP3

Breaking a basic programming expectation for a specific class of values of a specific type is not a good thing, in my opinion.