title: min/max of collections containing NaN (handling incomparability in ordering)
body: <p>I just ran into a nasty bug as a result of the following behavior:</p>
  
  <pre><code>scala&gt; List(1.0, 2.0, 3.0, Double.NaN).min
  res1: Double = NaN
  
  scala&gt; List(1.0, 2.0, 3.0, Double.NaN).max
  res2: Double = NaN
  </code></pre>
  
  <p>I understand that for a pairwise comparison it may sometimes be preferable to have <code>max(NaN, 0) = NaN</code> and this is probably the reason why <code>java.lang.Double.compare</code> follows this convention (there seems to be an <a href="http://en.wikipedia.org/wiki/IEEE_754_revision#min_and_max" rel="noreferrer">IEEE standard</a> for that). For a collection however, I really think that this is a strange convention. After all the above collection does contain valid numbers; and these numbers have a clear maximum and minimum. In my opinion, the conception that the <em>maximum number</em> of the collection is <em>not a number</em> is a contradiction since, well, NaN is not a number, so it cannot be the maximum or minimum "number" of a collection -- unless there are no valid numbers at all; in this case it makes perfect sense that the maximum "is not a number". Semantically the <code>min</code> and <code>max</code> functions degenerate to a check whether the collection contains a NaN. Since there are more appropriate ways to check for the existence of NaNs (e.g. <code>collection.find(_.isNaN)</code>) it would be great to maintain a semantically meaningful min/max on collections.</p>
  
  <p>So my question is: What is the best approach to obtain the behavior to ignore the existence of NaNs? I see two possibilities:</p>
  
  <ol>
  <li><p>Filtering NaNs before calling min/max. Since this requires explicit handling of the issue in all places and may incur performance penalties I would prefer something easier.</p></li>
  <li><p>It would be great to have a kind of NaN-ignoring ordering which can be used as an implicit ordering wherever necessary. I tried the following:  </p>
  
  <pre><code>  object NanAwareOrdering extends Ordering[Double] {
      def compare(x: Double, y: Double) = {
        if (x.isNaN()) {
          +1 // without checking x, return y &lt; x
        } else if (y.isNaN()) {
          -1 // without checking y, return x &lt; y
        } else {
          java.lang.Double.compare(x, y)
        }
      }
    }
  </code></pre>
  
  <p>However, this approach seems to depend on whether I'm interesting in finding the min or max, i.e.:</p>
  
  <pre><code> scala&gt; List(1.0, 2.0, 3.0, Double.NaN).min(NanAwareOrdering)
   res7: Double = 1.0
  
   scala&gt; List(1.0, 2.0, 3.0, Double.NaN).max(NanAwareOrdering)
   res8: Double = NaN
  </code></pre>
  
  <p>This means that I would have to have two NanAwareOrdering depending on whether I want the minimum or the maximum, which would forbid to have an <code>implicit val</code>. Therefore my question is: How can I define an ordering in a way to handle both cases at once?</p></li>
  </ol>
  
  <p><strong>Update:</strong></p>
  
  <p>For the sake of completeness: In the course of analyzing the issue I realized that the premise "degenerates to a check for NaN" is actually wrong. In fact, I think it is even more ugly:</p>
  
  <pre><code>scala&gt; List(1.0, Double.NaN).min
  res1: Double = NaN
  
  scala&gt; List(Double.NaN, 1.0).min
  res2: Double = 1.0
  </code></pre>