diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
commit | 36d22d82aa202bb199967e9512281e9a53db42c9 (patch) | |
tree | 105e8c98ddea1c1e4784a60a5a6410fa416be2de /js/src/tests/non262/Math/fround.js | |
parent | Initial commit. (diff) | |
download | firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.tar.xz firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.zip |
Adding upstream version 115.7.0esr.upstream/115.7.0esrupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'js/src/tests/non262/Math/fround.js')
-rw-r--r-- | js/src/tests/non262/Math/fround.js | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/js/src/tests/non262/Math/fround.js b/js/src/tests/non262/Math/fround.js new file mode 100644 index 0000000000..353f798d5d --- /dev/null +++ b/js/src/tests/non262/Math/fround.js @@ -0,0 +1,81 @@ +/* + * Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/licenses/publicdomain/ + */ + +// Some tests regarding conversion to Float32 +assertEq(Math.fround(), NaN); + +// Special values +assertEq(Math.fround(NaN), NaN); +assertEq(Math.fround(-Infinity), -Infinity); +assertEq(Math.fround(Infinity), Infinity); +assertEq(Math.fround(-0), -0); +assertEq(Math.fround(+0), +0); + +// Polyfill function for Float32 conversion +var toFloat32 = (function() { + var f32 = new Float32Array(1); + function f(x) { + f32[0] = x; + return f32[0]; + } + return f; +})(); + +// A test on a certain range of numbers, including big numbers, so that +// we get a loss in precision for some of them. +for (var i = 0; i < 64; ++i) { + var p = Math.pow(2, i) + 1; + assertEq(Math.fround(p), toFloat32(p)); + assertEq(Math.fround(-p), toFloat32(-p)); +} + +/******************************************** +/* Tests on maximal Float32 / Double values * +/*******************************************/ +function maxValue(exponentWidth, significandWidth) { + var n = 0; + var maxExp = Math.pow(2, exponentWidth - 1) - 1; + for (var i = significandWidth; i >= 0; i--) + n += Math.pow(2, maxExp - i); + return n; +} + +var DBL_MAX = maxValue(11, 52); +assertEq(DBL_MAX, Number.MAX_VALUE); // sanity check + +// Finite as a double, too big for a float +assertEq(Math.fround(DBL_MAX), Infinity); + +var FLT_MAX = maxValue(8, 23); +assertEq(Math.fround(FLT_MAX), FLT_MAX); +assertEq(Math.fround(FLT_MAX + Math.pow(2, Math.pow(2, 8 - 1) - 1 - 23 - 2)), FLT_MAX); // round-nearest rounds down to FLT_MAX +assertEq(Math.fround(FLT_MAX + Math.pow(2, Math.pow(2, 8 - 1) - 1 - 23 - 1)), Infinity); // no longer rounds down to FLT_MAX + +/********************************************************* +/******* Tests on denormalizations and roundings ********* +/********************************************************/ + +function minValue(exponentWidth, significandWidth) { + return Math.pow(2, -(Math.pow(2, exponentWidth - 1) - 2) - significandWidth); +} + +var DBL_MIN = Math.pow(2, -1074); +assertEq(DBL_MIN, Number.MIN_VALUE); // sanity check + +// Too small for a float +assertEq(Math.fround(DBL_MIN), 0); + +var FLT_MIN = minValue(8, 23); +assertEq(Math.fround(FLT_MIN), FLT_MIN); + +assertEq(Math.fround(FLT_MIN / 2), 0); // halfway, round-nearest rounds down to 0 (even) +assertEq(Math.fround(FLT_MIN / 2 + Math.pow(2, -202)), FLT_MIN); // first double > FLT_MIN / 2, rounds up to FLT_MIN + +assertEq(Math.fround(-FLT_MIN), -FLT_MIN); + +assertEq(Math.fround(-FLT_MIN / 2), -0); // halfway, round-nearest rounds up to -0 (even) +assertEq(Math.fround(-FLT_MIN / 2 - Math.pow(2, -202)), -FLT_MIN); // first double < -FLT_MIN / 2, rounds down to -FLT_MIN + +reportCompare(0, 0, "ok"); |