-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
44 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
'use strict'; | ||
var sign = require('../internals/math-sign'); | ||
|
||
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; | ||
}; | ||
|
||
// `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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,7 @@ | ||
'use strict'; | ||
var $ = require('../internals/export'); | ||
var IEEE754 = require('../internals/ieee754'); | ||
|
||
var packIEEE754 = IEEE754.pack; | ||
var unpackIEEE754 = IEEE754.unpack; | ||
var $isFinite = isFinite; | ||
var f16round = require('../internals/math-f16round'); | ||
|
||
// `Math.f16round` method | ||
// https://github.com/tc39/proposal-float16array | ||
$({ target: 'Math', stat: true }, { | ||
f16round: function f16round(x) { | ||
var n = +x; | ||
return $isFinite(n) && n !== 0 ? unpackIEEE754(packIEEE754(n, 10, 2), 10) : n; | ||
} | ||
}); | ||
$({ target: 'Math', stat: true }, { f16round: f16round }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters