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

Math.abs(-0.0F) and abs(-0.0D) must return +0.0, and not -0.0 according to JLS

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P3 P3
    • None
    • 1.1.4
    • core-libs
    • sparc
    • solaris_2.5.1



      Name: mf23781 Date: 12/09/97


      The behaviour of abs(-0.0) = -0.0 leads to oddities such as

      if (1.0 / abs(x) < 0.0) {
      }

      sometimes being true (when x = -0.0)

      The behaviour of abs(-0.0) in JDK 1.1 still appears inconsistent:

      The Java code is something like:
      public static double abs(double a) {
         return (a < 0) ? -a : a;
      }


      The behaviour also appears inconsistent in JDK 1.2:

      public static double abs(double a) {
          return (a < 0 || a == -0.0D) ? -a : a;
      }

      If a = +0.0, then a == -0.0D is true (both zeros are equal),
      so the statement returns -0.0 for abs(+0.0)


      Possible solution is:


         return (a <= 0) ? 0.0 - a : a;

      (because 0.0 - (-0.0) = 0.0, 0.0 - (0.0) = 0.0 )

      ======================================================================

            apalanissunw Anand Palaniswamy (Inactive)
            miflemi Mick Fleming
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: