title: Why is Java's Double.compare(double, double) implemented the way it is?
body: <p>I was looking at the implementation of <a href="http://java.sun.com/javase/6/docs/api/java/lang/Double.html#compare(double,%20double)" rel="noreferrer">compare(double, double)</a> in the Java standard library (6). It reads:</p>
  
  <pre><code>public static int compare(double d1, double d2) {
      if (d1 &lt; d2)
          return -1;       // Neither val is NaN, thisVal is smaller
      if (d1 &gt; d2)
          return 1;        // Neither val is NaN, thisVal is larger
  
      long thisBits = Double.doubleToLongBits(d1);
      long anotherBits = Double.doubleToLongBits(d2);
  
      return (thisBits == anotherBits ?  0 : // Values are equal
              (thisBits &lt; anotherBits ? -1 : // (-0.0, 0.0) or (!NaN, NaN)
               1));                          // (0.0, -0.0) or (NaN, !NaN)
  }
  </code></pre>
  
  <p>What are the merits of this implementation?</p>
  
  <hr>
  
  <p>edit: "Merits" was a (very) bad choice of words. I wanted to know how this works.</p>