UNPKG

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