summaryrefslogtreecommitdiffstats
path: root/js/src/tests/non262/BigInt/Number-conversion-rounding.js
blob: 5daf68948d8c654dea9e07b635a1781dd3143169 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
// Any copyright is dedicated to the Public Domain.
// https://creativecommons.org/licenses/publicdomain/

/**
 * Edge-case behavior at Number.MAX_VALUE and beyond til overflow to Infinity.
 */
function maxMagnitudeTests(isNegative)
{
  var sign = isNegative ? -1 : +1;
  var signBigInt = isNegative ? -1n : 1n;

  const MAX_VALUE = isNegative ? -Number.MAX_VALUE : +Number.MAX_VALUE;

  // 2**971+2**972+...+2**1022+2**1023
  var maxMagnitudeNumber = 0;
  for (let i = 971; i < 1024; i++)
    maxMagnitudeNumber += 2**i;
  maxMagnitudeNumber *= sign;
  assertEq(maxMagnitudeNumber, MAX_VALUE);

  // 2**971+2**972+...+2**1022+2**1023
  var maxMagnitudeNumberAsBigInt = 0n;
  for (let i = 971n; i < 1024n; i++)
    maxMagnitudeNumberAsBigInt += 2n**i;
  maxMagnitudeNumberAsBigInt *= signBigInt;
  var expectedMaxMagnitude = isNegative
                           ? -(2n**1024n) + 2n**971n
                           : 2n**1024n - 2n**971n;
  assertEq(maxMagnitudeNumberAsBigInt, expectedMaxMagnitude);

  // Initial sanity tests.
  assertEq(BigInt(maxMagnitudeNumber), maxMagnitudeNumberAsBigInt);
  assertEq(maxMagnitudeNumber, Number(maxMagnitudeNumberAsBigInt));

  // Test conversion of BigInt values above Number.MAX_VALUE.
  assertEq(Number(maxMagnitudeNumberAsBigInt + signBigInt * 1n), MAX_VALUE);
  assertEq(Number(maxMagnitudeNumberAsBigInt + signBigInt * 2n), MAX_VALUE);
  assertEq(Number(maxMagnitudeNumberAsBigInt + signBigInt * 3n), MAX_VALUE);
  assertEq(Number(maxMagnitudeNumberAsBigInt + signBigInt * 4n), MAX_VALUE);
  assertEq(Number(maxMagnitudeNumberAsBigInt + signBigInt * 5n), MAX_VALUE);
  assertEq(Number(maxMagnitudeNumberAsBigInt + signBigInt * 6n), MAX_VALUE);
  assertEq(Number(maxMagnitudeNumberAsBigInt + signBigInt * 7n), MAX_VALUE);
  assertEq(Number(maxMagnitudeNumberAsBigInt + signBigInt * 8n), MAX_VALUE);
  assertEq(Number(maxMagnitudeNumberAsBigInt + signBigInt * 9n), MAX_VALUE);
  assertEq(Number(maxMagnitudeNumberAsBigInt + signBigInt * 2n**20n), MAX_VALUE);
  assertEq(Number(maxMagnitudeNumberAsBigInt + signBigInt * 2n**400n), MAX_VALUE);
  assertEq(Number(maxMagnitudeNumberAsBigInt + signBigInt * 2n**800n), MAX_VALUE);
  assertEq(Number(maxMagnitudeNumberAsBigInt + signBigInt * 2n**900n), MAX_VALUE);
  assertEq(Number(maxMagnitudeNumberAsBigInt + signBigInt * 2n**969n), MAX_VALUE);

  // For conversion purposes, rounding for values above Number.MAX_VALUE do
  // rounding with respect to Number.MAX_VALUE and 2**1024 (which is treated as
  // the "even" value -- so if the value to convert lies halfway between those two
  // values, 2**1024 is selected).  But if 2**1024 is the value that *would* have
  // been chosen by this process, Infinity is substituted.
  assertEq(Number(maxMagnitudeNumberAsBigInt + signBigInt * (2n**970n - 1n)), MAX_VALUE);
  assertEq(Number(maxMagnitudeNumberAsBigInt + signBigInt * 2n**970n), sign * Infinity);
}
maxMagnitudeTests(false);
maxMagnitudeTests(true);

