math-fround.js 878 B

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