<div dir="ltr">On Tue, Oct 31, 2017 at 11:07 AM, Stephen Canon <span dir="ltr"><<a href="mailto:scanon@apple.com" target="_blank">scanon@apple.com</a>></span> wrote:<br><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">[Replying to the thread as a whole]<br>
<br>
There have been a bunch of suggestions for variants of `==` that either trap on NaN or return `Bool?`. I think that these suggestions result from people getting tunnel-vision on the idea of “make FloatingPoint equality satisfy desired axioms of Equatable / Comparable”. This is misguided. Our goal is (should be) to make a language usable by developers; satisfying axioms is only useful in as much as they serve that goal.<br>
<br>
Trapping or returning `Bool?` does not make it easier to write correct concrete code, and it does not enable writing generic algorithms that operate on Comparable or Equatable. Those are the problems to be solved.<br>
<br>
Why do they not help write correct concrete code? The overwhelming majority of cases in which IEEE 754 semantics lead to bugs are due to non-reflexivity of equality, so let’s focus on that. In the cases where this causes a bug, the user has code that looks like this:<br>
<br>
// Programmer fails to consider NaN behavior.<br>
if a == b {<br>
}<br>
<br>
but the correct implementation would be:<br>
<br>
// Programmer has thought about how to handle NaN here.<br>
if a == b || (a.isNaN && b.isNaN) {<br>
}<br>
<br>
W.r.t ease of writing correct *concrete* code, the task is to make *this* specific case cleaner and more intuitive. What does this look like under other proposed notions of equality? Suppose we make comparisons with NaN trap:<br>
<br>
// Programmer fails to consider NaN behavior. This now traps if a or b is NaN.<br>
// That’s somewhat safer, but almost surely not the desired behavior.<br>
if a == b {<br>
}<br>
<br>
// Programmer considers NaNs. They now cannot use `==` until they rule out<br>
// either a or b is NaN. This actually makes the code *more* complicated and<br>
// less readable. Alternatively, they use `&==` or whatever we call the unsafe<br>
// comparison and it’s just like what we had before, except now they have a<br>
// “weird operator”.<br>
if (!a.isNaN && !b.isNaN && a == b) || (a.isNaN && b.isNaN) {<br>
}<br>
<br>
Now what happens if we return Bool?<br>
<br>
// Programmer fails to consider NaN behavior. Maybe the error when they<br>
// wrote a == b clues them in that they should. Otherwise they just throw in<br>
// a `!` and move on. They have the same bug they had before.<br>
if (a == b)! {<br>
}<br>
<br>
// Programmer considers NaNs. Unchanged from what we have currently,<br>
// except that we replace || with ??.<br>
if a == b ?? (a.isNaN && b.isNaN) {<br>
}<br>
<br>
If we are going to do the work of introducing another notion of floating-point equality, it should directly solve non-reflexivity of equality *by making equality reflexive*. My preferred approach would be to simply identify all NaNs:<br>
<br>
// Programmer fails to consider NaN behavior. Now their code works!<br>
if a == b {<br>
}<br>
<br>
// Programmer thinks about NaNs, realizes they can simplify their existing code:<br>
if a == b {<br>
}<br>
<br>
What are the downsides of this?<br>
<br>
(a) it will confuse sometimes experts who expect IEEE 754 semantics.<br>
(b) any code that uses `a != a` as an idiom for detecting NaNs will be broken.<br>
<br>
(b) is by far the bigger risk. It *will* result in some bugs. Hopefully less than result from people failing to consider NaNs. The only real risk with (a) is that we get a biennial rant posted to hacker news about Swift equality being broken, and the response is basically “read the docs, use &== if you want that behavior”.<br></blockquote><div><br></div><div>One of my premises for this discussion was that concrete NaN != NaN is desirable, correct, and an absolute must-have; the question here was how to write correct *generic* code given that Equatable currently guarantees a == a for all a. Do you disagree with the premise?</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
One specific response:<br>
<span class=""><br>
> I see the handling of NaN as a legacy/compatibility issue due to committee/vendor politics from the 1980’s. I am pretty sure if they could do it over with modern tech, we would just have isNan() and NaN == NaN… or we might just have optionals instead.<br>
<br>
</span>With the exception of how they interact with non-floating-point types (comparisons, conversions to/from integers and strings), NaNs are just Maybes with fast hardware support. Integers and booleans and strings are outside the scope of IEEE 754, so it was not in the standard’s purview to do anything else for those operations. They are not some exotic legacy thing leftover from the 1980’s; they were quite ahead of their time.<br>
<br>
– Steve</blockquote></div><br></div></div>