Ralph Corderoy
Partagé en mode public -Python’s NaN-handling catches people out, as it does in many languages.
>>> def nan(): return float('nan')
...
>>> n = nan()
>>> n == n, n == nan(), [n] == [n], [n] == [nan()]
(False, False, True, False)
>>> {n: 0, n: 1, nan(): 2, nan(): 3}
{nan: 1, nan: 2, nan: 3}
IEEE 754 decided NaN would never be seen as equal to itself. Bertrand Meyer¹, creator of the Eiffel programming language² with its design by contract³, argues the IEEE 754 got it wrong and the opposite is more useful, less unexpected, and “least damaging to time-honored assumptions embedded in mathematics”.
1. https://en.wikipedia.org/wiki/Bertrand_Meyer 2. https://en.wikipedia.org/wiki/Eiffel_%28programming_language%29 3. https://en.wikipedia.org/wiki/Design_by_contract
>>> def nan(): return float('nan')
...
>>> n = nan()
>>> n == n, n == nan(), [n] == [n], [n] == [nan()]
(False, False, True, False)
>>> {n: 0, n: 1, nan(): 2, nan(): 3}
{nan: 1, nan: 2, nan: 3}
IEEE 754 decided NaN would never be seen as equal to itself. Bertrand Meyer¹, creator of the Eiffel programming language² with its design by contract³, argues the IEEE 754 got it wrong and the opposite is more useful, less unexpected, and “least damaging to time-honored assumptions embedded in mathematics”.
1. https://en.wikipedia.org/wiki/Bertrand_Meyer 2. https://en.wikipedia.org/wiki/Eiffel_%28programming_language%29 3. https://en.wikipedia.org/wiki/Design_by_contract
Let me start, dear reader of this blog, by probing your view of equality, and also of assignment. Two questions: Is a value x always equal to itself? (For the seasoned programmers in the audience: I am talking about a value, as in mathematics, not an expression, as in programming, ...
1




6 commentaires
I guess if not-a-number is compared with not-a-number using a numerical comparision then it makes sense for that to be false in some way.
· Traduire
Ajoutez un commentaire...


































