Math.round() implementation does not match spec

Marcus Lagergren marcus.lagergren at oracle.com
Thu Mar 28 09:32:34 PDT 2013


André,

It appears you are correct. I've filed bug number JDK-8011023 to track this.  I agree that the fix is correct.  If you have signed a third party source contribution agreement for the OpenJDK I can push this and credit you as contributor. 

/M


On Mar 28, 2013, at 5:09 PM, André Bargull <andrebargull at googlemail.com> wrote:

> 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