Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Math.round issues #288

Open
Yaffle opened this issue Jan 17, 2015 · 2 comments
Open

Math.round issues #288

Yaffle opened this issue Jan 17, 2015 · 2 comments
Assignees

Comments

@Yaffle
Copy link
Contributor

Yaffle commented Jan 17, 2015

see https://twitter.com/BrendanEich/status/360832908514181122

Math.round(0.5 - Number.EPSILON / 4) should be equal to 0

Math.round((2 / Number.EPSILON + 1) / 2 + 1) should be equal to (2 / Number.EPSILON + 1) / 2 + 1
Math.round(2 / Number.EPSILON) should be equal to 2 / Number.EPSILON

The ES5 spec - http://es5.github.io/#x15.8.2.15 - says, that Math.round "Returns the Number value that is closest to x and is equal to a mathematical integer", but Note 2 says nothing about
Note 2 was fixed in ES6 - https://people.mozilla.org/~jorendorff/es6-draft.html#sec-math.round

Some browsers have good implementaions (FF 35, Chrome ?):
https://bugzilla.mozilla.org/show_bug.cgi?id=622348
https://bugzilla.mozilla.org/show_bug.cgi?id=1000606

But IE 11, Opera 12, Safari are buggy here.

I think, the implementation may look like this:

Math.round = function (x) {
  var n = Number(x);
  var ceil = Math.ceil(n);
  return ceil  - (ceil - 0.5 > n ? 1 : 0);
};
@ljharb
Copy link
Member

ljharb commented Jan 18, 2015

Addressed by es-shims/es6-shim@90c803f

@ljharb ljharb closed this as completed Jan 18, 2015
@ljharb ljharb reopened this Jan 18, 2015
@ljharb
Copy link
Member

ljharb commented Jan 18, 2015

Sorry, that was in the es6-shim. The es5 parts need to be addressed here.

@ljharb ljharb self-assigned this Jan 18, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants