<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Oct 31, 2017, at 7:26 PM, Xiaodi Wu &lt;<a href="mailto:xiaodi.wu@gmail.com" class="">xiaodi.wu@gmail.com</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><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; -webkit-text-stroke-width: 0px;" class="">On Tue, Oct 31, 2017 at 5:56 PM, David Sweeris<span class="Apple-converted-space">&nbsp;</span><span dir="ltr" class="">&lt;<a href="mailto:davesweeris@mac.com" target="_blank" class="">davesweeris@mac.com</a>&gt;</span><span class="Apple-converted-space">&nbsp;</span>wrote:<br class=""><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" class=""><div class=""><div class="m_5991692719474945925h5"><br class=""><div class="">On Oct 31, 2017, at 09:07, Stephen Canon via swift-dev &lt;<a href="mailto:swift-dev@swift.org" target="_blank" class="">swift-dev@swift.org</a>&gt; wrote:<br class=""><br class=""></div><blockquote type="cite" class=""><div class=""><span class="">[Replying to the thread as a whole]</span><br class=""><span class=""></span><br class=""><span class="">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 class=""><span class=""></span><br class=""><span class="">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 class=""><span class=""></span><br class=""><span class="">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 class=""><span class=""></span><br class=""><span class="">&nbsp; &nbsp;// Programmer fails to consider NaN behavior.</span><br class=""><span class="">&nbsp; &nbsp;if a == b {</span><br class=""><span class="">&nbsp; &nbsp;}</span><br class=""><span class=""></span><br class=""><span class="">but the correct implementation would be:</span><br class=""><span class=""></span><br class=""><span class="">&nbsp; &nbsp;// Programmer has thought about how to handle NaN here.</span><br class=""><span class="">&nbsp; &nbsp;if a == b || (a.isNaN &amp;&amp; b.isNaN) {</span><br class=""><span class="">&nbsp; &nbsp;}</span><br class=""><span class=""></span><br class=""><span class="">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 class=""><span class=""></span><br class=""><span class="">&nbsp; &nbsp;// Programmer fails to consider NaN behavior.&nbsp; This now traps if a or b is NaN.</span><br class=""><span class="">&nbsp; &nbsp;// That’s somewhat safer, but almost surely not the desired behavior.</span><br class=""><span class="">&nbsp; &nbsp;if a == b {</span><br class=""><span class="">&nbsp; &nbsp;}</span><br class=""><span class=""></span><br class=""><span class="">&nbsp; &nbsp;// Programmer considers NaNs. They now cannot use `==` until they rule out</span><br class=""><span class="">&nbsp; &nbsp;// either a or b is NaN. This actually makes the code *more* complicated and</span><br class=""><span class="">&nbsp; &nbsp;// less readable. Alternatively, they use `&amp;==` or whatever we call the unsafe</span><br class=""><span class="">&nbsp; &nbsp;// comparison and it’s just like what we had before, except now they have a</span><br class=""><span class="">&nbsp; &nbsp;// “weird operator”.</span><br class=""><span class="">&nbsp; &nbsp;if (!a.isNaN &amp;&amp; !b.isNaN &amp;&amp; a == b) || (a.isNaN &amp;&amp; b.isNaN) {</span><br class=""><span class="">&nbsp; &nbsp;}</span><br class=""><span class=""></span><br class=""><span class="">Now what happens if we return Bool?</span><br class=""><span class=""></span><br class=""><span class="">&nbsp; &nbsp;// Programmer fails to consider NaN behavior.&nbsp; Maybe the error when they</span><br class=""><span class="">&nbsp; &nbsp;// wrote a == b clues them in that they should. Otherwise they just throw in</span><br class=""><span class="">&nbsp; &nbsp;// a `!` and move on. They have the same bug they had before.</span><br class=""><span class="">&nbsp; &nbsp;if (a == b)! {</span><br class=""><span class="">&nbsp; &nbsp;}</span><br class=""><span class=""></span><br class=""><span class="">&nbsp; &nbsp;// Programmer considers NaNs. Unchanged from what we have currently,</span><br class=""><span class="">&nbsp; &nbsp;// except that we replace || with ??.</span><br class=""><span class="">&nbsp; &nbsp;if a == b ?? (a.isNaN &amp;&amp; b.isNaN) {</span><br class=""><span class="">&nbsp; &nbsp;}</span><br class=""><span class=""></span><br class=""><span class="">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 class=""><span class=""></span><br class=""><span class="">&nbsp; &nbsp;// Programmer fails to consider NaN behavior. Now their code works!</span><br class=""><span class="">&nbsp; &nbsp;if a == b {</span><br class=""><span class="">&nbsp; &nbsp;}</span><br class=""><span class=""></span><br class=""><span class="">&nbsp; &nbsp;// Programmer thinks about NaNs, realizes they can simplify their existing code:</span><br class=""><span class="">&nbsp; &nbsp;if a == b {</span><br class=""><span class="">&nbsp; &nbsp;}</span><br class=""><span class=""></span><br class=""><span class="">What are the downsides of this?</span><br class=""><span class=""></span><br class=""><span class="">&nbsp; &nbsp;(a) it will confuse sometimes experts who expect IEEE 754 semantics.</span><br class=""><span class="">&nbsp; &nbsp;(b) any code that uses `a != a` as an idiom for detecting NaNs will be broken.</span><br class=""><span class=""></span><br class=""><span class="">(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 &amp;== if you want that behavior”.</span><br class=""></div></blockquote><br class=""></div></div><div class="">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="Apple-converted-space">&nbsp;</span><i class="">always</i><span class="Apple-converted-space">&nbsp;</span>evaluate to `true`?</div></div></blockquote><div class=""><br class=""></div><div class="">Yes. `!=` is an extension method that cannot be overridden</div></div></div></div></div></blockquote><br class=""></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><br class=""></div><div>- Dave Sweeris</div></body></html>