/**
 * Simple single-Digit on x64, double-Digit on x86 tests.
 */

assertEq(BigInt(Number(2n**53n - 2n)), 2n**53n - 2n);
assertEq(BigInt(Number(2n**53n - 1n)), 2n**53n - 1n);
assertEq(BigInt(Number(2n**53n)), 2n**53n);
assertEq(BigInt(Number(2n**53n + 1n)), 2n**53n);
assertEq(BigInt(Number(2n**53n + 2n)), 2n**53n + 2n);
assertEq(BigInt(Number(2n**53n + 3n)), 2n**53n + 4n);
assertEq(BigInt(Number(2n**53n + 4n)), 2n**53n + 4n);
assertEq(BigInt(Number(2n**53n + 5n)), 2n**53n + 4n);
assertEq(BigInt(Number(2n**53n + 6n)), 2n**53n + 6n);
assertEq(BigInt(Number(2n**53n + 7n)), 2n**53n + 8n);
assertEq(BigInt(Number(2n**53n + 8n)), 2n**53n + 8n);

assertEq(BigInt(Number(2n**54n - 4n)), 2n**54n - 4n);
assertEq(BigInt(Number(2n**54n - 3n)), 2n**54n - 4n);
assertEq(BigInt(Number(2n**54n - 2n)), 2n**54n - 2n);
assertEq(BigInt(Number(2n**54n - 1n)), 2n**54n);
assertEq(BigInt(Number(2n**54n)), 2n**54n);
assertEq(BigInt(Number(2n**54n + 1n)), 2n**54n);
assertEq(BigInt(Number(2n**54n + 2n)), 2n**54n);
assertEq(BigInt(Number(2n**54n + 3n)), 2n**54n + 4n);
assertEq(BigInt(Number(2n**54n + 4n)), 2n**54n + 4n);
assertEq(BigInt(Number(2n**54n + 5n)), 2n**54n + 4n);
assertEq(BigInt(Number(2n**54n + 6n)), 2n**54n + 8n);
assertEq(BigInt(Number(2n**54n + 7n)), 2n**54n + 8n);
assertEq(BigInt(Number(2n**54n + 8n)), 2n**54n + 8n);

assertEq(BigInt(Number(2n**55n - 8n)), 2n**55n - 8n);
assertEq(BigInt(Number(2n**55n - 7n)), 2n**55n - 8n);
assertEq(BigInt(Number(2n**55n - 6n)), 2n**55n - 8n);
assertEq(BigInt(Number(2n**55n - 5n)), 2n**55n - 4n);
assertEq(BigInt(Number(2n**55n - 4n)), 2n**55n - 4n);
assertEq(BigInt(Number(2n**55n - 3n)), 2n**55n - 4n);
assertEq(BigInt(Number(2n**55n - 2n)), 2n**55n);
assertEq(BigInt(Number(2n**55n - 1n)), 2n**55n);
assertEq(BigInt(Number(2n**55n)), 2n**55n);
assertEq(BigInt(Number(2n**55n + 1n)), 2n**55n);
assertEq(BigInt(Number(2n**55n + 2n)), 2n**55n);
assertEq(BigInt(Number(2n**55n + 3n)), 2n**55n);
assertEq(BigInt(Number(2n**55n + 4n)), 2n**55n);
assertEq(BigInt(Number(2n**55n + 5n)), 2n**55n + 8n);
assertEq(BigInt(Number(2n**55n + 6n)), 2n**55n + 8n);
assertEq(BigInt(Number(2n**55n + 7n)), 2n**55n + 8n);
assertEq(BigInt(Number(2n**55n + 8n)), 2n**55n + 8n);
assertEq(BigInt(Number(2n**55n + 9n)), 2n**55n + 8n);
assertEq(BigInt(Number(2n**55n + 10n)), 2n**55n + 8n);
assertEq(BigInt(Number(2n**55n + 11n)), 2n**55n + 8n);
assertEq(BigInt(Number(2n**55n + 12n)), 2n**55n + 16n);
assertEq(BigInt(Number(2n**55n + 13n)), 2n**55n + 16n);
assertEq(BigInt(Number(2n**55n + 14n)), 2n**55n + 16n);
assertEq(BigInt(Number(2n**55n + 15n)), 2n**55n + 16n);
assertEq(BigInt(Number(2n**55n + 16n)), 2n**55n + 16n);


