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

Fix math round when number > 2^52 on IE11 #832

Merged
merged 4 commits into from
Jun 2, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build/types/polyfill
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

+../../lib/polyfill/fullscreen.js
+../../lib/polyfill/indexed_db.js
+../../lib/polyfill/mathround.js
+../../lib/polyfill/mediakeys.js
+../../lib/polyfill/mediasource.js
+../../lib/polyfill/patchedmediakeys_ms.js
Expand Down
51 changes: 51 additions & 0 deletions lib/polyfill/mathround.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* @license
* Copyright 2016 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
goog.provide('shaka.polyfill.MathRound');

goog.require('shaka.log');
goog.require('shaka.polyfill.register');

/**
* @namespace shaka.polyfill.MathRound
*
* @summary A polyfill to patch math round bug on IE11.
*/


/**
* Install the polyfill if needed.
*/
shaka.polyfill.MathRound.install = function() {
shaka.log.debug('mathRound.install');

var agent = navigator.userAgent;
if (agent && agent.indexOf('rv:11.0') >= 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be better to use feature detection instead of browser detection. How about:

if (Math.round(4503599627370497) != 4503599627370497) {
...
}

shaka.log.debug('fix mathRound on IE11');
var original_mathRound = Math.round;
Math.round = function(number) {
var result = number;
// workaround for IE brain-dead Math.round() implementation
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please be more politically correct:

// Workaround for a rounding bug in IE11.

// https://stackoverflow.com/questions/12830742/javascript-math-round-bug-in-ie
if (number < 4503599627370496) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This constant would be more readable as 0x10000000000000, I think. Also, please move this to a static const. Something like:

/**

  • @const {number}
  • @Private
    */
    shaka.polyfill.MathRound.MAX_ACCURATE_INPUT_ = 0x10000000000000;

Finally, please be careful about < vs <=. From the stackoverflow thread, it appears that this constant does round correctly, so we should use the original for inputs <= the constant.

result = original_mathRound(number);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a comment that includes why it doesn't need to be rounded. Something like "Otherwise, due to the precision of JavaScript numbers, the number must already be an integer."

return result;
};
}
};

shaka.polyfill.register(shaka.polyfill.MathRound.install);
1 change: 1 addition & 0 deletions shaka-player.uncompiled.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,6 @@ goog.require('shaka.polyfill.MediaSource');
goog.require('shaka.polyfill.Promise');
goog.require('shaka.polyfill.VTTCue');
goog.require('shaka.polyfill.VideoPlaybackQuality');
goog.require('shaka.polyfill.MathRound');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alphabetize these calls.

goog.require('shaka.polyfill.installAll');
goog.require('shaka.util.Error');