Uploaded image for project: 'Commons Lang'
  1. Commons Lang
  2. LANG-381

NumberUtils.min(floatArray) returns wrong value if floatArray[0] happens to be Float.NaN

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 2.3
    • 2.4
    • None
    • None

    Description

      The min() method of NumberUtils returns the wrong result if the first value of the array happens to be Float.NaN. The following code snippet shows the behaviour:

      float a[] = new float[]

      {(float) 1.2, Float.NaN, (float) 3.7, (float) 27.0, (float) 42.0, Float.NaN}

      ;
      float b[] = new float[]

      {Float.NaN, (float) 1.2, Float.NaN, (float) 3.7, (float) 27.0, (float) 42.0, Float.NaN}

      ;

      float min = NumberUtils.min(a);
      System.out.println("min(a): " + min); // output: 1.2
      min = NumberUtils.min(b);
      System.out.println("min(b): " + min); // output: NaN

      This problem may exist for double-arrays as well.

      Proposal: Use Float.compare(float, float) or NumberUtils.compare(float, float) to achieve a consistent result.

      Attachments

        1. LANG-381-both.patch
          17 kB
          Henri Yandell
        2. LANG-381-IEEE-754r.patch
          5 kB
          Henri Yandell
        3. LANG-381-JDK.patch
          3 kB
          Henri Yandell

        Activity

          People

            Unassigned Unassigned
            tv Thomas Vandahl
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: