Discussion
One of the basic laws of equality is that a value is always equal to itself. Given two copies of the same input, the equals operator (==) should always return true. But the special value NaN, short for Not a Number, doesn’t follow that rule. Or does it?
Treat NaN as a Double and it follows the letter of the IEEE 754 specification for floating-point numbers, which stipulates that NaN is never equal to itself. But label the same NaN value with a less specific type, such as Any, and it goes back to behaving like an ordinary Kotlin object.
The result is a self-contradicting comparison that can return two different results for the same pair of inputs, depending on the types you give to those inputs.
Undefined Value
To make all your hardware ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access