Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #16570

Re: Specification error for Math.round

Newsgroups comp.lang.javascript
Date 2012-10-11 21:46 -0700
References <6dc8cb55-c29c-4bb2-ab6d-d9a9447cffd4@googlegroups.com> <b07ab1c9-fbf6-4c55-964e-17a69be1a7f3@googlegroups.com>
Message-ID <39e06fb5-7d74-4b5d-83bf-d2f7b3181168@googlegroups.com> (permalink)
Subject Re: Specification error for Math.round
From RobG <rgqld@iinet.net.au>

Show all headers | View raw


On Friday, 12 October 2012 06:16:03 UTC+10, Danny  wrote:
> I'd say that'd be an implementation bug in IE/Opera, I use your number and gave it to Opera 11.62, and gave me the answer you got, then
> 
> Math.round(4503599627370499) -> 4503599627370500

Which confirms that Opera is consistent with IE and inconsistent with Firefox and Chrome.
 
> Math.round(4503599627370495) -> 4503599627370495
> 
> Math.round(4503599627370494) -> 4503599627370494

Those numbers are less than 2^52, so don't have the issue.


> Math wise, usually x+0.5 should give x+1 as a rounded figure

Not at all. If x is a decimal, then:

    2.75 rounds to 3
    2.75 + 0.5 = 3.25 which also rounds to 3

If x is an integer, then for simple rounding you are correct. But as Patricia said, a common rounding algorithm is to round to the nearest even number (sometimes called "bankers rounding") to attenuate rounding inaccuracy.

A peculiarity of javascript numbers is that 4503599627370498.5 is rounded to 4503599627370498 when converted to a number, so there is a kind of natural bankers rounding for such numbers.

> so I see nothing wrong with the implementation in FF/chrome, both good engines btw, however I can work with Math.floor(x+0.5) too.

The problem is that it's impossible to comply with the standard within the bounds of ECMAScript numbers. One fix is to do something like:

  function specialRound(n) {
    var num = String(n);

    // If n is an integer, return it
    if (/^[0-9]+$/.test(num)) {
      return n;
    }

    // Otherwise, round it
    return Math.round(n);
  }


which does what the ES5 specification says. It can be useful:

  var a = 4503599627370498;
  var b = 4503599627370497;
  specialRound(a) - specialRound(b); // 1 in all browsers
  Math.round(a) - Math.round(b);     // 0 in IE, 1 in Firefox

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Specification error for Math.round RobG <rgqld@iinet.net.au> - 2012-10-10 19:52 -0700
  Re: Specification error for Math.round Patricia Shanahan <pats@acm.org> - 2012-10-11 07:29 +0100
    Re: Specification error for Math.round Dr J R Stockton <reply1241@merlyn.demon.co.uk.invalid> - 2012-10-12 23:00 +0100
  Re: Specification error for Math.round Danny <dann90038@gmail.com> - 2012-10-11 13:16 -0700
    Re: Specification error for Math.round Patricia Shanahan <pats@acm.org> - 2012-10-12 00:14 +0100
    Re: Specification error for Math.round RobG <rgqld@iinet.net.au> - 2012-10-11 21:46 -0700
      Re: Specification error for Math.round Patricia Shanahan <pats@acm.org> - 2012-10-12 08:28 +0100
        Re: Specification error for Math.round Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-10-13 22:14 +0200
          Re: Specification error for Math.round Patricia Shanahan <pats@acm.org> - 2012-10-13 21:45 +0100
            Re: Specification error for Math.round "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2012-10-13 23:59 +0200
        Re: Specification error for Math.round Dr J R Stockton <reply1241@merlyn.demon.co.uk.invalid> - 2012-10-13 19:31 +0100
      Re: Specification error for Math.round Denis McMahon <denismfmcmahon@gmail.com> - 2012-10-14 11:09 +0000
  Re: Specification error for Math.round Dr J R Stockton <reply1241@merlyn.demon.co.uk.invalid> - 2012-10-12 22:48 +0100

csiph-web