3 ms·
This is based on the IEEE 754 totalOrder predicate. If you look at how this function works on binary floats, it turns out that it sorts numbers as 0xffff, 0xfff
by jcranmer 4y ago
This is based on the IEEE 754 totalOrder predicate. If you look at how this function works on binary floats, it turns out that it sorts numbers as 0xffff, 0xfffe, 0xfffd, ..., 0x8001, 0x8000, 0x0000, 0x0001, 0x0002, ..., 0x7ffd, 0x7ffe, 0x7fff [1]. This is not a bad sort order, and the reason why the NaNs are split up are because of the existence of signed NaN.
When it comes to floating point, it's probably better to bow to what IEEE 754 specifies (if it specifies something) than do what you come up yourself, even if it's less useful. x != x holding true for NaNs is another example here.
[1] Actually, the sort order of NaN payloads is implementation-defined, but given the nice bit representation mentioned above is easy to implement, you're probably unlikely to see any other implementation.