Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6548425

Arrays.sort incorrectly sorts a double array containing negative zeros

    XMLWordPrintable

Details

    • Bug
    • Resolution: Fixed
    • P3
    • 7
    • 6
    • core-libs
    • b13
    • x86
    • windows_xp
    • Not verified

    Description

      FULL PRODUCT VERSION :
      java -1.6.0

      A DESCRIPTION OF THE PROBLEM :
      Arrays.sort don't behave properly when sorting a double array. When I use Arrays.sort to sort (3 inclusive, 8 exclusive) part of the double array {0.0d, 0.0d, 0.0d, 1, -0.0d, 2, -0.0d, -0.0d } , the expected result should
      be {0.0d, 0.0d, 0.0d, -0.0d, -0.0d, -0.0d, 1, 2}, but the actual result is
      {-0.0d, -0.0d, -0.0d, 0.0d, 0.0d, 0.0d, 1, 2}

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1. double[] da = {0.0d, 0.0d, 0.0d, 1, -0.0d, 2, -0.0d, -0.0d };
      2. Arrays.sort(da, 3, 8);
      3.for (int i = 0; i < 8; i++) {
             System.out.print(da[i] + " , " );
        }
        System.out.println();

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      0.0d, 0.0d, 0.0d, -0.0d, -0.0d, -0.0d, 1, 2,
      ACTUAL -
      -0.0d, -0.0d, -0.0d, 0.0d, 0.0d, 0.0d, 1, 2,

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.util.*;

      class DoubleSort {
      public static void main(String[] args){
        double[] da = {0.0d, 0.0d, 0.0d, 1, -0.0d, 2, -0.0d, -0.0d };
       Arrays.sort(da, 3, 8);
        for (int i = 0; i < 8; i++) {
             System.out.print(da[i] + " , " );
         }
        System.out.println();
      }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      the source code in Arrays is as follow

      // Postprocessing phase: change 0.0's to -0.0's as required
              if (numNegZeros != 0) {
                  int j = binarySearch0(a, fromIndex, n, 0.0d); // posn of ANY zero
                  do {
                      j--;
                  } while (j>=0 && a[j]==0.0d);

                  // j is now one less than the index of the FIRST zero
                  for (int k=0; k<numNegZeros; k++)
                      a[++j] = -0.0d;
              }
      we can fix this bug by replace the bound decision j >= 0 with j >= fromIndex

      Attachments

        Activity

          People

            martin Martin Buchholz
            ndcosta Nelson Dcosta (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: