js/src/tests/ecma_6/Math/fround.js

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 /*
michael@0 2 * Any copyright is dedicated to the Public Domain.
michael@0 3 * http://creativecommons.org/licenses/publicdomain/
michael@0 4 */
michael@0 5
michael@0 6 // Some tests regarding conversion to Float32
michael@0 7 assertEq(Math.fround(), NaN);
michael@0 8
michael@0 9 // Special values
michael@0 10 assertEq(Math.fround(NaN), NaN);
michael@0 11 assertEq(Math.fround(-Infinity), -Infinity);
michael@0 12 assertEq(Math.fround(Infinity), Infinity);
michael@0 13 assertEq(Math.fround(-0), -0);
michael@0 14 assertEq(Math.fround(+0), +0);
michael@0 15
michael@0 16 // Polyfill function for Float32 conversion
michael@0 17 var toFloat32 = (function() {
michael@0 18 var f32 = new Float32Array(1);
michael@0 19 function f(x) {
michael@0 20 f32[0] = x;
michael@0 21 return f32[0];
michael@0 22 }
michael@0 23 return f;
michael@0 24 })();
michael@0 25
michael@0 26 // A test on a certain range of numbers, including big numbers, so that
michael@0 27 // we get a loss in precision for some of them.
michael@0 28 for (var i = 0; i < 64; ++i) {
michael@0 29 var p = Math.pow(2, i) + 1;
michael@0 30 assertEq(Math.fround(p), toFloat32(p));
michael@0 31 assertEq(Math.fround(-p), toFloat32(-p));
michael@0 32 }
michael@0 33
michael@0 34 /********************************************
michael@0 35 /* Tests on maximal Float32 / Double values *
michael@0 36 /*******************************************/
michael@0 37 function maxValue(exponentWidth, significandWidth) {
michael@0 38 var n = 0;
michael@0 39 var maxExp = Math.pow(2, exponentWidth - 1) - 1;
michael@0 40 for (var i = significandWidth; i >= 0; i--)
michael@0 41 n += Math.pow(2, maxExp - i);
michael@0 42 return n;
michael@0 43 }
michael@0 44
michael@0 45 var DBL_MAX = maxValue(11, 52);
michael@0 46 assertEq(DBL_MAX, Number.MAX_VALUE); // sanity check
michael@0 47
michael@0 48 // Finite as a double, too big for a float
michael@0 49 assertEq(Math.fround(DBL_MAX), Infinity);
michael@0 50
michael@0 51 var FLT_MAX = maxValue(8, 23);
michael@0 52 assertEq(Math.fround(FLT_MAX), FLT_MAX);
michael@0 53 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
michael@0 54 assertEq(Math.fround(FLT_MAX + Math.pow(2, Math.pow(2, 8 - 1) - 1 - 23 - 1)), Infinity); // no longer rounds down to FLT_MAX
michael@0 55
michael@0 56 /*********************************************************
michael@0 57 /******* Tests on denormalizations and roundings *********
michael@0 58 /********************************************************/
michael@0 59
michael@0 60 function minValue(exponentWidth, significandWidth) {
michael@0 61 return Math.pow(2, -(Math.pow(2, exponentWidth - 1) - 2) - significandWidth);
michael@0 62 }
michael@0 63
michael@0 64 var DBL_MIN = Math.pow(2, -1074);
michael@0 65 assertEq(DBL_MIN, Number.MIN_VALUE); // sanity check
michael@0 66
michael@0 67 // Too small for a float
michael@0 68 assertEq(Math.fround(DBL_MIN), 0);
michael@0 69
michael@0 70 var FLT_MIN = minValue(8, 23);
michael@0 71 assertEq(Math.fround(FLT_MIN), FLT_MIN);
michael@0 72
michael@0 73 assertEq(Math.fround(FLT_MIN / 2), 0); // halfway, round-nearest rounds down to 0 (even)
michael@0 74 assertEq(Math.fround(FLT_MIN / 2 + Math.pow(2, -202)), FLT_MIN); // first double > FLT_MIN / 2, rounds up to FLT_MIN
michael@0 75
michael@0 76 assertEq(Math.fround(-FLT_MIN), -FLT_MIN);
michael@0 77
michael@0 78 assertEq(Math.fround(-FLT_MIN / 2), -0); // halfway, round-nearest rounds up to -0 (even)
michael@0 79 assertEq(Math.fround(-FLT_MIN / 2 - Math.pow(2, -202)), -FLT_MIN); // first double < -FLT_MIN / 2, rounds down to -FLT_MIN
michael@0 80
michael@0 81 reportCompare(0, 0, "ok");

mercurial