/**
 * Simple double-Digit on x64, triple-Digit on x86 tests.
 */

// The tests below that aren't subtracting bits will have no bits in the
// ultimate significand from the most-significant digit (because of the implicit
// one being excluded).
assertEq(BigInt(Number(2n**64n - 2n**11n)), 2n**64n - 2n**11n);
assertEq(BigInt(Number(2n**64n - 2n**11n + 2n**10n - 1n)), 2n**64n - 2n**11n);
assertEq(BigInt(Number(2n**64n - 2n**11n + 2n**10n)), 2n**64n);
assertEq(BigInt(Number(2n**64n - 2n**10n)), 2n**64n);
assertEq(BigInt(Number(2n**64n)), 2n**64n);
assertEq(BigInt(Number(2n**64n + 1n)), 2n**64n);
assertEq(BigInt(Number(2n**64n + 2n**5n)), 2n**64n);
assertEq(BigInt(Number(2n**64n + 2n**10n)), 2n**64n);
assertEq(BigInt(Number(2n**64n + 2n**11n)), 2n**64n);
assertEq(BigInt(Number(2n**64n + 2n**11n + 1n)), 2n**64n + 2n**12n);
assertEq(BigInt(Number(2n**64n + 2n**12n)), 2n**64n + 2n**12n);
assertEq(BigInt(Number(2n**64n + 2n**12n + 1n)), 2n**64n + 2n**12n);
assertEq(BigInt(Number(2n**64n + 2n**12n + 2n**5n)), 2n**64n + 2n**12n);
assertEq(BigInt(Number(2n**64n + 2n**12n + 2n**10n)), 2n**64n + 2n**12n);
assertEq(BigInt(Number(2n**64n + 2n**12n + 2n**11n - 1n)), 2n**64n + 2n**12n);
assertEq(BigInt(Number(2n**64n + 2n**12n + 2n**11n)), 2n**64n + 2n**13n);
assertEq(BigInt(Number(2n**64n + 2n**12n + 2n**11n + 1n)), 2n**64n + 2n**13n);

// These tests *will* have a bit from the most-significant digit in the ultimate
// significand.
assertEq(BigInt(Number(2n**65n - 2n**12n)), 2n**65n - 2n**12n);
assertEq(BigInt(Number(2n**65n - 2n**12n + 2n**11n - 1n)), 2n**65n - 2n**12n);
assertEq(BigInt(Number(2n**65n - 2n**12n + 2n**11n)), 2n**65n);
assertEq(BigInt(Number(2n**65n - 2n**11n)), 2n**65n);
assertEq(BigInt(Number(2n**65n)), 2n**65n);
assertEq(BigInt(Number(2n**65n + 1n)), 2n**65n);
assertEq(BigInt(Number(2n**65n + 2n**5n)), 2n**65n);
assertEq(BigInt(Number(2n**65n + 2n**11n)), 2n**65n);
assertEq(BigInt(Number(2n**65n + 2n**12n)), 2n**65n);
assertEq(BigInt(Number(2n**65n + 2n**12n + 1n)), 2n**65n + 2n**13n);
assertEq(BigInt(Number(2n**65n + 2n**13n)), 2n**65n + 2n**13n);
assertEq(BigInt(Number(2n**65n + 2n**13n + 1n)), 2n**65n + 2n**13n);
assertEq(BigInt(Number(2n**65n + 2n**13n + 2n**5n)), 2n**65n + 2n**13n);
assertEq(BigInt(Number(2n**65n + 2n**13n + 2n**11n)), 2n**65n + 2n**13n);
assertEq(BigInt(Number(2n**65n + 2n**13n + 2n**12n - 1n)), 2n**65n + 2n**13n);
assertEq(BigInt(Number(2n**65n + 2n**13n + 2n**12n)), 2n**65n + 2n**14n);
assertEq(BigInt(Number(2n**65n + 2n**13n + 2n**12n + 1n)), 2n**65n + 2n**14n);

