math-fround.js 804 B

1234567891011121314151617181920212223242526
  1. var sign = require('../internals/math-sign');
  2. var abs = Math.abs;
  3. var pow = Math.pow;
  4. var EPSILON = pow(2, -52);
  5. var EPSILON32 = pow(2, -23);
  6. var MAX32 = pow(2, 127) * (2 - EPSILON32);
  7. var MIN32 = pow(2, -126);
  8. var roundTiesToEven = function (n) {
  9. return n + 1 / EPSILON - 1 / EPSILON;
  10. };
  11. // `Math.fround` method implementation
  12. // https://tc39.github.io/ecma262/#sec-math.fround
  13. module.exports = Math.fround || function fround(x) {
  14. var $abs = abs(x);
  15. var $sign = sign(x);
  16. var a, result;
  17. if ($abs < MIN32) return $sign * roundTiesToEven($abs / MIN32 / EPSILON32) * MIN32 * EPSILON32;
  18. a = (1 + EPSILON32 / EPSILON) * $abs;
  19. result = a - (a - $abs);
  20. // eslint-disable-next-line no-self-compare
  21. if (result > MAX32 || result != result) return $sign * Infinity;
  22. return $sign * result;
  23. };