Commit | Line | Data |
---|---|---|
e67d6049 MM |
1 | /* |
2 | * Matt McCutchen's Big Integer Library | |
05780f4b | 3 | * http://mysite.verizon.net/mccutchen/bigint/ |
e67d6049 MM |
4 | */ |
5 | ||
05780f4b | 6 | #include "BigInteger.hh" |
e67d6049 MM |
7 | |
8 | // MANAGEMENT | |
9 | ||
10 | // Assignment operator | |
11 | void BigInteger::operator =(const BigInteger &x) { | |
12 | // Calls like a = a have no effect | |
13 | if (this == &x) | |
14 | return; | |
15 | // Copy sign | |
16 | sign = x.sign; | |
17 | // Copy the rest | |
18 | BigUnsigned::operator =(x); | |
19 | } | |
20 | ||
21 | // Constructor from an array of blocks and a sign | |
05780f4b | 22 | BigInteger::BigInteger(const Blk *b, Index l, Sign s) : BigUnsigned(b, l) { |
e67d6049 MM |
23 | switch (s) { |
24 | case zero: | |
25 | case positive: | |
26 | case negative: | |
27 | sign = (len == 0) ? zero : s; | |
28 | break; | |
29 | default: | |
05780f4b | 30 | throw "BigInteger::BigInteger(Blk *, Index, Sign): Invalid sign"; |
e67d6049 MM |
31 | } |
32 | } | |
33 | ||
34 | // Constructor from a BigUnsigned and a sign | |
35 | BigInteger::BigInteger(const BigUnsigned &x, Sign s) : BigUnsigned(x) { | |
36 | switch (s) { | |
37 | case zero: | |
38 | case positive: | |
39 | case negative: | |
40 | sign = (len == 0) ? zero : s; | |
41 | break; | |
42 | default: | |
05780f4b | 43 | throw "BigInteger::BigInteger(Blk *, Index, Sign): Invalid sign"; |
e67d6049 MM |
44 | } |
45 | } | |
46 | ||
47 | /* | |
48 | * The steps for construction of a BigInteger | |
49 | * from an integral value x are as follows: | |
50 | * 1. If x is zero, create an empty BigInteger and stop. | |
51 | * 2. Allocate a one-block number array. | |
52 | * 3. If x is positive (or of an unsigned type), set the | |
53 | * sign of the BigInteger to positive. | |
54 | * 4. If x is of a signed type and is negative, set the | |
55 | * sign of the BigInteger to negative. | |
56 | * 5. If x is of a signed type, convert x (or -x if x < 0) | |
57 | * to the unsigned type of the same length. | |
58 | * 6. Expand x (or the result of step 5) to a Blk, | |
59 | * and store it in the number array. | |
b3fe29df MM |
60 | * |
61 | * See remarks in `BigUnsigned.cc' and `NumberlikeArray.hh' | |
62 | * about new handling of zero-length arrays. | |
e67d6049 MM |
63 | */ |
64 | ||
65 | BigInteger::BigInteger(unsigned long x) { | |
b3fe29df MM |
66 | if (x == 0) |
67 | sign = zero; // NumberlikeArray did the rest | |
68 | else { | |
e67d6049 | 69 | cap = 1; |
a8b42b68 | 70 | blk = new Blk[1]; |
e67d6049 MM |
71 | sign = positive; |
72 | len = 1; | |
2f145f11 | 73 | blk[0] = Blk(x); |
e67d6049 MM |
74 | } |
75 | } | |
76 | ||
77 | BigInteger::BigInteger(long x) { | |
78 | if (x > 0) { | |
79 | cap = 1; | |
a8b42b68 | 80 | blk = new Blk[1]; |
e67d6049 MM |
81 | sign = positive; |
82 | len = 1; | |
2f145f11 | 83 | blk[0] = Blk(x); |
e67d6049 MM |
84 | } else if (x < 0) { |
85 | cap = 1; | |
a8b42b68 | 86 | blk = new Blk[1]; |
e67d6049 MM |
87 | sign = negative; |
88 | len = 1; | |
2f145f11 | 89 | blk[0] = Blk(-x); |
b3fe29df MM |
90 | } else |
91 | sign = zero; | |
e67d6049 MM |
92 | } |
93 | ||
94 | BigInteger::BigInteger(unsigned int x) { | |
b3fe29df | 95 | if (x == 0) |
e67d6049 | 96 | sign = zero; |
b3fe29df | 97 | else { |
e67d6049 | 98 | cap = 1; |
a8b42b68 | 99 | blk = new Blk[1]; |
e67d6049 MM |
100 | sign = positive; |
101 | len = 1; | |
2f145f11 | 102 | blk[0] = Blk(x); |
e67d6049 MM |
103 | } |
104 | } | |
105 | ||
106 | BigInteger::BigInteger(int x) { | |
107 | if (x > 0) { | |
108 | cap = 1; | |
a8b42b68 | 109 | blk = new Blk[1]; |
e67d6049 MM |
110 | sign = positive; |
111 | len = 1; | |
2f145f11 | 112 | blk[0] = Blk(x); |
e67d6049 MM |
113 | } else if (x < 0) { |
114 | cap = 1; | |
a8b42b68 | 115 | blk = new Blk[1]; |
e67d6049 MM |
116 | sign = negative; |
117 | len = 1; | |
2f145f11 | 118 | blk[0] = Blk(-x); |
b3fe29df MM |
119 | } else |
120 | sign = zero; | |
e67d6049 MM |
121 | } |
122 | ||
123 | BigInteger::BigInteger(unsigned short x) { | |
b3fe29df | 124 | if (x == 0) |
e67d6049 | 125 | sign = zero; |
b3fe29df | 126 | else { |
e67d6049 | 127 | cap = 1; |
a8b42b68 | 128 | blk = new Blk[1]; |
e67d6049 MM |
129 | sign = positive; |
130 | len = 1; | |
2f145f11 | 131 | blk[0] = Blk(x); |
e67d6049 MM |
132 | } |
133 | } | |
134 | ||
135 | BigInteger::BigInteger(short x) { | |
136 | if (x > 0) { | |
137 | cap = 1; | |
a8b42b68 | 138 | blk = new Blk[1]; |
e67d6049 MM |
139 | sign = positive; |
140 | len = 1; | |
2f145f11 | 141 | blk[0] = Blk(x); |
e67d6049 MM |
142 | } else if (x < 0) { |
143 | cap = 1; | |
a8b42b68 | 144 | blk = new Blk[1]; |
e67d6049 MM |
145 | sign = negative; |
146 | len = 1; | |
2f145f11 | 147 | blk[0] = Blk(-x); |
b3fe29df MM |
148 | } else |
149 | sign = zero; | |
e67d6049 MM |
150 | } |
151 | ||
152 | // CONVERTERS | |
153 | /* | |
154 | * The steps for conversion of a BigInteger to an | |
155 | * integral type are as follows: | |
156 | * 1. If the BigInteger is zero, return zero. | |
157 | * 2. If the BigInteger is positive: | |
158 | * 3. If it is more than one block long or its lowest | |
159 | * block has bits set out of the range of the target | |
160 | * type, throw an exception. | |
161 | * 4. Otherwise, convert the lowest block to the | |
162 | * target type and return it. | |
163 | * 5. If the BigInteger is negative: | |
164 | * 6. If the target type is unsigned, throw an exception. | |
165 | * 7. If it is more than one block long or its lowest | |
166 | * block has bits set out of the range of the target | |
167 | * type, throw an exception. | |
168 | * 8. Otherwise, convert the lowest block to the | |
169 | * target type, negate it, and return it. | |
170 | */ | |
171 | ||
172 | namespace { | |
173 | // These masks are used to test whether a Blk has bits | |
174 | // set out of the range of a smaller integral type. Note | |
175 | // that this range is not considered to include the sign bit. | |
176 | const BigUnsigned::Blk lMask = ~0 >> 1; | |
177 | const BigUnsigned::Blk uiMask = (unsigned int)(~0); | |
178 | const BigUnsigned::Blk iMask = uiMask >> 1; | |
179 | const BigUnsigned::Blk usMask = (unsigned short)(~0); | |
180 | const BigUnsigned::Blk sMask = usMask >> 1; | |
181 | } | |
182 | ||
183 | BigInteger::operator unsigned long() const { | |
184 | switch (sign) { | |
185 | case zero: | |
186 | return 0; | |
187 | case positive: | |
188 | if (len == 1) | |
2f145f11 | 189 | return blk[0]; |
e67d6049 MM |
190 | else |
191 | throw "BigInteger operator unsigned long() const: Value is too big for an unsigned long"; | |
192 | case negative: | |
193 | throw "BigInteger operator unsigned long() const: Cannot convert a negative integer to an unsigned type"; | |
194 | default: | |
195 | throw "BigInteger: Internal error"; | |
196 | } | |
197 | } | |
198 | ||
199 | BigInteger::operator long() const { | |
200 | switch (sign) { | |
201 | case zero: | |
202 | return 0; | |
203 | case positive: | |
2f145f11 MM |
204 | if (len == 1 && (blk[0] & ~lMask) == 0) |
205 | return long(blk[0]); | |
e67d6049 MM |
206 | else |
207 | throw "BigInteger operator long() const: Value is too big for a long"; | |
208 | case negative: | |
2f145f11 MM |
209 | if (len == 1 && (blk[0] & ~lMask) == 0) |
210 | return -long(blk[0]); | |
e67d6049 MM |
211 | else |
212 | throw "BigInteger operator long() const: Value is too big for a long"; | |
213 | default: | |
214 | throw "BigInteger: Internal error"; | |
215 | } | |
216 | } | |
217 | ||
218 | BigInteger::operator unsigned int() const { | |
219 | switch (sign) { | |
220 | case zero: | |
221 | return 0; | |
222 | case positive: | |
2f145f11 MM |
223 | if (len == 1 && (blk[0] & ~uiMask) == 0) |
224 | return (unsigned int)(blk[0]); | |
e67d6049 MM |
225 | else |
226 | throw "BigInteger operator unsigned int() const: Value is too big for an unsigned int"; | |
227 | case negative: | |
228 | throw "BigInteger operator unsigned int() const: Cannot convert a negative integer to an unsigned type"; | |
229 | default: | |
230 | throw "BigInteger: Internal error"; | |
231 | } | |
232 | } | |
233 | ||
234 | BigInteger::operator int() const { | |
235 | switch (sign) { | |
236 | case zero: | |
237 | return 0; | |
238 | case positive: | |
2f145f11 MM |
239 | if (len == 1 && (blk[0] & ~iMask) == 0) |
240 | return int(blk[0]); | |
e67d6049 MM |
241 | else |
242 | throw "BigInteger operator int() const: Value is too big for an int"; | |
243 | case negative: | |
2f145f11 MM |
244 | if (len == 1 && (blk[0] & ~iMask) == 0) |
245 | return -int(blk[0]); | |
e67d6049 MM |
246 | else |
247 | throw "BigInteger operator int() const: Value is too big for an int"; | |
248 | default: | |
249 | throw "BigInteger: Internal error"; | |
250 | } | |
251 | } | |
252 | ||
253 | BigInteger::operator unsigned short() const { | |
254 | switch (sign) { | |
255 | case zero: | |
256 | return 0; | |
257 | case positive: | |
2f145f11 MM |
258 | if (len == 1 && (blk[0] & ~usMask) == 0) |
259 | return (unsigned short)(blk[0]); | |
e67d6049 MM |
260 | else |
261 | throw "BigInteger operator unsigned short() const: Value is too big for an unsigned short"; | |
262 | case negative: | |
263 | throw "BigInteger operator unsigned short() const: Cannot convert a negative integer to an unsigned type"; | |
264 | default: | |
265 | throw "BigInteger: Internal error"; | |
266 | } | |
267 | } | |
268 | ||
269 | BigInteger::operator short() const { | |
270 | switch (sign) { | |
271 | case zero: | |
272 | return 0; | |
273 | case positive: | |
2f145f11 MM |
274 | if (len == 1 && (blk[0] & ~sMask) == 0) |
275 | return short(blk[0]); | |
e67d6049 MM |
276 | else |
277 | throw "BigInteger operator short() const: Value is too big for a short"; | |
278 | case negative: | |
2f145f11 MM |
279 | if (len == 1 && (blk[0] & ~sMask) == 0) |
280 | return -short(blk[0]); | |
e67d6049 MM |
281 | else |
282 | throw "BigInteger operator short() const: Value is too big for a short"; | |
283 | default: | |
284 | throw "BigInteger: Internal error"; | |
285 | } | |
286 | } | |
287 | ||
288 | // COMPARISON | |
289 | BigInteger::CmpRes BigInteger::compareTo(const BigInteger &x) const { | |
290 | // A greater sign implies a greater number | |
291 | if (sign < x.sign) | |
292 | return less; | |
293 | else if (sign > x.sign) | |
294 | return greater; | |
295 | else switch (sign) { | |
296 | // If the signs are the same... | |
297 | case zero: | |
298 | return equal; // Two zeros are equal | |
299 | case positive: | |
300 | // Compare the magnitudes | |
301 | return BigUnsigned::compareTo(x); | |
302 | case negative: | |
303 | // Compare the magnitudes, but return the opposite result | |
304 | return CmpRes(-BigUnsigned::compareTo(x)); | |
305 | default: | |
306 | throw "BigInteger: Internal error"; | |
307 | } | |
308 | } | |
309 | ||
310 | // PUT-HERE OPERATIONS | |
311 | // These do some messing around to determine the sign of the result, | |
312 | // then call one of BigUnsigned's put-heres. | |
313 | ||
314 | // Addition | |
315 | void BigInteger::add(const BigInteger &a, const BigInteger &b) { | |
316 | // Block unsafe calls | |
317 | if (this == &a || this == &b) | |
318 | throw "BigInteger::add: One of the arguments is the invoked object"; | |
319 | // If one argument is zero, copy the other. | |
320 | if (a.sign == zero) | |
321 | operator =(b); | |
322 | else if (b.sign == zero) | |
323 | operator =(a); | |
324 | // If the arguments have the same sign, take the | |
325 | // common sign and add their magnitudes. | |
326 | else if (a.sign == b.sign) { | |
327 | sign = a.sign; | |
328 | BigUnsigned::add(a, b); | |
329 | } else { | |
330 | // Otherwise, their magnitudes must be compared. | |
331 | switch (a.BigUnsigned::compareTo(b)) { | |
332 | // If their magnitudes are the same, copy zero. | |
333 | case equal: | |
334 | len = 0; | |
335 | sign = zero; | |
336 | break; | |
337 | // Otherwise, take the sign of the greater, and subtract | |
338 | // the lesser magnitude from the greater magnitude. | |
339 | case greater: | |
340 | sign = a.sign; | |
341 | BigUnsigned::subtract(a, b); | |
342 | break; | |
343 | case less: | |
344 | sign = b.sign; | |
345 | BigUnsigned::subtract(b, a); | |
346 | break; | |
347 | } | |
348 | } | |
349 | } | |
350 | ||
351 | // Subtraction | |
352 | void BigInteger::subtract(const BigInteger &a, const BigInteger &b) { | |
353 | // Notice that this routine is identical to BigInteger::add, | |
354 | // if one replaces b.sign by its opposite. | |
355 | // Block unsafe calls | |
356 | if (this == &a || this == &b) | |
357 | throw "BigInteger::subtract: One of the arguments is the invoked object"; | |
358 | // If a is zero, copy b and flip its sign. If b is zero, copy a. | |
359 | if (a.sign == zero) { | |
360 | BigUnsigned::operator =(b); | |
f316def7 MM |
361 | // Take the negative of _b_'s, sign, not ours. |
362 | // Bug pointed out by Sam Larkin on 2005.03.30. | |
363 | sign = Sign(-b.sign); | |
e67d6049 | 364 | } else if (b.sign == zero) |
f316def7 | 365 | operator =(a); |
e67d6049 MM |
366 | // If their signs differ, take a.sign and add the magnitudes. |
367 | else if (a.sign != b.sign) { | |
368 | sign = a.sign; | |
369 | BigUnsigned::add(a, b); | |
370 | } else { | |
371 | // Otherwise, their magnitudes must be compared. | |
372 | switch (a.BigUnsigned::compareTo(b)) { | |
373 | // If their magnitudes are the same, copy zero. | |
374 | case equal: | |
375 | len = 0; | |
376 | sign = zero; | |
377 | break; | |
378 | // If a's magnitude is greater, take a.sign and | |
379 | // subtract a from b. | |
380 | case greater: | |
381 | sign = a.sign; | |
382 | BigUnsigned::subtract(a, b); | |
383 | break; | |
384 | // If b's magnitude is greater, take the opposite | |
385 | // of b.sign and subtract b from a. | |
386 | case less: | |
387 | sign = Sign(-b.sign); | |
388 | BigUnsigned::subtract(b, a); | |
389 | break; | |
390 | } | |
391 | } | |
392 | } | |
393 | ||
394 | // Multiplication | |
395 | void BigInteger::multiply(const BigInteger &a, const BigInteger &b) { | |
396 | // Block unsafe calls | |
397 | if (this == &a || this == &b) | |
398 | throw "BigInteger::multiply: One of the arguments is the invoked object"; | |
399 | // If one object is zero, copy zero and return. | |
400 | if (a.sign == zero || b.sign == zero) { | |
401 | sign = zero; | |
402 | len = 0; | |
403 | return; | |
404 | } | |
405 | // If the signs of the arguments are the same, the result | |
406 | // is positive, otherwise it is negative. | |
407 | sign = (a.sign == b.sign) ? positive : negative; | |
408 | // Multiply the magnitudes. | |
409 | BigUnsigned::multiply(a, b); | |
410 | } | |
411 | ||
05780f4b MM |
412 | /* |
413 | * DIVISION WITH REMAINDER | |
414 | * Please read the comments before the definition of | |
415 | * `BigUnsigned::divideWithRemainder' in `BigUnsigned.cc' for lots of | |
416 | * information you should know before reading this function. | |
417 | * | |
418 | * Following Knuth, I decree that x / y is to be | |
419 | * 0 if y==0 and floor(real-number x / y) if y!=0. | |
420 | * Then x % y shall be x - y*(integer x / y). | |
421 | * | |
422 | * Note that x = y * (x / y) + (x % y) always holds. | |
423 | * In addition, (x % y) is from 0 to y - 1 if y > 0, | |
424 | * and from -(|y| - 1) to 0 if y < 0. (x % y) = x if y = 0. | |
425 | * | |
426 | * Examples: (q = a / b, r = a % b) | |
427 | * a b q r | |
428 | * === === === === | |
429 | * 4 3 1 1 | |
430 | * -4 3 -2 2 | |
431 | * 4 -3 -2 -2 | |
432 | * -4 -3 1 -1 | |
433 | */ | |
434 | void BigInteger::divideWithRemainder(const BigInteger &b, BigInteger &q) { | |
e67d6049 | 435 | // Block unsafe calls |
05780f4b MM |
436 | if (this == &b || this == &q || &b == &q) |
437 | throw "BigInteger::divideWithRemainder: One of the arguments is the invoked object"; | |
438 | // Division by zero gives quotient 0 and remainder *this | |
439 | if (b.sign == zero) { | |
440 | q.len = 0; | |
441 | q.sign = zero; | |
e67d6049 MM |
442 | return; |
443 | } | |
05780f4b MM |
444 | // 0 / b gives quotient 0 and remainder 0 |
445 | if (sign == zero) { | |
446 | q.len = 0; | |
447 | q.sign = zero; | |
e67d6049 MM |
448 | return; |
449 | } | |
05780f4b MM |
450 | |
451 | // Here *this != 0, b != 0. | |
452 | ||
453 | // Do the operands have the same sign? | |
454 | if (sign == b.sign) { | |
455 | // Yes: easy case. Quotient is zero or positive. | |
456 | q.sign = positive; | |
457 | } else { | |
458 | // No: harder case. Quotient is negative. | |
459 | q.sign = negative; | |
460 | // Decrease the magnitude of the dividend by one. | |
461 | BigUnsigned::operator --(); | |
462 | /* | |
463 | * We tinker with the dividend before and with the | |
464 | * quotient and remainder after so that the result | |
465 | * comes out right. To see why it works, consider the following | |
466 | * list of examples, where A is the magnitude-decreased | |
467 | * a, Q and R are the results of BigUnsigned division | |
468 | * with remainder on A and |b|, and q and r are the | |
469 | * final results we want: | |
470 | * | |
471 | * a A b Q R q r | |
472 | * -3 -2 3 0 2 -1 0 | |
473 | * -4 -3 3 1 0 -2 2 | |
474 | * -5 -4 3 1 1 -2 1 | |
475 | * -6 -5 3 1 2 -2 0 | |
476 | * | |
477 | * It appears that we need a total of 3 corrections: | |
478 | * Decrease the magnitude of a to get A. Increase the | |
479 | * magnitude of Q to get q (and make it negative). | |
480 | * Find r = (b - 1) - R and give it the desired sign. | |
481 | */ | |
482 | } | |
483 | ||
484 | // Divide the magnitudes. | |
485 | BigUnsigned::divideWithRemainder(b, q); | |
486 | ||
487 | if (sign != b.sign) { | |
488 | // More for the harder case (as described): | |
489 | // Increase the magnitude of the quotient by one. | |
490 | q.BigUnsigned::operator ++(); | |
491 | // Modify the remainder. | |
492 | BigUnsigned temp(*this); | |
493 | BigUnsigned::subtract(b, temp); | |
494 | BigUnsigned::operator --(); | |
495 | } | |
496 | ||
497 | // Sign of the remainder is always the sign of the divisor b. | |
498 | sign = b.sign; | |
499 | ||
500 | // Set signs to zero as necessary. (Thanks David Allen!) | |
e67d6049 MM |
501 | if (len == 0) |
502 | sign = zero; | |
05780f4b MM |
503 | if (q.len == 0) |
504 | q.sign = zero; | |
505 | ||
506 | // WHEW!!! | |
e67d6049 MM |
507 | } |
508 | ||
509 | // Negation | |
510 | void BigInteger::negate(const BigInteger &a) { | |
511 | // Block unsafe calls | |
512 | if (this == &a) | |
513 | throw "BigInteger::negate: The argument is the invoked object"; | |
514 | // Copy a's magnitude | |
515 | BigUnsigned::operator =(a); | |
516 | // Copy the opposite of a.sign | |
517 | sign = Sign(-a.sign); | |
518 | } | |
519 | ||
520 | // INCREMENT/DECREMENT OPERATORS | |
521 | ||
522 | // Prefix increment | |
523 | void BigInteger::operator ++() { | |
524 | switch (sign) { | |
525 | case zero: | |
526 | allocate(1); | |
527 | sign = positive; | |
528 | len = 1; | |
2f145f11 | 529 | blk[0] = 1; |
e67d6049 MM |
530 | break; |
531 | case positive: | |
532 | BigUnsigned::operator ++(); | |
533 | break; | |
534 | case negative: | |
535 | BigUnsigned::operator --(); | |
536 | if (len == 0) | |
537 | sign = zero; | |
538 | break; | |
539 | } | |
540 | } | |
541 | ||
542 | // Postfix increment: same as prefix | |
543 | void BigInteger::operator ++(int) { | |
544 | operator ++(); | |
545 | } | |
546 | ||
547 | // Prefix decrement | |
548 | void BigInteger::operator --() { | |
549 | switch (sign) { | |
550 | case zero: | |
551 | allocate(1); | |
552 | sign = negative; | |
553 | len = 1; | |
2f145f11 | 554 | blk[0] = 1; |
e67d6049 MM |
555 | break; |
556 | case negative: | |
557 | BigUnsigned::operator ++(); | |
558 | break; | |
559 | case positive: | |
560 | BigUnsigned::operator --(); | |
561 | if (len == 0) | |
562 | sign = zero; | |
563 | break; | |
564 | } | |
565 | } | |
566 | ||
567 | // Postfix decrement: same as prefix | |
568 | void BigInteger::operator --(int) { | |
569 | operator --(); | |
570 | } | |
571 |