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


Groups > comp.lang.javascript > #16549

Specification error for Math.round

Newsgroups comp.lang.javascript
Date 2012-10-10 19:52 -0700
Message-ID <6dc8cb55-c29c-4bb2-ab6d-d9a9447cffd4@googlegroups.com> (permalink)
Subject Specification error for Math.round
From RobG <rgqld@iinet.net.au>

Show all headers | View raw


The largest contiguous integer (i.e. the largest of the continuous sequence that can be represented exactly) in Javascript is 2^53, so it might be expected that integers below this value should not exhibit precision errors.

The ES5 specification for Math.round(x)[1] says that:

  "If x is already an integer, the result is x"

and that:

  "The value of Math.round(x) is the same as the
   value of Math.floor(x+0.5)"

So one might expect Math.round(x) to simply return x for all integers up to 2^53. But if the Math.floor criterion is applied, the limit is 2^52 (4503599627370496), so these criteria conflict for odd numbers between 2^52 and 2^53.

Browsers must choose which criterion they will respect, naturally some of the more popular ones have chosen differently.

e.g. in Firefox and Chrome:

    Math.round(4503599627370496)       -> 4503599627370496
    Math.round(4503599627370497)       -> 4503599627370497
    Math.floor(4503599627370497 + 0.5) -> 4503599627370498

which satisfies the first criterion, but fails the second.

In IE and Opera:

    Math.round(4503599627370496)       -> 4503599627370496
    Math.round(4503599627370497)       -> 4503599627370498
    Math.floor(4503599627370497 + 0.5) -> 4503599627370498

which satisfies the second criterion but not the first.

There are arguments to support both cases but there is no conclusive agrument one way or the other. It seems to me that this can't be called a bug in implementations, but is a specification error for providing incompatible requirements.

Is it possible to meet both criteria within the bounds of javascript numbers? 

Is there some overwhelmingly good argument to support one criterion and not the other?


1. http://ecma-international.org/ecma-262/5.1/#sec-15.8.2.15
2. http://ecma-international.org/ecma-262/5.1/#sec-15.8.2.6

-- 
Rob

Back to comp.lang.javascript | Previous | Next — 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