Skip to content

Commit

Permalink
unify Math.f16round and Math.fround
Browse files Browse the repository at this point in the history
  • Loading branch information
zloirock committed Sep 24, 2023
1 parent 9466445 commit 95fc11e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 44 deletions.
27 changes: 5 additions & 22 deletions packages/core-js/internals/math-f16round.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,12 @@
'use strict';
var sign = require('../internals/math-sign');
var floatRound = require('../internals/math-float-round');

var abs = Math.abs;

var EPSILON = 2.220446049250313e-16; // Number.EPSILON
var EPSILON16 = 0.0009765625; // 2 ** -10
var INVERSE_EPSILON = 1 / EPSILON;
var MAX16 = 65504; // 2 ** 15 - 2 ** 10
var MIN16 = 6.103515625e-05; // 2 ** -14

var roundTiesToEven = function (n) {
return n + INVERSE_EPSILON - INVERSE_EPSILON;
};
var FLOAT16_EPSILON = 0.0009765625;
var FLOAT16_MAX_VALUE = 65504;
var FLOAT16_MIN_VALUE = 6.103515625e-05;

// `Math.f16round` method implementation
// https://github.com/tc39/proposal-float16array
module.exports = Math.f16round || function f16round(x) {
var n = +x;
var $abs = abs(n);
var $sign = sign(n);
var a, result;
if ($abs < MIN16) return $sign * roundTiesToEven($abs / MIN16 / EPSILON16) * MIN16 * EPSILON16;
a = (1 + EPSILON16 / EPSILON) * $abs;
result = a - (a - $abs);
// eslint-disable-next-line no-self-compare -- NaN check
if (result > MAX16 || result !== result) return $sign * Infinity;
return $sign * result;
return floatRound(x, FLOAT16_EPSILON, FLOAT16_MAX_VALUE, FLOAT16_MIN_VALUE);
};
23 changes: 23 additions & 0 deletions packages/core-js/internals/math-float-round.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict';
var sign = require('../internals/math-sign');

var abs = Math.abs;

var EPSILON = 2.220446049250313e-16; // Number.EPSILON
var INVERSE_EPSILON = 1 / EPSILON;

var roundTiesToEven = function (n) {
return n + INVERSE_EPSILON - INVERSE_EPSILON;
};

module.exports = function (x, FLOAT_EPSILON, FLOAT_MAX_VALUE, FLOAT_MIN_VALUE) {
var n = +x;
var absolute = abs(n);
var s = sign(n);
if (absolute < FLOAT_MIN_VALUE) return s * roundTiesToEven(absolute / FLOAT_MIN_VALUE / FLOAT_EPSILON) * FLOAT_MIN_VALUE * FLOAT_EPSILON;
var a = (1 + FLOAT_EPSILON / EPSILON) * absolute;
var result = a - (a - absolute);
// eslint-disable-next-line no-self-compare -- NaN check
if (result > FLOAT_MAX_VALUE || result !== result) return s * Infinity;
return s * result;
};
27 changes: 5 additions & 22 deletions packages/core-js/internals/math-fround.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,13 @@
'use strict';
var sign = require('../internals/math-sign');
var floatRound = require('../internals/math-float-round');

var abs = Math.abs;

var EPSILON = 2.220446049250313e-16; // Number.EPSILON
var EPSILON32 = 1.1920928955078125e-7; // 2 ** -23;
var INVERSE_EPSILON = 1 / EPSILON;
var MAX32 = 3.4028234663852886e+38; // 2 ** 128 - 2 ** 104
var MIN32 = 1.1754943508222875e-38; // 2 ** -126;

var roundTiesToEven = function (n) {
return n + INVERSE_EPSILON - INVERSE_EPSILON;
};
var FLOAT32_EPSILON = 1.1920928955078125e-7; // 2 ** -23;
var FLOAT32_MAX_VALUE = 3.4028234663852886e+38; // 2 ** 128 - 2 ** 104
var FLOAT32_MIN_VALUE = 1.1754943508222875e-38; // 2 ** -126;

// `Math.fround` method implementation
// https://tc39.es/ecma262/#sec-math.fround
// eslint-disable-next-line es/no-math-fround -- safe
module.exports = Math.fround || function fround(x) {
var n = +x;
var $abs = abs(n);
var $sign = sign(n);
var a, result;
if ($abs < MIN32) return $sign * roundTiesToEven($abs / MIN32 / EPSILON32) * MIN32 * EPSILON32;
a = (1 + EPSILON32 / EPSILON) * $abs;
result = a - (a - $abs);
// eslint-disable-next-line no-self-compare -- NaN check
if (result > MAX32 || result !== result) return $sign * Infinity;
return $sign * result;
return floatRound(x, FLOAT32_EPSILON, FLOAT32_MAX_VALUE, FLOAT32_MIN_VALUE);
};

0 comments on commit 95fc11e

Please sign in to comment.