#include "BigInteger.hh" void BigInteger::operator =(const BigInteger &x) { // Calls like a = a have no effect if (this == &x) return; // Copy sign sign = x.sign; // Copy the rest mag = x.mag; } BigInteger::BigInteger(const Blk *b, Index blen, Sign s) : mag(b, blen) { switch (s) { case zero: if (!mag.isZero()) throw "BigInteger::BigInteger(const Blk *, Index, Sign): Cannot use a sign of zero with a nonzero magnitude"; sign = zero; break; case positive: case negative: // If the magnitude is zero, force the sign to zero. sign = mag.isZero() ? zero : s; break; default: /* g++ seems to be optimizing out this case on the assumption * that the sign is a valid member of the enumeration. Oh well. */ throw "BigInteger::BigInteger(const Blk *, Index, Sign): Invalid sign"; } } BigInteger::BigInteger(const BigUnsigned &x, Sign s) : mag(x) { switch (s) { case zero: if (!mag.isZero()) throw "BigInteger::BigInteger(const BigUnsigned &, Sign): Cannot use a sign of zero with a nonzero magnitude"; sign = zero; break; case positive: case negative: // If the magnitude is zero, force the sign to zero. sign = mag.isZero() ? zero : s; break; default: /* g++ seems to be optimizing out this case on the assumption * that the sign is a valid member of the enumeration. Oh well. */ throw "BigInteger::BigInteger(const BigUnsigned &, Sign): Invalid sign"; } } /* CONSTRUCTION FROM PRIMITIVE INTEGERS * Same idea as in BigUnsigned.cc, except that negative input results in a * negative BigInteger instead of an exception. */ // Done longhand to let us use initialization. BigInteger::BigInteger(unsigned long x) : mag(x) { sign = mag.isZero() ? zero : positive; } BigInteger::BigInteger(unsigned int x) : mag(x) { sign = mag.isZero() ? zero : positive; } BigInteger::BigInteger(unsigned short x) : mag(x) { sign = mag.isZero() ? zero : positive; } // For signed input, determine the desired magnitude and sign separately. namespace { template BigInteger::Blk magOf(X x) { /* UX(...) cast needed to stop short(-2^15), which negates to * itself, from sign-extending in the conversion to Blk. */ return BigInteger::Blk(x < 0 ? UX(-x) : x); } template BigInteger::Sign signOf(X x) { return (x == 0) ? BigInteger::zero : (x > 0) ? BigInteger::positive : BigInteger::negative; } } BigInteger::BigInteger(long x) : sign(signOf(x)), mag(magOf(x)) {} BigInteger::BigInteger(int x) : sign(signOf(x)), mag(magOf(x)) {} BigInteger::BigInteger(short x) : sign(signOf(x)), mag(magOf(x)) {} // CONVERSION TO PRIMITIVE INTEGERS /* Reuse BigUnsigned's conversion to an unsigned primitive integer. * The friend is a separate function rather than * BigInteger::convertToUnsignedPrimitive to avoid requiring BigUnsigned to * declare BigInteger. */ template inline X convertBigUnsignedToPrimitiveAccess(const BigUnsigned &a) { return a.convertToPrimitive(); } template X BigInteger::convertToUnsignedPrimitive() const { if (sign == negative) throw "BigInteger::to: " "Cannot convert a negative integer to an unsigned type"; else return convertBigUnsignedToPrimitiveAccess(mag); } /* Similar to BigUnsigned::convertToPrimitive, but split into two cases for * nonnegative and negative numbers. */ template X BigInteger::convertToSignedPrimitive() const { if (sign == zero) return 0; else if (mag.getLength() == 1) { // The single block might fit in an X. Try the conversion. Blk b = mag.getBlock(0); if (sign == positive) { X x = X(b); if (x >= 0 && Blk(x) == b) return x; } else { X x = -X(b); /* UX(...) needed to avoid rejecting conversion of * -2^15 to a short. */ if (x < 0 && Blk(UX(-x)) == b) return x; } // Otherwise fall through. } throw "BigInteger::to: " "Value is too big to fit in the requested type"; } unsigned long BigInteger::toUnsignedLong () const { return convertToUnsignedPrimitive (); } unsigned int BigInteger::toUnsignedInt () const { return convertToUnsignedPrimitive (); } unsigned short BigInteger::toUnsignedShort() const { return convertToUnsignedPrimitive (); } long BigInteger::toLong () const { return convertToSignedPrimitive (); } int BigInteger::toInt () const { return convertToSignedPrimitive (); } short BigInteger::toShort () const { return convertToSignedPrimitive (); } // COMPARISON BigInteger::CmpRes BigInteger::compareTo(const BigInteger &x) const { // A greater sign implies a greater number if (sign < x.sign) return less; else if (sign > x.sign) return greater; else switch (sign) { // If the signs are the same... case zero: return equal; // Two zeros are equal case positive: // Compare the magnitudes return mag.compareTo(x.mag); case negative: // Compare the magnitudes, but return the opposite result return CmpRes(-mag.compareTo(x.mag)); default: throw "BigInteger internal error"; } } /* COPY-LESS OPERATIONS * These do some messing around to determine the sign of the result, * then call one of BigUnsigned's copy-less operations. */ // See remarks about aliased calls in BigUnsigned.cc . #define DTRT_ALIASED(cond, op) \ if (cond) { \ BigInteger tmpThis; \ tmpThis.op; \ *this = tmpThis; \ return; \ } void BigInteger::add(const BigInteger &a, const BigInteger &b) { DTRT_ALIASED(this == &a || this == &b, add(a, b)); // If one argument is zero, copy the other. if (a.sign == zero) operator =(b); else if (b.sign == zero) operator =(a); // If the arguments have the same sign, take the // common sign and add their magnitudes. else if (a.sign == b.sign) { sign = a.sign; mag.add(a.mag, b.mag); } else { // Otherwise, their magnitudes must be compared. switch (a.mag.compareTo(b.mag)) { case equal: // If their magnitudes are the same, copy zero. mag = 0; sign = zero; break; // Otherwise, take the sign of the greater, and subtract // the lesser magnitude from the greater magnitude. case greater: sign = a.sign; mag.subtract(a.mag, b.mag); break; case less: sign = b.sign; mag.subtract(b.mag, a.mag); break; } } } void BigInteger::subtract(const BigInteger &a, const BigInteger &b) { // Notice that this routine is identical to BigInteger::add, // if one replaces b.sign by its opposite. DTRT_ALIASED(this == &a || this == &b, subtract(a, b)); // If a is zero, copy b and flip its sign. If b is zero, copy a. if (a.sign == zero) { mag = b.mag; // Take the negative of _b_'s, sign, not ours. // Bug pointed out by Sam Larkin on 2005.03.30. sign = Sign(-b.sign); } else if (b.sign == zero) operator =(a); // If their signs differ, take a.sign and add the magnitudes. else if (a.sign != b.sign) { sign = a.sign; mag.add(a.mag, b.mag); } else { // Otherwise, their magnitudes must be compared. switch (a.mag.compareTo(b.mag)) { // If their magnitudes are the same, copy zero. case equal: mag = 0; sign = zero; break; // If a's magnitude is greater, take a.sign and // subtract a from b. case greater: sign = a.sign; mag.subtract(a.mag, b.mag); break; // If b's magnitude is greater, take the opposite // of b.sign and subtract b from a. case less: sign = Sign(-b.sign); mag.subtract(b.mag, a.mag); break; } } } void BigInteger::multiply(const BigInteger &a, const BigInteger &b) { DTRT_ALIASED(this == &a || this == &b, multiply(a, b)); // If one object is zero, copy zero and return. if (a.sign == zero || b.sign == zero) { sign = zero; mag = 0; return; } // If the signs of the arguments are the same, the result // is positive, otherwise it is negative. sign = (a.sign == b.sign) ? positive : negative; // Multiply the magnitudes. mag.multiply(a.mag, b.mag); } /* * DIVISION WITH REMAINDER * Please read the comments before the definition of * `BigUnsigned::divideWithRemainder' in `BigUnsigned.cc' for lots of * information you should know before reading this function. * * Following Knuth, I decree that x / y is to be * 0 if y==0 and floor(real-number x / y) if y!=0. * Then x % y shall be x - y*(integer x / y). * * Note that x = y * (x / y) + (x % y) always holds. * In addition, (x % y) is from 0 to y - 1 if y > 0, * and from -(|y| - 1) to 0 if y < 0. (x % y) = x if y = 0. * * Examples: (q = a / b, r = a % b) * a b q r * === === === === * 4 3 1 1 * -4 3 -2 2 * 4 -3 -2 -2 * -4 -3 1 -1 */ void BigInteger::divideWithRemainder(const BigInteger &b, BigInteger &q) { // Defend against aliased calls; // same idea as in BigUnsigned::divideWithRemainder . if (this == &q) throw "BigInteger::divideWithRemainder: Cannot write quotient and remainder into the same variable"; if (this == &b || &q == &b) { BigInteger tmpB(b); divideWithRemainder(tmpB, q); return; } // Division by zero gives quotient 0 and remainder *this if (b.sign == zero) { q.mag = 0; q.sign = zero; return; } // 0 / b gives quotient 0 and remainder 0 if (sign == zero) { q.mag = 0; q.sign = zero; return; } // Here *this != 0, b != 0. // Do the operands have the same sign? if (sign == b.sign) { // Yes: easy case. Quotient is zero or positive. q.sign = positive; } else { // No: harder case. Quotient is negative. q.sign = negative; // Decrease the magnitude of the dividend by one. mag--; /* * We tinker with the dividend before and with the * quotient and remainder after so that the result * comes out right. To see why it works, consider the following * list of examples, where A is the magnitude-decreased * a, Q and R are the results of BigUnsigned division * with remainder on A and |b|, and q and r are the * final results we want: * * a A b Q R q r * -3 -2 3 0 2 -1 0 * -4 -3 3 1 0 -2 2 * -5 -4 3 1 1 -2 1 * -6 -5 3 1 2 -2 0 * * It appears that we need a total of 3 corrections: * Decrease the magnitude of a to get A. Increase the * magnitude of Q to get q (and make it negative). * Find r = (b - 1) - R and give it the desired sign. */ } // Divide the magnitudes. mag.divideWithRemainder(b.mag, q.mag); if (sign != b.sign) { // More for the harder case (as described): // Increase the magnitude of the quotient by one. q.mag++; // Modify the remainder. mag.subtract(b.mag, mag); mag--; } // Sign of the remainder is always the sign of the divisor b. sign = b.sign; // Set signs to zero as necessary. (Thanks David Allen!) if (mag.isZero()) sign = zero; if (q.mag.isZero()) q.sign = zero; // WHEW!!! } // Negation void BigInteger::negate(const BigInteger &a) { DTRT_ALIASED(this == &a, negate(a)); // Copy a's magnitude mag = a.mag; // Copy the opposite of a.sign sign = Sign(-a.sign); } // INCREMENT/DECREMENT OPERATORS // Prefix increment void BigInteger::operator ++() { if (sign == negative) { mag--; if (mag == 0) sign = zero; } else { mag++; sign = positive; // if not already } } // Postfix increment: same as prefix void BigInteger::operator ++(int) { operator ++(); } // Prefix decrement void BigInteger::operator --() { if (sign == positive) { mag--; if (mag == 0) sign = zero; } else { mag++; sign = negative; } } // Postfix decrement: same as prefix void BigInteger::operator --(int) { operator --(); } id='n255' href='#n255'>255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861