<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=""><div class="">The only other alternative I can think of is to include a notion of unordered to our comparisons…</div><div class=""><br class=""></div><div class="">What if we made &lt;=&gt; return an optional comparison result, with nil meaning unordered (which is how Ruby does it)? &nbsp;For == and &lt;, we still require a strict total ordering (i.e. they would not be optional). &nbsp;Most of the time, &lt;=&gt; and &lt;, == would have the same comparison (&lt;=&gt; could have a default implementation based on == and &lt;), but in the case of floating point it would be slightly different: &nbsp;&lt;=&gt; would return nil for anything involving NaN (in full accordance with IEEE 754), while &lt; and == would somehow jam it into a strict total order (preferably with Nan == NaN so we don’t break collections).</div><div class=""><br class=""></div><div class="">Thanks,</div><div class="">Jon</div><br class=""><div><blockquote type="cite" class=""><div class="">On Apr 24, 2017, at 8:25 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 Mon, Apr 24, 2017 at 9:06 PM, Jonathan Hull via swift-evolution<span class="Apple-converted-space">&nbsp;</span><span dir="ltr" class="">&lt;<a href="mailto:swift-evolution@swift.org" target="_blank" class="">swift-evolution@swift.org</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;">As I am thinking about it more, this means that for == and &lt;<br class=""><br class="">NaN == NaN<br class="">-0 == +0<br class="">+Inf &lt; NaN<br class=""><br class="">Since this would break from IEEE,</blockquote><div class=""><br class=""></div><div class="">Yeah, as Steve mentioned, it's a huge deal to break from IEEE rules. Both the existing design and Dave's proposed design jump through huge hoops to preserve the behavior of these operators and reconcile them with the rest of Swift. The biggest weakness of my alternative idea as written up earlier is that it cannot preserve the spelling of these operators but requires a minor adjustment (`&amp;`). It's simply a no-go to move `==` to `compare(with: other, using: .ieee)`. No one will write numerics algorithms like that.</div><div class=""><br class=""></div><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;">I think we should also consider taking the opportunity to make == and &lt; work with a default tolerance.&nbsp; That is, 'a == b' would check that (a - b) &lt; epsilon for some reasonable choice of epsilon that works for common usage.&nbsp; I know this would make the hash function harder to write, but I think it is worthwhile.<br class=""><br class="">Then, as I mentioned before, people who need strict IEEE conformance would explicitly declare that need by using 'compare(with: other, using: .IEEE)' or whatever we want to call that metric.&nbsp; We can even provide metrics for different IEEE levels, if desired.<br class=""><br class="">We could also provide a function ‘tolerance(_:Self) -&gt; Comparable.Metric’ on FloatingPoint which returns a comparison metric that defines equality as (a - b) &lt; tolerance, for those who want to specify a specific tolerance for their use-case/algorithm...<br class=""><br class="">Thanks,<br class="">Jon<br class=""><br class=""><br class=""><br class="">&gt; On Apr 23, 2017, at 7:18 AM, Jonathan Hull via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:<br class="">&gt;<br class="">&gt; There is one more option which hasn’t really been considered:<br class="">&gt;<br class="">&gt; • == and != are tied to the Equatable protocol, which is essentially the == operation.<br class="">&gt; •&nbsp; &lt;, &lt;=, &gt;, &gt;= are tied to the Comparable protocol (which is kept the same except for minor changes/additions listed below)<br class="">&gt; • Hashable still requires Equatable<br class="">&gt; • There is a new ComparisonMetric concept which lets an algorithm specify exactly how the comparison is done (see below)<br class="">&gt;<br class="">&gt;<br class="">&gt; Tl;dr: There are different definitions of ‘comparison’ which make sense in different domains… so let’s make it explicit so it doesn’t surprise anyone.<br class="">&gt;<br class="">&gt; The question then becomes, which metric should be the default (i.e. the one defined by ‘&lt;‘ and ‘==‘), and the answer is: the one which lets us use floats/doubles in dictionaries and sets.&nbsp; People and algorithms which need full IEEE correctness can use a different metric which specifically guarantees it.&nbsp; They can even build their own metric if needed.<br class="">&gt;<br class="">&gt;<br class="">&gt; ====The Design====<br class="">&gt; // (Note: I wrote this code in mail, so it may not compile)<br class="">&gt;<br class="">&gt;<br class="">&gt; //This defines the result of a comparison. It would ideally be nested in the protocol below if that becomes possible.<br class="">&gt; enum ComparisonResult : Int {<br class="">&gt;&nbsp; &nbsp; &nbsp; &nbsp;case ascending = -1<br class="">&gt;&nbsp; &nbsp; &nbsp; &nbsp;case equal = 0<br class="">&gt;&nbsp; &nbsp; &nbsp; &nbsp;case descending = 1<br class="">&gt; }<br class="">&gt;<br class="">&gt; protocol Comparable {<br class="">&gt;&nbsp; &nbsp; &nbsp; &nbsp;typealias Metric = (Self, Self) -&gt; ComparisonResult&nbsp; //Give ourselves an easy way to refer to this function type<br class="">&gt;<br class="">&gt;&nbsp; &nbsp; &nbsp; &nbsp;var defaultMetric: Metric<br class="">&gt;&nbsp; &nbsp; &nbsp; &nbsp;static func &lt;(lhs: Self, rhs: Self) -&gt; Bool<br class="">&gt; }<br class="">&gt;<br class="">&gt; extension Comparable {<br class="">&gt;&nbsp; &nbsp; &nbsp; &nbsp;//Not shown: We would define &lt;=, etc… plus ≤,≥,and ≠&nbsp; (because, hey, it is my proposal)<br class="">&gt;<br class="">&gt;&nbsp; &nbsp; &nbsp; &nbsp;func compare(with other: Self, using metric: Metric) -&gt; ComparisonResult {<br class="">&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return metric(self, other)<br class="">&gt;&nbsp; &nbsp; &nbsp; &nbsp;}<br class="">&gt;<br class="">&gt;&nbsp; &nbsp; &nbsp; &nbsp;func compare(with other: Self) -&gt; ComparisonResult {<br class="">&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return self.defaultMetric(self, other)<br class="">&gt;&nbsp; &nbsp; &nbsp; &nbsp;}<br class="">&gt;<br class="">&gt;&nbsp; &nbsp; &nbsp; &nbsp;static func &lt;=&gt; (lhs: Self, rhs: Self) -&gt; Int {<br class="">&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return self.defaultMetric(lhs, rhs).rawValue<br class="">&gt;&nbsp; &nbsp; &nbsp; &nbsp;}<br class="">&gt;<br class="">&gt;&nbsp; &nbsp; &nbsp; &nbsp;var defaultMetric: Metric {<br class="">&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return&nbsp; { lhs, rhs in<br class="">&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if lhs == rhs {<br class="">&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return .equal<br class="">&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} else if lhs &lt; rhs {<br class="">&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return .ascending<br class="">&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}<br class="">&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return .descending<br class="">&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}<br class="">&gt;&nbsp; &nbsp; &nbsp; &nbsp;}<br class="">&gt; }<br class="">&gt;<br class="">&gt; ============<br class="">&gt;<br class="">&gt; Then for Double, we would make a second metric for IEEE compliant (or multiple for different levels)<br class="">&gt;<br class="">&gt; extension Double : Comparable {<br class="">&gt;<br class="">&gt;&nbsp; &nbsp; &nbsp; &nbsp;static func &lt; (lhs: Self, rhs: Self) -&gt; Bool {<br class="">&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//define using best for dictionaries / sets / layman understanding<br class="">&gt;&nbsp; &nbsp; &nbsp; &nbsp;}<br class="">&gt;<br class="">&gt;&nbsp; &nbsp; &nbsp; &nbsp;static func == (lhs: Self, rhs: Self) -&gt; Bool {<br class="">&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//define using best for dictionaries / sets / layman understanding<br class="">&gt;&nbsp; &nbsp; &nbsp; &nbsp;}<br class="">&gt;<br class="">&gt;&nbsp; &nbsp; &nbsp; &nbsp;static var IEEEcompare:Comparable.Metric {<br class="">&gt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//return function here that does full IEEE comparison<br class="">&gt;&nbsp; &nbsp; &nbsp; &nbsp;}<br class="">&gt;<br class="">&gt; }<br class="">&gt;<br class="">&gt; Then we can call ‘myDouble.compare(with: otherDouble, using: .IEEEcompare)’ when needed.<br class="">&gt;<br class="">&gt;<br class="">&gt; Thanks,<br class="">&gt; Jon<br class="">&gt;<br class="">&gt;<br class="">&gt;<br class="">&gt;&gt; On Apr 22, 2017, at 9:58 PM, Chris Lattner via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:<br class="">&gt;&gt;<br class="">&gt;&gt; On Apr 22, 2017, at 6:06 PM, Xiaodi Wu &lt;<a href="mailto:xiaodi.wu@gmail.com" class="">xiaodi.wu@gmail.com</a>&gt; wrote:<br class="">&gt;&gt;&gt; but my quick reaction to `&amp;==` is that it would make me quite nervous to have `==` not bound to 754-equals as it is in essentially every other language. In particular, I worry about the risk of people porting numerical code that depends on isnan(x) &lt;—&gt; !(x &lt; y) in non-obvious ways that they are unlikely to test. I’ll try to follow up with more detailed thoughts tomorrow.<br class="">&gt;&gt;&gt;<br class="">&gt;&gt;&gt; Indeed, it makes me a little nervous too. That said, `==` being either bound or not bound to 754 depending on the context is what makes me even more nervous.<br class="">&gt;&gt;&gt;<br class="">&gt;&gt;&gt; I was once adamantly against a new spelling for `==`, but on reconsideration it's clear to me that few if any numerical recipes can be ported verbatim from C-like languages and we should probably not encourage people to do so. Already, `+` needs to be rewritten as `&amp;+`, `&lt;&lt;` probably should be rewritten as `&amp;&lt;&lt;` (I still haven't had enough time to think about this), and the bitwise operators have differing precedences that require careful proofreading.<br class="">&gt;&gt;<br class="">&gt;&gt;<br class="">&gt;&gt; I haven’t been following this proposal or discussion closely, but it seems to me that there are a few workable approaches with different tradeoffs:<br class="">&gt;&gt;<br class="">&gt;&gt; 1. The strictly correct but user hostile approach:<br class="">&gt;&gt;<br class="">&gt;&gt; * == and != are tied to the Equatable protocol, which is essentially the == operation.<br class="">&gt;&gt; * &lt;, &lt;=, &gt;, &gt;= are tied to the Comparable protocol, which is essentially the &lt;=&gt; operation.<br class="">&gt;&gt; * Hashable doesn’t require equatable, it requires a related StrictlyEquatable protocol.<br class="">&gt;&gt; * StrictlyEquatable refines Equatable (with no other requirements, it is just a marker protocol), in which case FP types can’t conform to it, and thus can’t participate as dictionary keys<br class="">&gt;&gt;<br class="">&gt;&gt; =&gt; This approach sucks because you can’t have Set&lt;Float&gt;, or Dictionary&lt;Float, String&gt;.<br class="">&gt;&gt;<br class="">&gt;&gt; 2. The strictly correct but somewhat user hostile approach:<br class="">&gt;&gt;<br class="">&gt;&gt; * == and != are tied to the Equatable protocol, which is essentially the == operation.<br class="">&gt;&gt; * &lt;, &lt;=, &gt;, &gt;= are tied to the Comparable protocol, which is essentially the &lt;=&gt; operation.<br class="">&gt;&gt; * Hashable doesn’t require equatable, it requires a related StrictlyEquatable protocol.<br class="">&gt;&gt; * StrictlyEquatable doesn’t refine Equatable: it has a different requirement, and FP types can therefore implement both Equatable and StrictlyEquatable.<br class="">&gt;&gt;<br class="">&gt;&gt; =&gt; This approach is suboptimal because implementing your own type requires you to implement the &lt;=&gt; operation, as well as the StrictlyEquatable protocol, both.<br class="">&gt;&gt;<br class="">&gt;&gt; 3. The user friendly but incorrect model:<br class="">&gt;&gt;<br class="">&gt;&gt; * == and != are tied to the Equatable protocol, which is essentially the == operation.<br class="">&gt;&gt; * &lt;, &lt;=, &gt;, &gt;= are tied to the Comparable protocol, which is essentially the &lt;=&gt; operation.<br class="">&gt;&gt; * Hashable is defined in terms of Equatable.<br class="">&gt;&gt;<br class="">&gt;&gt; =&gt; This is easy (types just have to define &lt;=&gt;), but fails for FP types.<br class=""><span class="">&gt;&gt;<br class="">&gt;&gt;<br class="">&gt;&gt; I don’t think that this proposal is acceptable as written.&nbsp; I think it is really bad that abstracting a concrete algorithm would change its behavior so substantially.&nbsp; I don’t care about SNaNs, but I do care about the difference between +0/-1 and secondarily that of NaN handling.&nbsp; It seems really bad that generalizing something like:<br class="">&gt;&gt;<br class="">&gt;&gt; func doThing(a : Double, b : Double) -&gt; Bool {<br class="">&gt;&gt;&nbsp; ….<br class="">&gt;&gt;&nbsp; return a != b<br class="">&gt;&gt; }<br class="">&gt;&gt;<br class="">&gt;&gt; to:<br class="">&gt;&gt;<br class="">&gt;&gt; func doThing&lt;T : FloatingPoint&gt; (a : T, b : T) -&gt; Bool {<br class="">&gt;&gt;&nbsp; ….<br class="">&gt;&gt;&nbsp; return a != b<br class="">&gt;&gt; }<br class="">&gt;&gt;<br class="">&gt;&gt; would change behavior (e.g. when a is -0.0 and b is +0.0).&nbsp; &nbsp;Likewise, "T : Equatable".<br class="">&gt;&gt;<br class=""></span>&gt;&gt; -Chris<br class="">&gt;&gt;<br class="">&gt;&gt;<br class="">&gt;&gt; ______________________________<wbr class="">_________________<br class="">&gt;&gt; swift-evolution mailing list<br class="">&gt;&gt;<span class="Apple-converted-space">&nbsp;</span><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">&gt;&gt;<span class="Apple-converted-space">&nbsp;</span><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank" class="">https://lists.swift.org/<wbr class="">mailman/listinfo/swift-<wbr class="">evolution</a><br class="">&gt;<br class="">&gt; ______________________________<wbr class="">_________________<br class="">&gt; swift-evolution mailing list<br class="">&gt;<span class="Apple-converted-space">&nbsp;</span><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">&gt;<span class="Apple-converted-space">&nbsp;</span><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank" class="">https://lists.swift.org/<wbr class="">mailman/listinfo/swift-<wbr class="">evolution</a><br class=""><br class="">______________________________<wbr class="">_________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank" class="">https://lists.swift.org/<wbr class="">mailman/listinfo/swift-<wbr class="">evolution</a></blockquote></div></div></div></div></blockquote></div><br class=""></body></html>