<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><br><div>On Oct 31, 2017, at 20:58, Xiaodi Wu <<a href="mailto:xiaodi.wu@gmail.com">xiaodi.wu@gmail.com</a>> wrote:<br><br></div><blockquote type="cite"><div><div dir="ltr">On Tue, Oct 31, 2017 at 10:50 PM, Xiaodi Wu <span dir="ltr"><<a href="mailto:xiaodi.wu@gmail.com" target="_blank">xiaodi.wu@gmail.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"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div><div class="h5">On Tue, Oct 31, 2017 at 10:23 PM, David Sweeris <span dir="ltr"><<a href="mailto:davesweeris@mac.com" target="_blank">davesweeris@mac.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div style="word-wrap:break-word"><div><div class="m_-4560982478301145588gmail-h5"><br><div><blockquote type="cite"><div>On Oct 31, 2017, at 7:26 PM, Xiaodi Wu <<a href="mailto:xiaodi.wu@gmail.com" target="_blank">xiaodi.wu@gmail.com</a>> wrote:</div><br class="m_-4560982478301145588gmail-m_5435354574384829892Apple-interchange-newline"><div><div dir="ltr" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px">On Tue, Oct 31, 2017 at 5:56 PM, David Sweeris<span class="m_-4560982478301145588gmail-m_5435354574384829892Apple-converted-space"> </span><span dir="ltr"><<a href="mailto:davesweeris@mac.com" target="_blank">davesweeris@mac.com</a>></span><span class="m_-4560982478301145588gmail-m_5435354574384829892Apple-converted-space"> </span><wbr>wrote:<br><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div dir="auto"><div><div class="m_-4560982478301145588gmail-m_5435354574384829892m_5991692719474945925h5"><br><div>On Oct 31, 2017, at 09:07, Stephen Canon via swift-dev <<a href="mailto:swift-dev@swift.org" target="_blank">swift-dev@swift.org</a>> wrote:<br><br></div><blockquote type="cite"><div><span>[Replying to the thread as a whole]</span><br><span></span><br><span>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.</span><br><span></span><br><span>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.</span><br><span></span><br><span>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:</span><br><span></span><br><span> // Programmer fails to consider NaN behavior.</span><br><span> if a == b {</span><br><span> }</span><br><span></span><br><span>but the correct implementation would be:</span><br><span></span><br><span> // Programmer has thought about how to handle NaN here.</span><br><span> if a == b || (a.isNaN && b.isNaN) {</span><br><span> }</span><br><span></span><br><span>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:</span><br><span></span><br><span> // Programmer fails to consider NaN behavior. This now traps if a or b is NaN.</span><br><span> // That’s somewhat safer, but almost surely not the desired behavior.</span><br><span> if a == b {</span><br><span> }</span><br><span></span><br><span> // Programmer considers NaNs. They now cannot use `==` until they rule out</span><br><span> // either a or b is NaN. This actually makes the code *more* complicated and</span><br><span> // less readable. Alternatively, they use `&==` or whatever we call the unsafe</span><br><span> // comparison and it’s just like what we had before, except now they have a</span><br><span> // “weird operator”.</span><br><span> if (!a.isNaN && !b.isNaN && a == b) || (a.isNaN && b.isNaN) {</span><br><span> }</span><br><span></span><br><span>Now what happens if we return Bool?</span><br><span></span><br><span> // Programmer fails to consider NaN behavior. Maybe the error when they</span><br><span> // wrote a == b clues them in that they should. Otherwise they just throw in</span><br><span> // a `!` and move on. They have the same bug they had before.</span><br><span> if (a == b)! {</span><br><span> }</span><br><span></span><br><span> // Programmer considers NaNs. Unchanged from what we have currently,</span><br><span> // except that we replace || with ??.</span><br><span> if a == b ?? (a.isNaN && b.isNaN) {</span><br><span> }</span><br><span></span><br><span>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:</span><br><span></span><br><span> // Programmer fails to consider NaN behavior. Now their code works!</span><br><span> if a == b {</span><br><span> }</span><br><span></span><br><span> // Programmer thinks about NaNs, realizes they can simplify their existing code:</span><br><span> if a == b {</span><br><span> }</span><br><span></span><br><span>What are the downsides of this?</span><br><span></span><br><span> (a) it will confuse sometimes experts who expect IEEE 754 semantics.</span><br><span> (b) any code that uses `a != a` as an idiom for detecting NaNs will be broken.</span><br><span></span><br><span>(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”.</span><br></div></blockquote><br></div></div><div>One more thought — and it’s crazy enough that I’m not even sure it’s worth posting — does Swift’s `Equatable` semantics require that `(a == b) != (a != b)`<span class="m_-4560982478301145588gmail-m_5435354574384829892Apple-converted-space"> </span><i>always</i><span class="m_-4560982478301145588gmail-m_5435354574384829892Apple-converted-space"> </span>evaluate to `true`?</div></div></blockquote><div><br></div><div>Yes. `!=` is an extension method that cannot be overridden</div></div></div></div></div></blockquote><br></div></div></div><div>Wait, what? So if I have a `Password` type, and want to trigger extra logging if the `!=` function is called too many times within a second or something, that won't get called in generic code? That seems... unintuitive...</div></div></blockquote><div><br></div></div></div><div>That's correct, as it is for all protocol extension methods (for example, most of the collection APIs).</div></div></div></div></blockquote><div><br></div><div>Incidentally, even if it were desirable to log on comparison, why would you want to log only if `!=` returns `true` and not when `==` returns `false`?</div></div></div></div>
</div></blockquote><br><div><span style="background-color: rgba(255, 255, 255, 0);">Mostly because if I ever wrote a Password class, I’d probably make it handle all the hashing and stuff internally so that the correct usage would look like `if enteredPW != storedPW {...}`. I know they’re (generally) the same, but I tend to think `_ != _` rather than `!(_ == _)`.</span><br></div><div><span style="background-color: rgba(255, 255, 255, 0);"><br></span></div><div><span style="background-color: rgba(255, 255, 255, 0);">I know it’d be a different thread (on a different mailing list), but does anyone care if I propose that we change this? I’m strongly in favor of providing a default implementation of !=, but I can’t think of why it shouldn’t just be a <i>default</i> method... with the developers still having the option to provide their own if they think they can get there faster than the default implementation (or maybe even just because they want to set a breakpoint on `!=` but not `==` or something).</span></div><div><span style="background-color: rgba(255, 255, 255, 0);"><br></span></div><div><span style="background-color: rgba(255, 255, 255, 0);">- Dave Sweeris</span></div></body></html>