1. /*
  2. * @(#)SignedMutableBigInteger.java 1.6 00/02/02
  3. *
  4. * Copyright 1996-2000 Sun Microsystems, Inc. All Rights Reserved.
  5. *
  6. * This software is the proprietary information of Sun Microsystems, Inc.
  7. * Use is subject to license terms.
  8. *
  9. */
  10. package java.math;
  11. /**
  12. * A class used to represent multiprecision integers that makes efficient
  13. * use of allocated space by allowing a number to occupy only part of
  14. * an array so that the arrays do not have to be reallocated as often.
  15. * When performing an operation with many iterations the array used to
  16. * hold a number is only increased when necessary and does not have to
  17. * be the same size as the number it represents. A mutable number allows
  18. * calculations to occur on the same number without having to create
  19. * a new number for every step of the calculation as occurs with
  20. * BigIntegers.
  21. *
  22. * Note that SignedMutableBigIntegers only support signed addition and
  23. * subtraction. All other operations occur as with MutableBigIntegers.
  24. *
  25. * @see BigInteger
  26. * @version 1.6, 02/02/00
  27. * @author Michael McCloskey
  28. * @since 1.3
  29. */
  30. class SignedMutableBigInteger extends MutableBigInteger {
  31. /**
  32. * The sign of this MutableBigInteger.
  33. */
  34. int sign = 1;
  35. // Constructors
  36. /**
  37. * The default constructor. An empty MutableBigInteger is created with
  38. * a one word capacity.
  39. */
  40. SignedMutableBigInteger() {
  41. super();
  42. }
  43. /**
  44. * Construct a new MutableBigInteger with a magnitude specified by
  45. * the int val.
  46. */
  47. SignedMutableBigInteger(int val) {
  48. super(val);
  49. }
  50. /**
  51. * Construct a new MutableBigInteger with a magnitude equal to the
  52. * specified MutableBigInteger.
  53. */
  54. SignedMutableBigInteger(MutableBigInteger val) {
  55. super(val);
  56. }
  57. // Arithmetic Operations
  58. /**
  59. * Signed addition built upon unsigned add and subtract.
  60. */
  61. void signedAdd(SignedMutableBigInteger addend) {
  62. if (sign == addend.sign)
  63. add(addend);
  64. else
  65. sign = sign * subtract(addend);
  66. }
  67. /**
  68. * Signed addition built upon unsigned add and subtract.
  69. */
  70. void signedAdd(MutableBigInteger addend) {
  71. if (sign == 1)
  72. add(addend);
  73. else
  74. sign = sign * subtract(addend);
  75. }
  76. /**
  77. * Signed subtraction built upon unsigned add and subtract.
  78. */
  79. void signedSubtract(SignedMutableBigInteger addend) {
  80. if (sign == addend.sign)
  81. sign = sign * subtract(addend);
  82. else
  83. add(addend);
  84. }
  85. /**
  86. * Signed subtraction built upon unsigned add and subtract.
  87. */
  88. void signedSubtract(MutableBigInteger addend) {
  89. if (sign == 1)
  90. sign = sign * subtract(addend);
  91. else
  92. add(addend);
  93. if (intLen == 0)
  94. sign = 1;
  95. }
  96. /**
  97. * Print out the first intLen ints of this MutableBigInteger's value
  98. * array starting at offset.
  99. */
  100. public String toString() {
  101. BigInteger b = new BigInteger(this, sign);
  102. return
  103. b.toString();
  104. }
  105. }