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

Math.round is not EcmaScript 5 compliant

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P2 P2
    • 8
    • 8
    • core-libs
    • None
    • b93
    • Verified

      From Andre Bargull on the nashorn-dev list

      The implementation for Math.round() does not match what's specified in the ES spec.

      A few test cases which currently fail:
      assertEq(-Infinity, 1/Math.round(-0.5))
      assertEq(9007199254740991, Math.round(9007199254740991))
      assertEq(18446744073709552000, Math.round(9223372036854775807*2))

      The NativeMath#round() method should be changed as follows:
      @Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
      public static Object round(final Object self, final Object x) {
         double d = JSType.toNumber(x);
         int e = Math.getExponent(d);
         if (e >= 52) {
             return d;
         }
         return Math.copySign(Math.floor(d + 0.5), d);
      }


      - André

            lagergren Marcus Lagergren
            lagergren Marcus Lagergren
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: