Skip to content

Commit

Permalink
Fix math round when number > 2^52 on IE11 (#832)
Browse files Browse the repository at this point in the history
Don't use math round on ie11 if number > 2^52.
  • Loading branch information
iKinnrot authored and joeyparrish committed Jun 5, 2017
1 parent 86f6b2b commit 734ac9b
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
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
58 changes: 58 additions & 0 deletions lib/polyfill/mathround.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* @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 some browsers.
*/


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


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

var testNumber = shaka.polyfill.MathRound.MAX_ACCURATE_INPUT_ + 1;
if (Math.round(testNumber) != testNumber) {
shaka.log.debug('polyfill Math.round');
var original_mathRound = Math.round;
Math.round = function(number) {
var result = number;
// https://stackoverflow.com/questions/12830742/javascript-math-round-bug-in-ie
// Due to the precision of JavaScript numbers, the number must be integer
if (number <= shaka.polyfill.MathRound.MAX_ACCURATE_INPUT_) {
result = original_mathRound(number);
}
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 @@ -44,6 +44,7 @@ goog.require('shaka.offline.OfflineScheme');
goog.require('shaka.offline.Storage');
goog.require('shaka.polyfill.Fullscreen');
goog.require('shaka.polyfill.IndexedDB');
goog.require('shaka.polyfill.MathRound');
goog.require('shaka.polyfill.MediaKeys');
goog.require('shaka.polyfill.MediaSource');
goog.require('shaka.polyfill.Promise');
Expand Down

0 comments on commit 734ac9b

Please sign in to comment.