Uploaded image for project: 'Commons Math'
  1. Commons Math
  2. MATH-471

MathUtils.equals(double, double) does not work properly for floats

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • None
    • 2.2, 3.0
    • None
    • None

    Description

      MathUtils.equals(double, double) does not work properly for floats.

      There is no equals(float,float) so float parameters are automatically promoted to double. However, that is not necessarily appropriate, given that the ULP for a double is much smaller than the ULP for a float.

      So for example:

      double oneDouble = 1.0d;
      assertTrue(MathUtils.equals(oneDouble, Double.longBitsToDouble(1 + Double.doubleToLongBits(oneDouble)))); // OK
      float oneFloat = 1.0f;
      assertTrue(MathUtils.equals(oneFloat, Float.intBitsToFloat(1 + Float.floatToIntBits(oneFloat)))); // FAILS
      float  f1 = 333.33334f;
      double d1 = 333.33334d;
      assertTrue(MathUtils.equals(d1, f1)); // FAILS
      

      I think the equals() methods need to be duplicated with the appropriate changes for floats to avoid any problems with the promotion of floats.

      Attachments

        1. Math471.patch
          7 kB
          Sebb

        Activity

          People

            Unassigned Unassigned
            sebb Sebb
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: