1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/src/tests/ecma_6/Math/fround.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,81 @@ 1.4 +/* 1.5 + * Any copyright is dedicated to the Public Domain. 1.6 + * http://creativecommons.org/licenses/publicdomain/ 1.7 + */ 1.8 + 1.9 +// Some tests regarding conversion to Float32 1.10 +assertEq(Math.fround(), NaN); 1.11 + 1.12 +// Special values 1.13 +assertEq(Math.fround(NaN), NaN); 1.14 +assertEq(Math.fround(-Infinity), -Infinity); 1.15 +assertEq(Math.fround(Infinity), Infinity); 1.16 +assertEq(Math.fround(-0), -0); 1.17 +assertEq(Math.fround(+0), +0); 1.18 + 1.19 +// Polyfill function for Float32 conversion 1.20 +var toFloat32 = (function() { 1.21 + var f32 = new Float32Array(1); 1.22 + function f(x) { 1.23 + f32[0] = x; 1.24 + return f32[0]; 1.25 + } 1.26 + return f; 1.27 +})(); 1.28 + 1.29 +// A test on a certain range of numbers, including big numbers, so that 1.30 +// we get a loss in precision for some of them. 1.31 +for (var i = 0; i < 64; ++i) { 1.32 + var p = Math.pow(2, i) + 1; 1.33 + assertEq(Math.fround(p), toFloat32(p)); 1.34 + assertEq(Math.fround(-p), toFloat32(-p)); 1.35 +} 1.36 + 1.37 +/******************************************** 1.38 +/* Tests on maximal Float32 / Double values * 1.39 +/*******************************************/ 1.40 +function maxValue(exponentWidth, significandWidth) { 1.41 + var n = 0; 1.42 + var maxExp = Math.pow(2, exponentWidth - 1) - 1; 1.43 + for (var i = significandWidth; i >= 0; i--) 1.44 + n += Math.pow(2, maxExp - i); 1.45 + return n; 1.46 +} 1.47 + 1.48 +var DBL_MAX = maxValue(11, 52); 1.49 +assertEq(DBL_MAX, Number.MAX_VALUE); // sanity check 1.50 + 1.51 +// Finite as a double, too big for a float 1.52 +assertEq(Math.fround(DBL_MAX), Infinity); 1.53 + 1.54 +var FLT_MAX = maxValue(8, 23); 1.55 +assertEq(Math.fround(FLT_MAX), FLT_MAX); 1.56 +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 1.57 +assertEq(Math.fround(FLT_MAX + Math.pow(2, Math.pow(2, 8 - 1) - 1 - 23 - 1)), Infinity); // no longer rounds down to FLT_MAX 1.58 + 1.59 +/********************************************************* 1.60 +/******* Tests on denormalizations and roundings ********* 1.61 +/********************************************************/ 1.62 + 1.63 +function minValue(exponentWidth, significandWidth) { 1.64 + return Math.pow(2, -(Math.pow(2, exponentWidth - 1) - 2) - significandWidth); 1.65 +} 1.66 + 1.67 +var DBL_MIN = Math.pow(2, -1074); 1.68 +assertEq(DBL_MIN, Number.MIN_VALUE); // sanity check 1.69 + 1.70 +// Too small for a float 1.71 +assertEq(Math.fround(DBL_MIN), 0); 1.72 + 1.73 +var FLT_MIN = minValue(8, 23); 1.74 +assertEq(Math.fround(FLT_MIN), FLT_MIN); 1.75 + 1.76 +assertEq(Math.fround(FLT_MIN / 2), 0); // halfway, round-nearest rounds down to 0 (even) 1.77 +assertEq(Math.fround(FLT_MIN / 2 + Math.pow(2, -202)), FLT_MIN); // first double > FLT_MIN / 2, rounds up to FLT_MIN 1.78 + 1.79 +assertEq(Math.fround(-FLT_MIN), -FLT_MIN); 1.80 + 1.81 +assertEq(Math.fround(-FLT_MIN / 2), -0); // halfway, round-nearest rounds up to -0 (even) 1.82 +assertEq(Math.fround(-FLT_MIN / 2 - Math.pow(2, -202)), -FLT_MIN); // first double < -FLT_MIN / 2, rounds down to -FLT_MIN 1.83 + 1.84 +reportCompare(0, 0, "ok");