Source: lib/polyfill/mathround.js

  1. /**
  2. * @license
  3. * Copyright 2016 Google Inc.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. goog.provide('shaka.polyfill.MathRound');
  18. goog.require('shaka.log');
  19. goog.require('shaka.polyfill.register');
  20. /**
  21. * @namespace shaka.polyfill.MathRound
  22. *
  23. * @summary A polyfill to patch math round bug on some browsers.
  24. */
  25. /**
  26. @const {number}
  27. @private
  28. */
  29. shaka.polyfill.MathRound.MAX_ACCURATE_INPUT_ = 0x10000000000000;
  30. /**
  31. * Install the polyfill if needed.
  32. */
  33. shaka.polyfill.MathRound.install = function() {
  34. shaka.log.debug('mathRound.install');
  35. var testNumber = shaka.polyfill.MathRound.MAX_ACCURATE_INPUT_ + 1;
  36. if (Math.round(testNumber) != testNumber) {
  37. shaka.log.debug('polyfill Math.round');
  38. var original_mathRound = Math.round;
  39. Math.round = function(number) {
  40. var result = number;
  41. // https://stackoverflow.com/questions/12830742/javascript-math-round-bug-in-ie
  42. // Due to the precision of JavaScript numbers, the number must be integer
  43. if (number <= shaka.polyfill.MathRound.MAX_ACCURATE_INPUT_) {
  44. result = original_mathRound(number);
  45. }
  46. return result;
  47. };
  48. }
  49. };
  50. shaka.polyfill.register(shaka.polyfill.MathRound.install);