// ...and in these tests, the contributed bit from the most-significant digit
// is additionally nonzero.
assertEq(BigInt(Number(2n**65n + 2n**64n)), 2n**65n + 2n**64n);
assertEq(BigInt(Number(2n**65n + 2n**64n + 1n)), 2n**65n + 2n**64n);
assertEq(BigInt(Number(2n**65n + 2n**64n + 2n**5n)), 2n**65n + 2n**64n);
assertEq(BigInt(Number(2n**65n + 2n**64n + 2n**11n)), 2n**65n + 2n**64n);
assertEq(BigInt(Number(2n**65n + 2n**64n + 2n**12n)), 2n**65n + 2n**64n);
assertEq(BigInt(Number(2n**65n + 2n**64n + 2n**12n + 1n)), 2n**65n + 2n**64n + 2n**13n);
assertEq(BigInt(Number(2n**65n + 2n**64n + 2n**13n)), 2n**65n + 2n**64n + 2n**13n);
assertEq(BigInt(Number(2n**65n + 2n**64n + 2n**13n + 1n)), 2n**65n + 2n**64n + 2n**13n);
assertEq(BigInt(Number(2n**65n + 2n**64n + 2n**13n + 2n**5n)), 2n**65n + 2n**64n + 2n**13n);
assertEq(BigInt(Number(2n**65n + 2n**64n + 2n**13n + 2n**11n)), 2n**65n + 2n**64n + 2n**13n);
assertEq(BigInt(Number(2n**65n + 2n**64n + 2n**13n + 2n**12n - 1n)), 2n**65n + 2n**64n + 2n**13n);
assertEq(BigInt(Number(2n**65n + 2n**64n + 2n**13n + 2n**12n)), 2n**65n + 2n**64n + 2n**14n);
assertEq(BigInt(Number(2n**65n + 2n**64n + 2n**13n + 2n**12n + 1n)), 2n**65n + 2n**64n + 2n**14n);

/**
 * Versions of the testing above with all the high-order bits massively bumped
 * upward to test that super-low bits, not just bits in high digits, are
 * properly accounted for in rounding.
 */

// The tests below that aren't subtracting bits will have no bits in the
// ultimate significand from the most-significant digit (because of the implicit
// one being excluded).
assertEq(BigInt(Number(2n**940n - 2n**887n + 1n)), 2n**940n - 2n**887n);
assertEq(BigInt(Number(2n**940n - 2n**887n + 2n**886n - 1n)), 2n**940n - 2n**887n);
assertEq(BigInt(Number(2n**940n - 2n**887n + 2n**886n)), 2n**940n);
assertEq(BigInt(Number(2n**940n - 2n**886n)), 2n**940n);
assertEq(BigInt(Number(2n**940n)), 2n**940n);
assertEq(BigInt(Number(2n**940n + 1n)), 2n**940n);
assertEq(BigInt(Number(2n**940n + 2n**880n)), 2n**940n);
assertEq(BigInt(Number(2n**940n + 2n**885n)), 2n**940n);
assertEq(BigInt(Number(2n**940n + 2n**887n)), 2n**940n);
assertEq(BigInt(Number(2n**940n + 2n**887n + 1n)), 2n**940n + 2n**888n);
assertEq(BigInt(Number(2n**940n + 2n**888n)), 2n**940n + 2n**888n);
assertEq(BigInt(Number(2n**940n + 2n**888n + 1n)), 2n**940n + 2n**888n);
assertEq(BigInt(Number(2n**940n + 2n**888n + 2n**5n)), 2n**940n + 2n**888n);
assertEq(BigInt(Number(2n**940n + 2n**888n + 2n**12n)), 2n**940n + 2n**888n);
assertEq(BigInt(Number(2n**940n + 2n**888n + 2n**887n - 1n)), 2n**940n + 2n**888n);
assertEq(BigInt(Number(2n**940n + 2n**888n + 2n**887n)), 2n**940n + 2n**889n);
assertEq(BigInt(Number(2n**940n + 2n**888n + 2n**887n + 1n)), 2n**940n + 2n**889n);

// These tests *will* have a bit from the most-significant digit in the ultimate
// significand.
assertEq(BigInt(Number(2n**941n - 2n**888n)), 2n**941n - 2n**888n);
assertEq(BigInt(Number(2n**941n - 2n**888n + 2n**887n - 1n)), 2n**941n - 2n**888n);
assertEq(BigInt(Number(2n**941n - 2n**888n + 2n**887n)), 2n**941n);
assertEq(BigInt(Number(2n**941n - 2n**887n)), 2n**941n);
assertEq(BigInt(Number(2n**941n)), 2n**941n);
assertEq(BigInt(Number(2n**941n + 1n)), 2n**941n);
assertEq(BigInt(Number(2n**941n + 2n**881n)), 2n**941n);
assertEq(BigInt(Number(2n**941n + 2n**886n)), 2n**941n);
assertEq(BigInt(Number(2n**941n + 2n**888n)), 2n**941n);
assertEq(BigInt(Number(2n**941n + 2n**888n + 1n)), 2n**941n + 2n**889n);
assertEq(BigInt(Number(2n**941n + 2n**889n)), 2n**941n + 2n**889n);
assertEq(BigInt(Number(2n**941n + 2n**889n + 1n)), 2n**941n + 2n**889n);
assertEq(BigInt(Number(2n**941n + 2n**889n + 2n**5n)), 2n**941n + 2n**889n);
assertEq(BigInt(Number(2n**941n + 2n**889n + 2n**12n)), 2n**941n + 2n**889n);
assertEq(BigInt(Number(2n**941n + 2n**889n + 2n**888n - 1n)), 2n**941n + 2n**889n);
assertEq(BigInt(Number(2n**941n + 2n**889n + 2n**888n)), 2n**941n + 2n**890n);
assertEq(BigInt(Number(2n**941n + 2n**889n + 2n**888n + 1n)), 2n**941n + 2n**890n);

// ...and in these tests, the contributed bit from the most-significant digit
// is additionally nonzero.
assertEq(BigInt(Number(2n**941n + 2n**940n)), 2n**941n + 2n**940n);
assertEq(BigInt(Number(2n**941n + 2n**940n + 1n)), 2n**941n + 2n**940n);
assertEq(BigInt(Number(2n**941n + 2n**940n + 2n**881n)), 2n**941n + 2n**940n);
assertEq(BigInt(Number(2n**941n + 2n**940n + 2n**886n)), 2n**941n + 2n**940n);
assertEq(BigInt(Number(2n**941n + 2n**940n + 2n**888n)), 2n**941n + 2n**940n);
assertEq(BigInt(Number(2n**941n + 2n**940n + 2n**888n + 1n)), 2n**941n + 2n**940n + 2n**889n);
assertEq(BigInt(Number(2n**941n + 2n**940n + 2n**889n)), 2n**941n + 2n**940n + 2n**889n);
assertEq(BigInt(Number(2n**941n + 2n**940n + 2n**889n + 1n)), 2n**941n + 2n**940n + 2n**889n);
assertEq(BigInt(Number(2n**941n + 2n**940n + 2n**889n + 2n**5n)), 2n**941n + 2n**940n + 2n**889n);
assertEq(BigInt(Number(2n**941n + 2n**940n + 2n**889n + 2n**12n)), 2n**941n + 2n**940n + 2n**889n);
assertEq(BigInt(Number(2n**941n + 2n**940n + 2n**889n + 2n**888n - 1n)), 2n**941n + 2n**940n + 2n**889n);
assertEq(BigInt(Number(2n**941n + 2n**940n + 2n**889n + 2n**888n)), 2n**941n + 2n**940n + 2n**890n);
assertEq(BigInt(Number(2n**941n + 2n**940n + 2n**889n + 2n**888n + 1n)), 2n**941n + 2n**940n + 2n**890n);

if (typeof reportCompare === "function")
  reportCompare(true, true);