Open Yaffle opened 9 years ago
see https://twitter.com/BrendanEich/status/360832908514181122
1) Math.round(0.5 - Number.EPSILON / 4) should be equal to 0
Math.round(0.5 - Number.EPSILON / 4)
0
2) 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
Math.round((2 / Number.EPSILON + 1) / 2 + 1)
(2 / Number.EPSILON + 1) / 2 + 1
Math.round(2 / Number.EPSILON)
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
Math.round
Note 2
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); };
Addressed by https://github.com/es-shims/es6-shim/commit/90c803f68390dd13fd5297b1e2d54d44f8dac94b
Sorry, that was in the es6-shim. The es5 parts need to be addressed here.
es6-shim
es5
see https://twitter.com/BrendanEich/status/360832908514181122
1)
Math.round(0.5 - Number.EPSILON / 4)
should be equal to0
2)
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 to2 / 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", butNote 2
says nothing aboutNote 2
was fixed in ES6 - https://people.mozilla.org/~jorendorff/es6-draft.html#sec-math.roundSome 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: