<div dir="ltr">On Sat, Apr 22, 2017 at 11:00 PM, Dave Abrahams <span dir="ltr">&lt;<a href="mailto:dabrahams@apple.com" target="_blank">dabrahams@apple.com</a>&gt;</span> wrote:<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">
<span class="gmail-"><br>
&gt;&gt; &gt; That is to say, I would expect the standard library to supply an<br>
&gt;&gt; &gt; alternative implementation of equality for Array&lt;T where T :<br>
&gt;&gt; &gt; FloatingPoint&gt;.<br>
&gt;&gt;<br>
&gt;&gt; And also for Dictionary?  What do you expect to happen when Double is<br>
&gt;&gt; used as a dictionary key and it happens to be NaN?<br>
&gt;<br>
&gt; The proposal is very clear that `Dictionary` and `sort` will always use<br>
&gt; level 2 comparison.<br>
<br>
</span>Yes, but I&#39;m not asking about my own proposal :-).  My question is, what<br>
are *your* expectations for dictionaries or sets, specifically for the<br>
insertion of NaN keys and for comparison of whole collections?</blockquote><div><br></div><div>My expectations for comparison of collections clearly differ from yours. For me, I think it is a reasonable expectation that, for all `x` and `y` where `x == y`, `[x] == `[y]`, `[[x]] == [[y]]`, `[x, x, x] == [y, y, y]`, etc. To put it more strongly, I think that anything short of that is rather inexplicable.</div><div><br></div><div>With respect to dictionaries keys or sets, it&#39;s a subtly different question, I think. When it comes to questions like &quot;does this dictionary have this key&quot; or &quot;does this set already have this element,&quot; I think what the user is really asking about is a closer approximation to identity than to equality. I get that this gets complicated if we&#39;re talking about sets of class instances, so let me move away from the word identity and re-phrase. If a user asks if a set (or even an array) contains something, it&#39;s not exactly identical to asking if a set contains an element _equal to_ something. See:</div><div><br></div><div>* &quot;Does the set of cars in your parking lot contain my car?&quot; =&gt; `parkingLot.contains(myCar)`</div><div>* &quot;Does the set of cars in your parking lot contain a car that is equal to my car?&quot; =&gt; `parkingLot.contains { $0 == myCar }`</div><div><br></div><div>Put another way, one can simultaneously hold the thought that (1) a thing is itself, obviously; but (2) a thing is not necessarily _equal to_ itself.</div><div> </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"><div><div class="gmail-h5">
&gt;&gt; &gt;&gt; &gt;&gt; This is a bump in the rug – push it down in one place, it pops up<br>
&gt;&gt; &gt;&gt; &gt;&gt; in another. I feel like this proposal at least moves the bump to<br>
&gt;&gt; &gt;&gt; &gt;&gt; where<br>
&gt;&gt; &gt;&gt; fewer<br>
&gt;&gt; &gt;&gt; &gt;&gt; people will trip over it. I think it highly likely that the<br>
&gt;&gt; &gt;&gt; intersection of<br>
&gt;&gt; &gt;&gt; &gt;&gt; developers who understand enough about floating point to write truly<br>
&gt;&gt; &gt;&gt; &gt;&gt; correct concrete code, but won’t know about or discover the<br>
&gt;&gt; documented<br>
&gt;&gt; &gt;&gt; &gt;&gt; difference in generic code, is far smaller than the set of people who<br>
&gt;&gt; &gt;&gt; hit<br>
&gt;&gt; &gt;&gt; &gt;&gt; problems with the existing behavior.<br>
&gt;&gt; &gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt; &gt;<br>
&gt;&gt; &gt;&gt; &gt; So, to extend this analogy, I&#39;d rather say that the bump is not in the<br>
&gt;&gt; &gt;&gt; rug<br>
&gt;&gt; &gt;&gt; &gt; [Comparable] but rather in a section of the floor [FP NaN]. The rug<br>
&gt;&gt; might<br>
&gt;&gt; &gt;&gt; &gt; overlie the bump, but the bump will always be there and people will<br>
&gt;&gt; find<br>
&gt;&gt; &gt;&gt; it<br>
&gt;&gt; &gt;&gt; &gt; as they walk even if they don&#39;t immediately see it.<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt; Correct.<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt; &gt; If we don&#39;t want people to trip over the bump while walking on the<br>
&gt;&gt; &gt;&gt; &gt; rug, one very good alternative, IMHO, is to shape the rug so that it<br>
&gt;&gt; &gt;&gt; &gt; doesn&#39;t cover the bump.<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt; At what cost?<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt; More specifically: why is it the right behavior, for our audience, to<br>
&gt;&gt; &gt;&gt; trap when Equatable comparison happens to encounter NaN?  Will this not<br>
&gt;&gt; &gt;&gt; simply &quot;crash&quot; programs in the field that otherwise would have &quot;just<br>
&gt;&gt; &gt;&gt; worked?&quot;<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt; No, as I propose it, programs in the field would be automatically<br>
&gt;&gt; migrated<br>
&gt;&gt; &gt; to an alternative set of comparison operators `&amp;==`, `&amp;&lt;`, etc. that<br>
&gt;&gt; would<br>
&gt;&gt; &gt; work exactly as `==`, `&lt;`, etc. do today.<br>
&gt;&gt;<br>
&gt;&gt; Meaning, for floating point NaN &amp;== NaN is false, and if you want to<br>
&gt;&gt; write numeric code that accounts for NaN, you use &amp;==.<br>
&gt;&gt;<br>
&gt;&gt; OK, so... Is &amp;== a protocol requirement, or a protocol extension, or<br>
&gt;&gt; neither?  If so, to which protocol is it attached?<br>
&gt;<br>
&gt; Please allow me to refer you to a Gist:<br>
&gt; <a href="https://gist.github.com/xwu/e864ffdf343160a8a26839388f677768" rel="noreferrer" target="_blank">https://gist.github.com/xwu/<wbr>e864ffdf343160a8a26839388f6777<wbr>68</a><br>
&gt;<br>
&gt; In brief, it would be a protocol requirement on Comparable with a default<br>
&gt; implementation. The rationale for its being on Comparable is given in the<br>
&gt; text.<br>
<br>
</div></div>I&#39;m sorry, I&#39;ve seen your gist; I still can&#39;t find this rationale.<br></blockquote><div><br></div><div>&quot;These operators will be defined on Comparable and not on FloatingPoint. A key rationale for this design is to permit types other than FloatingPoint, including third-party types, to distinguish between signaling and quiet comparison of values when some values may be unordered with respect to others. (Another rationale for this design is that &amp;&lt; corresponds to what is currently spelled as &lt;, which can be used as a predicate for Sequence.sorted.)&quot;<br></div><div><br></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"><span class="gmail-">
&gt; I am not married to its being a requirement vs. an extension, but my<br>
&gt; initial thought here is that there might be reason to provide an<br>
&gt; alternative implementation in a conforming type, say for performance<br>
&gt; reasons on Float.<br>
&gt;<br>
&gt;&gt; &gt; I would quibble with the notion that all such generic algorithms<br>
&gt;&gt; &gt; currently &quot;just work,&quot;<br>
&gt;&gt;<br>
&gt;&gt; I never claimed they do!  They don&#39;t, because Equatable.== for floating<br>
&gt;&gt; point is not an equivalence relation.  That&#39;s part of what we aim to<br>
&gt;&gt; fix.<br>
&gt;&gt;<br>
&gt;&gt; You are proposing to fix that same problem a different way, one that leaves<br>
&gt;&gt; NaNs a bit out-in-the-cold (not necessarily bad), but also explicitly<br>
&gt;&gt; modifies generic algorithms so they continue to silently produce<br>
&gt;&gt; unspecified results (bad!)<br>
&gt;<br>
&gt; To clarify, no, I would not have the stdlib&#39;s generic algorithms continue<br>
&gt; to produce unspecified results. I propose changes to them which align their<br>
&gt; behavior with what you and Ben have proposed.<br>
<br>
</span>OK, I guess I must&#39;ve misunderstood your earlier statements.<br>
<br>
So this, IMO, is not tenable, at least in its current form:<br>
<br>
,----<br>
| The default implementation for contains, elementsEqual, split, and<br>
| starts(with:) on Sequence where Iterator.Element : Equatable, and for<br>
| index(of:) on Collection where Iterator.Element : Equatable, will<br>
| (notionally) use the following predicate:<br>
|<br>
| {<br>
|   ($0 &amp;== $1) || (<br>
|     ($0 &lt;=&gt; $1) == .none &amp;&amp;<br>
|     ($0 &lt;=&gt; $0) == .none &amp;&amp;<br>
|     ($1 &lt;=&gt; $1) == .none)<br>
| }<br>
`----<br>
<br>
The first problem here is that I can&#39;t figure out what this means and I<br>
doubt any normal user could either.</blockquote><div><br></div><div>I think it is a little ill-served by the notation. However the concept is simple. Sometimes, a thing cannot be compared even to itself. The prime example we speak of is NaN != NaN. This is the only major concept that the design I propose here would require a user to understand.</div><div><br></div><div>In this notation, if `x` cannot be compared to itself, `x &lt;=&gt; x` is nil. For `contains` and other methods that are asking &quot;do you have a thing&quot; more than they are asking &quot;do you have a thing that is equivalent to a thing,&quot; we&#39;ll regard all values that can&#39;t be compared even to themselves as &quot;the same&quot;; therefore, if `x &lt;=&gt; x` is nil and `y &lt;=&gt; y` is nil, then a collection with an element `x` will be regarded as &quot;containing&quot; `y`.</div><div><br></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">How would this change in semantics<br>
be reflected in the documentation for these algorithms?  How would you<br>
describe their requirements and results?  All these algorithms currently<br>
have simple, understandable descriptions.<br></blockquote><div><br></div><div>The description would be augmented by an explanation along the lines of this: &quot;Some types can represent values that do not compare equal to themselves; one example is floating point NaN (&quot;not a number&quot;). For the purposes of { contains | split | etc. }, every value not equal to itself is considered indistinguishable from every other value not equal to itself.&quot;</div><div><br></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">
Secondarily, I can&#39;t expect any generic algorithm author to understand<br>
what he&#39;s getting with this.<span class="gmail-"><br></span></blockquote><div><br></div><div>Obviously, we&#39;d have to have real-world data to back it up, but the beauty of `Comparison?` being an optional enum is that all an author has to do is handle all the cases. The compiler can even help with that. In other words, all your generic algorithm author has to do is to decide *something* for the question, &quot;What should I do when x &lt;=&gt; y returns nil?&quot;</div><div><br></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"><span class="gmail-">
&gt; Any automatically migrated third-party generic code would indeed continue<br>
&gt; to exhibit the same behavior as in Swift 3--but not only do I not consider<br>
&gt; that to be a problem, I consider it to be a requirement of source<br>
&gt; compatibility which is absolutely essential.<br>
<br>
</span>Well, we have not promised to preserve unspecified behaviors, so<br>
technically we can handle this either way.  And we all seem to be agreed<br>
that any code that uses, e.g., the default sort(), *will* change its<br>
behavior with respect to NaN.  So this is really a matter of degree.<br>
<span class="gmail-"><br>
&gt; It would not, however, be _invisible_ to the reader of the generic<br>
&gt; algorithm. The use of my proposed `&amp;==` in a generic context should stand<br>
&gt; out and prompt re-evaluation. That is to say, by using a different<br>
&gt; spelling, we would have a visible hint in the code that a generic algorithm<br>
&gt; may produce unspecified results with NaN.<br>
<br>
</span>That&#39;s a very nice feature.<br>
<span class="gmail-"><br>
&gt;&gt;&gt; but the result is that they would behave exactly as they do today and<br>
&gt;&gt;&gt; therefore would at least be no more broken.<br>
&gt;&gt;<br>
&gt;&gt; If that&#39;s all we acheive, we should do nothing.<br>
&gt;<br>
&gt; I should hope that it&#39;s not all we achieve. But, consider the following two<br>
&gt; alternatives: migrated code exhibits identical behavior to Swift 3, or<br>
&gt; migrated code silently exhibits different behavior that is &quot;fixed.&quot;<br>
<br>
</span>I think we&#39;re both agreed that the latter *will* happen with<br>
<br>
  sort(floatsContainingNaN)<br>
<br>
or<br>
<br>
   floatsContainingNaN.contains(.<wbr>NaN)</blockquote><div><br></div><div>Well, I do not agree that `[+0.0].contains(-0.0)` should return `false`, but we can discuss that on the side.</div><div><br></div><div>Otherwise, the key difference here is that code that&#39;s _correctly written_ for FP *would never use* a statement like `floatsContainingNaN.contains(.nan)` because with its current behavior it&#39;d be equivalent to `false` (or at least, it&#39;s not reliably `true`). The same cannot be said for `Array&lt;Double&gt;.==`, which can be profitably used when the user knows that `[.nan] != [.nan]`.</div><div><br></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"><span class="gmail-">
&gt; I am very disturbed by the possibility of the latter. It is the only<br>
&gt; part of this proposal that keeps me up at night.<br>
<br>
</span>I agree it&#39;s concerning, but I don&#39;t think fixing just part of this<br>
problem is going to help either ;-).<br>
<span class="gmail-"><br>
&gt; As it turns out, some people really do understand how floating point<br>
&gt; comparison works, and they might have even carefully written code that<br>
&gt; behaves correctly, relying on the current behavior when things are<br>
&gt; compared. Please don&#39;t &quot;fix&quot; that code. If an array of type [Float] starts<br>
&gt; to distinguish between +0.0 and -0.0 as you propose, I&#39;m quite sure that<br>
&gt; there is at least some code of my own that will be quite broken.<br>
<br>
</span>yep, sadly.  But IIUC that&#39;s inevitable.<br>
<span class="gmail-"><br>
&gt;&gt; &gt; Standard library changes to `sort` and other functions will make them<br>
&gt;&gt; &gt; &quot;just work&quot; with no distinguishable difference to the end user as<br>
&gt;&gt; &gt; compared to this proposal here.<br>
&gt;&gt;<br>
&gt;&gt; I&#39;m sorry, I don&#39;t know what &quot;this proposal here&quot; means.  Is that yours<br>
&gt;&gt; or the one Ben and I offered?  It&#39;s certainly different from the results<br>
&gt;&gt; of our proposal.<br>
&gt;&gt;<br>
&gt;&gt; The big problem with our proposal, AFAICT, is that<br>
&gt;&gt;<br>
&gt;&gt;     floatsIncludingNaNs.sort()<br>
&gt;&gt;<br>
&gt;&gt; works but<br>
&gt;&gt;<br>
&gt;&gt;     floatsIncludingNaNs.sort(&gt;)<br>
&gt;&gt;<br>
&gt;&gt; does not.  That is a real problem, but it *is* a difference from the<br>
&gt;&gt; current behavior, where neither one works.<br>
&gt;&gt;<br>
&gt;<br>
&gt; Hmm, I get the sense that some of my replies to you have been lost. I have<br>
&gt; explicitly proposed a design where `floatsIncludingNaNs.sort()` produces<br>
&gt; the same behavior as what is proposed by you and Ben. I&#39;d like to refer you<br>
&gt; again to the fleshed out Gist:<br>
&gt;<br>
&gt; <a href="https://gist.github.com/xwu/e864ffdf343160a8a26839388f677768" rel="noreferrer" target="_blank">https://gist.github.com/xwu/<wbr>e864ffdf343160a8a26839388f6777<wbr>68</a><br>
<br>
</span>Sorry I misread your earlier remarks.  I don&#39;t think I missed them.<br>
<span class="gmail-"><br>
&gt;&gt; &gt; It would be an improvement over how the algorithms work today with<br>
&gt;&gt; &gt; NaN.<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt; The major difference to the end user between what I propose and<br>
&gt;&gt; &gt; this proposal here will surface when _new_ code is written that<br>
&gt;&gt; &gt; uses `==` in the generic context, when working with types whose<br>
&gt;&gt; &gt; values may compare unordered. Since I propose `&lt;=&gt;` to return a<br>
&gt;&gt; &gt; value of type `Comparison?`, using the revised operator `==` is an<br>
&gt;&gt; &gt; assertion that the result of comparison is not unordered. A user is<br>
&gt;&gt; &gt; welcome to use `&amp;==` or a custom predicate if that is not their<br>
&gt;&gt; &gt; intention.<br>
&gt;&gt;<br>
&gt;&gt; The problem with this is that there&#39;s still no simple way to get an<br>
</span>                                                  ^^^^^^<br>
<span class="gmail-">&gt;&gt; equivalence relation or a total order over all Doubles, including NaNs.<br>
&gt;<br>
&gt; There is. Given two values x and y, `x &amp;&lt; y || (y &lt;=&gt; y) == nil` is<br>
&gt; identical to the `&lt;` that you propose.<br>
<br>
</span>Err, I rest my case?<br></blockquote><div><br></div><div>It&#39;d be easier with some more syntactic sugar, I guess. But my point above is that it is not difficult to describe what is happening in prose. Here, simply, if a value is not equal to itself, then it&#39;s ordered after all values that are equal to themselves.</div><div><br></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"><span class="gmail-">
&gt;&gt; Now, I&#39;m totally willing to have the discussion about how NaNs have no<br>
&gt;&gt; business being used as dictionary keys, or sort keys, or searched for,<br>
&gt;&gt; or any of the other things we do with day-to-day values.  That&#39;s not<br>
&gt;&gt; something I really have an opinion on, yet.<br>
&gt;<br>
&gt; I would not assert that NaN has no business being used here; again, my<br>
&gt; alternative design accommodates all of these use cases.<br>
&gt;<br>
&gt; Where we differ is that, in the case of a generic algorithm, my<br>
&gt; alternative design would result in the author of that algorithm either<br>
&gt; explicitly accommodating the presence of unordered values or asserting<br>
&gt; their absence.<br>
<br>
</span>That is a nice property, except that IIUC you&#39;re talking about granting<br>
an exception for the most common forms of algorithms.<br></blockquote><div><br></div><div>Not that I&#39;m aware of? All algorithms will need to be modified to deal with what happens if `(x &lt;=&gt; y) == nil`. I&#39;ve just listed the ways in which stdlib functions will do so.</div><div> </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">
Aside: I object to characterizing these things as unordered.</blockquote><div><br></div><div>Sorry, just using the terminology I read about. For instance, IEEE says that NaN compares _unordered_ to itself.</div><div> </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">For the<br>
purposes of the algorithms that are written to accomodate them, they<br>
must be very much ordered, if not with respect to each other, at *least*<br>
with respect to other values in the space.  In other words, there needs<br>
to be an underlying strict-weak order or the algorithms won&#39;t work.<br></blockquote><div><br></div><div>Not a fixed one, though, unless I&#39;m misunderstanding? It would be trivial in my design for `sort` to order NaN values to the _end of the array_, as opposed to ordering them greater than all other values, with the result that NaN comes last whether you sort ascending or descending. Not that I&#39;m proposing this behavior, but I don&#39;t *think* that it breaks anything.</div><div><br></div><div>Put another way, as far as I can tell, values like NaN only need to have a specified sequence with respect to other values _for the purposes of any particular operation at hand_. Therefore, I&#39;ve proposed a design where it&#39;s the generic algorithm and not the type that makes the decision for how these values are placed in sequence.</div><div><br></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"><span class="gmail-">
&gt; It is not an avoidable problem--this is the bump in the rug that<br>
&gt; cannot be smoothed out.<br>
&gt;<br>
&gt; I would posit that it is not possible to write an arbitrary generic<br>
&gt; algorithm that (a) compares floating point values; (b) doesn&#39;t account for<br>
&gt; NaN; and (c) behaves correctly, where correctly here means that it returns<br>
&gt; what an average user would expect who is not thinking of floating point<br>
&gt; comparison foibles.<br>
<br>
</span>Existence proof: any algorithm that, internally, uses binary search over<br>
a sorted collection of Comparabble values, or stores Comparable values<br>
in a tree-based dictionary, but does not care exactly how two distinct<br>
values sort, will work just fine<br>
<br>
...provided average users expect the existence of a distinct -0.0<br>
;-)<br></blockquote><div><br></div><div>Ha, that&#39;s not what I was trying to get at. I fully expect that there will be _some_ algorithms that will work out in this way. But, it is not possible to say that any particular design of Comparable will smooth over wrinkles with NaN such that generic algorithms in general can ignore the possibility of NaN and yet handle them &quot;correctly.&quot;</div><div><br></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"><span class="gmail-">
&gt; For instance, generic `max` produces what to the average user is<br>
&gt; nonsense if NaN compares greater than everything.<br>
&gt;<br>
&gt;&gt; I am, however, concerned that ordinary valid computations can lead to<br>
&gt;&gt; NaN and that allowing the appearance of a NaN to turn into a trap<br>
&gt;&gt; much later in the program, where it is finally compared with<br>
&gt;&gt; something, is not a behavior that would work for ordinary users.<br>
<br>
</span>That is my central worry with anything that turns operations on NaN into<br>
a trap.  I&#39;d very much appreciate hearing your thoughts.<br></blockquote><div><br></div><div>There are a limited number of ordinary operations that actually generate NaN.</div><div><br></div><div>If using arithmetic operators, there&#39;s 0/0 (which, as everyone already knows, traps if you&#39;re doing integer math--you&#39;ll notice I lobbied to remove `/` from protocols refined by both Integer and FloatingPoint, so now it&#39;s not even possible to accidentally do this in generic code unless you unwisely make your own `Divisible` protocol).</div><div><br></div><div>Other than that, you&#39;d have to be already working with infinite values and the arithmetic operators, or you&#39;d have to invoke some of the trigonometric and transcendental functions--but those are specific to floating point and not accounting for NaN would be pretty silly there.</div><div><br></div><div>In general, I really think there&#39;s something valuable to trapping when you try to propagate NaN through code that doesn&#39;t expect it. After all, _something_ has gone wrong if you&#39;re copying &quot;NaN GB&quot; of data, or you&#39;re at &quot;NaN%&quot; progress on a task. And those are innocuous examples because it&#39;s probable that the value is being used for user interface display and not much else. With other uses, certainly nothing good can come of an unaccounted-for NaN. Is it really the correct thing to supply some &quot;default&quot; answer, however explainable, when a user unintentionally asks, &quot;Is my number x less than not-a-number?&quot; As a healthcare provider, would it be OK for me to prescribe NaN doses of medication? An EMR written in Swift might tell me it&#39;s not less than the minimum dose!</div><div><br></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"><div><div class="gmail-h5">
&gt;&gt;<br>
&gt;&gt; &gt;&gt; My purpose in exploring an alternative design is to see if it would be<br>
&gt;&gt; &gt;&gt; &gt; feasible for non-FP-aware comparison operators to refuse to compare<br>
&gt;&gt; NaN,<br>
&gt;&gt; &gt;&gt; &gt; rather than giving different answers depending on context.<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt; So... to be clear, this is still different behavior based on context.<br>
&gt;&gt; &gt;&gt; Is this not just as confusing a result?<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt;   let nan = 0.0 / 0.0<br>
&gt;&gt; &gt;&gt;   print(nan == nan)     // false<br>
&gt;&gt; &gt;&gt;   print([nan] == [nan]) // trap<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt; &gt; I now strongly believe that this may make for a design simultaneously<br>
&gt;&gt; &gt;&gt; &gt; _less_ complex *and* _more_ comprehensive (as measured by the<br>
&gt;&gt; &gt;&gt; &gt; flatness-of-rug metric).<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;&gt; I&#39;m certainly willing to discuss it, but so far it doesn&#39;t seem like<br>
&gt;&gt; &gt;&gt; you&#39;ve been willing to answer the central questions above.<br>
&gt;&gt; &gt;&gt;<br>
&gt;&gt; &gt;<br>
&gt;&gt; &gt; Clearly, I&#39;m not understanding the central questions. Which ones have I<br>
&gt;&gt; &gt; left unanswered?<br>
&gt;&gt;<br>
&gt;&gt; Again:<br>
&gt;&gt;<br>
&gt;&gt;   Why is it the right behavior, for our audience, to trap when Equatable<br>
&gt;&gt;   comparison happens to encounter NaN?<br>
&gt;&gt;<br>
&gt;<br>
&gt; There are three possibilities (that I know of) when an equatable comparison<br>
&gt; `==` encounters NaN:<br>
&gt;<br>
&gt; * unspecified behavior (the current situation)<br>
&gt; * a default behavior (as proposed by you and Ben, that would be ordering<br>
&gt; NaN after all other values)<br>
&gt; * trapping (as proposed by me)<br>
&gt;<br>
&gt; I take it as given that you do not need to be convinced why unspecified<br>
&gt; behavior is inferior to the alternatives. As to why trapping is superior to<br>
&gt; a default behavior, I return to what I talk about above:<br>
&gt;<br>
&gt; Rhetorical question--do you think that there is any design for Comparable<br>
&gt; that would allow someone to compare floating point values *without* knowing<br>
&gt; about the existence of NaN in such a way that an arbitrary generic<br>
&gt; algorithm would behave as expected for a user who isn&#39;t thinking about<br>
&gt; floating point comparison?<br>
<br>
</div></div>Yep, see above.<br>
<span class="gmail-"><br>
&gt; As I point out above, ordering NaN after all other values works for `sort`<br>
&gt; but doesn&#39;t work so well for `max`. You can say that you&#39;ll provide a<br>
&gt; special floating point `max`, but I can do you one better with a design<br>
&gt; where the _generic algorithm_ and not the _comparable type_ sorts out what<br>
&gt; happens with unordered values. In such a design, both generic `sort` and<br>
&gt; generic `max` can offer fully specified, sensible behavior *without*<br>
&gt; special versions for floating point.<br>
<br>
</span>Yeah... I&#39;ll believe it when I see it.  The sensible specification, that<br>
is ;-).  So far what I&#39;ve seen looks much too complicated for simple<br>
notions like equality and ordering.<br>
<span class="gmail-"><br>
&gt; So, having said all of that, I return to address the question directly.<br>
&gt; Let&#39;s consider where a user might encounter `==` unexpectedly trapping on<br>
&gt; NaN:<br>
&gt;<br>
&gt; * The user is writing FP code, intending to use FP comparisons, and hasn&#39;t<br>
&gt; heard about the change in spelling. He or she is given immediate feedback<br>
&gt; and uses the corresponding operators `&amp;==`, etc. This is a one-time<br>
&gt; learning experience.<br>
<br>
</span>Yeah, that happens as long as the appearance of NaN in his or her<br>
computation is a case caught by tests.  But I&#39;m not confident it will<br>
be.<br>
<span class="gmail-"><br>
&gt; * The user is authoring a generic algorithm and using `==`. Trapping is<br>
&gt; optimal because either they will test their algorithm with floating point<br>
&gt; NaN and then consider how to handle that special case, or they will not<br>
&gt; test their algorithm and `==` is effectively a precondition that the<br>
&gt; algorithm will not encounter NaN, which would be an untested scenario. If,<br>
&gt; on the other hand, a default behavior is instead what occurs, it may not be<br>
&gt; unspecified behavior to _you_ the author of this proposal for Comparable,<br>
&gt; but it certainly would be behavior that has never been reasoned through by<br>
&gt; the author of the generic algorithm.<br>
&gt;<br>
&gt; * The user is calling a generic algorithm not designed for handling NaN<br>
&gt; using a FP argument that is NaN. I believe that trapping is still the<br>
&gt; correct behavior because silently proceeding with a result that the author<br>
&gt; of the generic algorithm has never tested or thought about is potentially<br>
&gt; more harmful than the user of the algorithm getting immediate feedback that<br>
&gt; the algorithm has not been tested with NaN. For instance, how can I tell if<br>
&gt; stdlib `max` is &quot;NaN-ready&quot;? Well, if `max` is not NaN-ready, in my<br>
&gt; proposed design `max(.nan)` will trap right away; in yours, one must<br>
&gt; inspect the result and consider whether the behavior is what is intended<br>
&gt; and appropriate, which should principally be the job of the author of the<br>
&gt; generic algorithm.<br>
<br>
</span>Again, I&#39;m concerned that NaNs will arise as the result of computations<br>
involving external inputs, and that programs that would otherwise<br>
harmlessly propagate NaN values (just as non-signaling NaN was<br>
designed!) will trap in the field, very far from the original source of<br>
the problem, which means bugs will be very hard to correct.<br></blockquote><div><br></div><div>Is it that common to get NaN as a result of external inputs? JSON, for example, doesn&#39;t even permit NaN. I agree that it is possible to propagate NaN in a useful way, and indeed I would propose to expand the option to propagate values that are unordered with respect to themselves by having operators defined on Comparable. However, my disagreement with you here is that we should not assume that _unintentional_ propagation is _harmless_ propagation.</div></div></div></div>