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. * @see https://goo.gl/1XDi1W
  25. */
  26. /**
  27. @const {number}
  28. @private
  29. */
  30. shaka.polyfill.MathRound.MAX_ACCURATE_INPUT_ = 0x10000000000000;
  31. /**
  32. * Install the polyfill if needed.
  33. */
  34. shaka.polyfill.MathRound.install = function() {
  35. shaka.log.debug('mathRound.install');
  36. let testNumber = shaka.polyfill.MathRound.MAX_ACCURATE_INPUT_ + 1;
  37. if (Math.round(testNumber) != testNumber) {
  38. shaka.log.debug('polyfill Math.round');
  39. let originalMathRound = Math.round;
  40. Math.round = function(number) {
  41. let result = number;
  42. // Due to the precision of JavaScript numbers, the number must be integer.
  43. if (number <= shaka.polyfill.MathRound.MAX_ACCURATE_INPUT_) {
  44. result = originalMathRound(number);
  45. }
  46. return result;
  47. };
  48. }
  49. };
  50. shaka.polyfill.register(shaka.polyfill.MathRound.install);