Math.round() implementation does not match spec

André Bargull andrebargull at googlemail.com
Thu Mar 28 09:09:48 PDT 2013


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é


More information about the nashorn-dev mailing list