math-fround.js 866 B

123456789101112131415161718192021222324252627
  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.es/ecma262/#sec-math.fround
  13. // eslint-disable-next-line es-x/no-math-fround -- safe
  14. module.exports = Math.fround || function fround(x) {
  15. var $abs = abs(x);
  16. var $sign = sign(x);
  17. var a, result;
  18. if ($abs < MIN32) return $sign * roundTiesToEven($abs / MIN32 / EPSILON32) * MIN32 * EPSILON32;
  19. a = (1 + EPSILON32 / EPSILON) * $abs;
  20. result = a - (a - $abs);
  21. // eslint-disable-next-line no-self-compare -- NaN check
  22. if (result > MAX32 || result != result) return $sign * Infinity;
  23. return $sign * result;
  24. };