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