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

portability : Math.java

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 1.2.0
    • core-libs
    • sparc
    • solaris_2.5.1

      abs(double ) is wrong

          /**
           * Returns the absolute value of a <code>double</code> value.
           * If the argument is not negative, the argument is returned.
           * If the argument is negative, the negation of the argument is returned.
           *
           * @param a a <code>double</code> value.
           * @return the absolute value of the argument.
           * @since JDK1.0
           */

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


      In floating point, 0.0 == -0.0, so abs(+0.0) will return -0.0.

      The best thing is to recode this as:
       return a <= 0.0 ? 0.0 - a :a;

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

      This is probably more efficient than coding an extra comparison, or
      a doubleToLongBits operation.

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

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: