_math-fround.js 716 B

1234567891011121314151617181920212223
  1. // 20.2.2.16 Math.fround(x)
  2. var sign = require('./_math-sign');
  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. module.exports = Math.fround || function fround(x) {
  12. var $abs = Math.abs(x);
  13. var $sign = sign(x);
  14. var a, result;
  15. if ($abs < MIN32) return $sign * roundTiesToEven($abs / MIN32 / EPSILON32) * MIN32 * EPSILON32;
  16. a = (1 + EPSILON32 / EPSILON) * $abs;
  17. result = a - (a - $abs);
  18. // eslint-disable-next-line no-self-compare
  19. if (result > MAX32 || result != result) return $sign * Infinity;
  20. return $sign * result;
  21. };