roundTiesToEven

# Jonathan Watt (18 years ago)

I've noticed that rounding in the reference implementation is implemented using IEEE roundTiesToEven, but in browsers .5 values appear to round towards zero. Although roundTiesToEven is desirable from a computational point of view, there's likely a significant risk that changing to this method could break some existing ECMAScript.

Math.round(1.5) === Math.round(2.5) // true in RI, false in browsers

, Jonathan

# liorean (18 years ago)

On 11/11/2007, Jonathan Watt <jwatt at jwatt.org> wrote:

I've noticed that rounding in the reference implementation is implemented using IEEE roundTiesToEven, but in browsers .5 values appear to round towards zero. Although roundTiesToEven is desirable from a computational point of view, there's likely a significant risk that changing to this method could break some existing ECMAScript.

That's really serious breakage - books, tutorials, references and real world code all take for granted that halves alway round up.

# Jonathan Watt (18 years ago)

liorean wrote:

On 11/11/2007, Jonathan Watt <jwatt at jwatt.org> wrote:

I've noticed that rounding in the reference implementation is implemented using IEEE roundTiesToEven, but in browsers .5 values appear to round towards zero. Although roundTiesToEven is desirable from a computational point of view, there's likely a significant risk that changing to this method could break some existing ECMAScript.

That's really serious breakage

Well, it's likely only a small bug in the reference implementation rather than a requirement of the specification, so not too big a deal I'd think.

  • books, tutorials, references and real world code all take for granted that halves alway round up.

Err, right. Round up, not round to zero. Thanks for the correction.

# Brendan Eich (18 years ago)

On Nov 11, 2007, at 4:11 AM, liorean wrote:

On 11/11/2007, Jonathan Watt <jwatt at jwatt.org> wrote:

I've noticed that rounding in the reference implementation is
implemented using IEEE roundTiesToEven, but in browsers .5 values appear to round
towards zero. Although roundTiesToEven is desirable from a computational point
of view, there's likely a significant risk that changing to this method
could break some existing ECMAScript.

That's really serious breakage - books, tutorials, references and real world code all take for granted that halves alway round up.

Don't worry, that's not an intended change to double rounding
(decimal is new and has more rounding control, but for double we're
stuck).

It looks like an unintended consequence of using the SML-NJ Real64
library. I filed bugs.ecmascript.org/ticket/301. Thanks,
Jonathan, for pointing this out,

# Jonathan Watt (18 years ago)

Brendan Eich wrote:

I filed bugs.ecmascript.org/ticket/301.

Thanks for that. I'll file issues in the bug tracker directly in future.

Jonathan

# Graydon Hoare (18 years ago)

Jonathan Watt wrote:

liorean wrote:

On 11/11/2007, Jonathan Watt <jwatt at jwatt.org> wrote:

I've noticed that rounding in the reference implementation is implemented using IEEE roundTiesToEven, but in browsers .5 values appear to round towards zero. Although roundTiesToEven is desirable from a computational point of view, there's likely a significant risk that changing to this method could break some existing ECMAScript. That's really serious breakage

Well, it's likely only a small bug in the reference implementation rather than a requirement of the specification, so not too big a deal I'd think.

  • books, tutorials, references and real world code all take for granted that halves alway round up.

Err, right. Round up, not round to zero. Thanks for the correction.

I believe there is some confusion here. There is no proposed change to the binary floating point operations in ES4, relative to ES3.

There is a new datatype for decimal floating point, and this datatype has a different default rounding mode than the rounding mode in ES3 binary floating point. But this datatype has a wide variety of arithmetical differences. In particular:

  • You can actually represent 0.5 in the first place! Binary floating point cannot. Decimal can. In the default decimal context, 0.5 + 0.5 gives an exact decimal answer; there is no rounding to worry about.

  • You can adjust the rounding modes and precision used for decimal arithmetic, to precisely match your local monetary / banking / tax regulations. The choice of half-even as a default mode is based on precedent of other decimal libraries and banking locales. See the sage advice of Mike Cowlishaw on choice of decimal rounding modes: www2.hursley.ibm.com/decimal/decifaq1.html#rounding

There is no existing ES3 code in the wild that uses decimal arithmetic at all, much less a particular decimal rounding mode.

# Graydon Hoare (18 years ago)

Graydon Hoare wrote:

  • You can actually represent 0.5 in the first place! Binary floating point cannot. Decimal can. In the default decimal context, 0.5 + 0.5 gives an exact decimal answer; there is no rounding to worry about.

Oops, excuse me: 0.1. As I've had pointed out, 0.5 is happily representable in both :)

(And ... I appear to be quick on the draw on this thread anyways; if the RI happens to set up its binary floating point rounding modes, that's a correctly-reported bug in it. SML/NJ has surprised us with its mapping of IEEE754 binary floating point several times already. No change to the existing ES3 binary floating point is intended. Sorry, mea culpa.)