Skip to content

Commit 259cf3f

Browse files
committed
avoid duplication of some logic
1 parent 663d4b2 commit 259cf3f

File tree

5 files changed

+24
-21
lines changed

5 files changed

+24
-21
lines changed

packages/core-js/internals/math-float-round.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
'use strict';
22
var sign = require('../internals/math-sign');
3+
var roundTiesToEven = require('../internals/math-round-ties-to-even');
34

45
var abs = Math.abs;
56

67
var EPSILON = 2.220446049250313e-16; // Number.EPSILON
7-
var INVERSE_EPSILON = 1 / EPSILON;
8-
9-
var roundTiesToEven = function (n) {
10-
return n + INVERSE_EPSILON - INVERSE_EPSILON;
11-
};
128

139
module.exports = function (x, FLOAT_EPSILON, FLOAT_MAX_VALUE, FLOAT_MIN_VALUE) {
1410
var n = +x;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'use strict';
2+
var log = Math.log;
3+
var LN2 = Math.LN2;
4+
5+
// `Math.log2` method
6+
// https://tc39.es/ecma262/#sec-math.log2
7+
// eslint-disable-next-line es/no-math-log2 -- safe
8+
module.exports = Math.log2 || function log2(x) {
9+
return log(x) / LN2;
10+
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict';
2+
var EPSILON = 2.220446049250313e-16; // Number.EPSILON
3+
var INVERSE_EPSILON = 1 / EPSILON;
4+
5+
module.exports = function (n) {
6+
return n + INVERSE_EPSILON - INVERSE_EPSILON;
7+
};
Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
'use strict';
22
var $ = require('../internals/export');
3-
4-
var log = Math.log;
5-
var LN2 = Math.LN2;
3+
var log2 = require('../internals/math-log2');
64

75
// `Math.log2` method
86
// https://tc39.es/ecma262/#sec-math.log2
97
$({ target: 'Math', stat: true }, {
10-
log2: function log2(x) {
11-
return log(x) / LN2;
12-
}
8+
log2: log2
139
});

packages/core-js/modules/esnext.data-view.set-float16.js

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,11 @@ var $ = require('../internals/export');
33
var uncurryThis = require('../internals/function-uncurry-this');
44
var aDataView = require('../internals/a-data-view');
55
var toIndex = require('../internals/to-index');
6+
// TODO: Replace with module dependency in `core-js@4`
7+
var log2 = require('../internals/math-log2');
8+
var roundTiesToEven = require('../internals/math-round-ties-to-even');
69

710
var pow = Math.pow;
8-
var log = Math.log;
9-
var LN2 = Math.LN2;
10-
11-
var EPSILON = 2.220446049250313e-16; // Number.EPSILON
12-
var INVERSE_EPSILON = 1 / EPSILON;
13-
14-
var roundTiesToEven = function (n) {
15-
return n + INVERSE_EPSILON - INVERSE_EPSILON;
16-
};
1711

1812
var MIN_INFINITY16 = 65520; // (2 - 2 ** -11) * 2 ** 15
1913
var MIN_NORMAL16 = 0.000061005353927612305; // (1 - 2 ** -11) * 2 ** -14
@@ -31,7 +25,7 @@ var packFloat16 = function (value) {
3125
if (value < MIN_NORMAL16) return neg << 15 | roundTiesToEven(value * REC_MIN_SUBNORMAL16); // subnormal
3226

3327
// normal
34-
var exponent = log(value) / LN2 | 0;
28+
var exponent = log2(value) | 0;
3529
if (exponent === -15) {
3630
// we round from a value between 2 ** -15 * (1 + 1022/1024) (the largest subnormal) and 2 ** -14 * (1 + 0/1024) (the smallest normal)
3731
// to the latter (former impossible because of the subnormal check above)

0 commit comments

Comments
 (0)