<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><br class=""><div><br class=""><blockquote type="cite" class=""><div class="">On Oct 24, 2017, at 22:04, Xiaodi Wu via swift-dev <<a href="mailto:swift-dev@swift.org" class="">swift-dev@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">On Tue, Oct 24, 2017 at 4:28 PM, David Zarzycki <span dir="ltr" class=""><<a href="mailto:dave@znu.io" target="_blank" class="">dave@znu.io</a>></span> wrote:<br class=""><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 style="word-wrap:break-word;line-break:after-white-space" class=""><span class=""><br class=""><div class=""><br class=""><blockquote type="cite" class=""><div class="">On Oct 24, 2017, at 14:55, Ben Cohen via swift-dev <<a href="mailto:swift-dev@swift.org" target="_blank" class="">swift-dev@swift.org</a>> wrote:</div><br class="m_-4265814734774014143Apple-interchange-newline"><div class=""><div 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" class="">There really is no way to square this circle. Every option is going to have downsides. We have to balance correctness, least surprise/most expected behavior for most people, and consistency. For me, making generic use of Equatable and Comparable stick to the documented conformance generically, while keeping FP-specific uses the way they are, is the least bad option. </div></div></blockquote></div><br class=""></span><div class="">Hi Ben,</div><div class=""><br class=""></div><div class="">Out of curiosity, what do you think about breaking Equatable into two protocols: Equatable and Substitutable? The former would be defined in terms of mathematics, while the latter is simple to define and usable by collections, etc. For example, while +0 equals -0, they are not substitutable (because 1.0 / +0.0 == +inf BUT 1.0 / -0.0 == -inf). In the case of NaNs, substitutability would depend on the NaN payload being the same or not (because some later subsystem might interpret the NaN payload).</div></div></blockquote><div class=""> <br class=""></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word;line-break:after-white-space" class=""><div class="">Finally, and unless I’m missing something, floating-point “substitutability” would translate to bitwise equality at the hardware level, which is nice from a performance perspective.</div></div></blockquote><div class=""><br class=""></div><div class="">As Steve pointed out, floating-point "substitutability" as you define it is unlikely to be desired in most contexts; differentiation by NaN payload or by decimal value representation is highly unintuitive, and it would be intolerable for generic `.contains(.nan)` to sometimes return true and sometimes false depending on NaN payload.</div><div class=""> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word;line-break:after-white-space" class=""><div class=""><br class=""></div><div class="">On the topic of sorting, we could do the same thing, where we break Comparable into two protocols: Comparable and Sortable. The former would be defined in terms of mathematics, while the latter has no mathematical obligation and therefore values like +0 and -0 can be consistently sorted. The same goes of NaNs with payloads (they can be sorted). And finally, at the hardware level, this should be quite efficient because of how the floating point bits are laid out (in that an integer comparison of FP bits should work for this Sortable proposal).</div><div class=""></div></div></blockquote></div><br class=""></div><div class="gmail_extra">Floating point values can certainly be consistently sorted as an sequence of bits, but again, it is unlikely that anyone would want such a sort of their floating point values.</div><div class="gmail_extra"><br class=""></div><div class="gmail_extra">But as to the topic of splitting protocols, it is essentially the design adopted by Rust. During discussion of Ben's original proposal, some core team members argued very eloquently against such a design based on what's happened in Rust. Namely:</div><div class="gmail_extra"><br class=""></div><div class="gmail_extra">- If Float is not Equatable but instead SomethingOtherThanEquatable (in Rust, it's called `PartialEq`), then we will have officially done our job in terms of stdlib semantic guarantees. However, in practice, people *want* to write generic algorithms that work with both integers and floating point values.</div><div class="gmail_extra">- Based on the Rust experience, what people will do when they realize that Float doesn't conform to Equatable is instead to write generic algorithms that operate on SomethingOtherThanEquatable while assuming that integer `==` and floating point `==` will work the same way.</div><div class="gmail_extra">- The result is that you now have two protocols instead of one, but most people still use only one--and still use it incorrectly. The existing problem has simply been transferred from Equatable to SomethingOtherThanEquatable. The new Equatable accomplishes nothing in terms of helping people write better code.</div></div></div></blockquote><br class=""></div><div>Hi Xiaodi,</div><div><br class=""></div><div>Based on what you wrote above, I am definitely *not* proposing for the Rust model. I was proposing something different where both fixed and floating point types would continue to conform to Equatable and Comparable, and those protocols would continue to be defined in terms of mathematics.</div><div><br class=""></div><div>What I am proposing is that collection classes, sorting algorithms, etc. use protocols that are *not* defined in terms of mathematics. While this may be temporarily surprising to some, I think the net result is easy to understand, easy to reason about, and defensible.</div><div><br class=""></div><div>Finally, on the specific topic of “.contains(.nan)”, I think this is both solvable and overblown. Just replace “.nan” with “.nan(.default)” and people will quickly understand and move on.</div><div><br class=""></div><div>Dave</div></body></html>