|
1 // ES5 15.1.2.2 step 1 |
|
2 |
|
3 /* |
|
4 * Boundary testing for super-large positive numbers between non-exponential |
|
5 * and in-exponential-form. |
|
6 * |
|
7 * NB: While 1e21 is exactly representable as an IEEE754 double-precision |
|
8 * number, its nearest neighboring representable values are a good distance |
|
9 * away, 65536 to be precise. |
|
10 */ |
|
11 |
|
12 // This is the boundary in theory. |
|
13 assertEq(parseInt(1e21), 1); |
|
14 |
|
15 // This is the boundary in practice. |
|
16 assertEq(parseInt(1e21 - 65537) > 1e20, true); |
|
17 assertEq(parseInt(1e21 - 65536), 1); |
|
18 assertEq(parseInt(1e21 + 65536), 1); |
|
19 |
|
20 // Check that we understand floating point accuracy near the boundary |
|
21 assertEq(1e21 - 65537 !== 1e21 - 65536, true); |
|
22 assertEq(1e21 - 65536, 1e21); |
|
23 assertEq(1e21 + 65535, 1e21); |
|
24 assertEq(1e21 + 65536, 1e21); |
|
25 |
|
26 // ES5 leaves exact precision in ToString(bigMagNum) undefined, which |
|
27 // might make this value inconsistent across implementations (maybe, |
|
28 // nobody's done the math here). Regardless, it's definitely a number |
|
29 // very close to 1, and not a large-magnitude positive number. |
|
30 assertEq(1e21 + 65537 !== 1e21, true); |
|
31 assertEq(parseInt(1e21 + 65537) < 1.001, true); |
|
32 |
|
33 |
|
34 /* |
|
35 * Now do the same tests for super-large negative numbers crossing the |
|
36 * opposite boundary. |
|
37 */ |
|
38 |
|
39 // This is the boundary in theory. |
|
40 assertEq(parseInt(-1e21), -1); |
|
41 |
|
42 // This is the boundary in practice. |
|
43 assertEq(parseInt(-1e21 + 65537) < -1e20, true); |
|
44 assertEq(parseInt(-1e21 + 65536), -1); |
|
45 assertEq(parseInt(-1e21 - 65536), -1); |
|
46 |
|
47 // Check that we understand floating point accuracy near the boundary |
|
48 assertEq(-1e21 + 65537 !== -1e21 + 65536, true); |
|
49 assertEq(-1e21 + 65536, -1e21); |
|
50 assertEq(-1e21 - 65535, -1e21); |
|
51 assertEq(-1e21 - 65536, -1e21); |
|
52 |
|
53 // ES5 leaves exact precision in ToString(bigMagNum) undefined, which |
|
54 // might make this value inconsistent across implementations (maybe, |
|
55 // nobody's done the math here). Regardless, it's definitely a number |
|
56 // very close to -1, and not a large-magnitude negative number. |
|
57 assertEq(-1e21 - 65537 !== 1e21, true); |
|
58 assertEq(parseInt(-1e21 - 65537) > -1.001, true); |
|
59 |
|
60 |
|
61 /* Check values around the boundary. */ |
|
62 arr = [1e0, 5e1, 9e19, 0.1e20, 1.3e20, 1e20, 9e20, 9.99e20, 0.1e21, |
|
63 1e21, 1.0e21, 2e21, 2e20, 2.1e22, 9e21, 0.1e22, 1e22, 3e46, 3e23, 3e100, 3.4e200, 7e1000, |
|
64 1e21, 1e21+65537, 1e21+65536, 1e21-65536, 1e21-65537]; |
|
65 |
|
66 /* Check across a range of values in case we missed anything. */ |
|
67 for (var i = 0; i < 4000; i++) { |
|
68 arr.push(1e19 + i*1e19); |
|
69 } |
|
70 |
|
71 for (var i in arr) { |
|
72 assertEq(parseInt( arr[i]), parseInt(String( arr[i]))); |
|
73 assertEq(parseInt(-arr[i]), parseInt(String(-arr[i]))); |
|
74 } |
|
75 |
|
76 |