KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > math > SignedMutableBigInteger


1 /*
2  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
3  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
4  */

5
6 /*
7  * @(#)SignedMutableBigInteger.java 1.9 03/12/19
8  */

9
10 package java.math;
11
12 /**
13  * A class used to represent multiprecision integers that makes efficient
14  * use of allocated space by allowing a number to occupy only part of
15  * an array so that the arrays do not have to be reallocated as often.
16  * When performing an operation with many iterations the array used to
17  * hold a number is only increased when necessary and does not have to
18  * be the same size as the number it represents. A mutable number allows
19  * calculations to occur on the same number without having to create
20  * a new number for every step of the calculation as occurs with
21  * BigIntegers.
22  *
23  * Note that SignedMutableBigIntegers only support signed addition and
24  * subtraction. All other operations occur as with MutableBigIntegers.
25  *
26  * @see BigInteger
27  * @version 1.9, 12/19/03
28  * @author Michael McCloskey
29  * @since 1.3
30  */

31
32 class SignedMutableBigInteger extends MutableBigInteger JavaDoc {
33
34    /**
35      * The sign of this MutableBigInteger.
36      */

37     int sign = 1;
38
39     // Constructors
40

41     /**
42      * The default constructor. An empty MutableBigInteger is created with
43      * a one word capacity.
44      */

45     SignedMutableBigInteger() {
46         super();
47     }
48
49     /**
50      * Construct a new MutableBigInteger with a magnitude specified by
51      * the int val.
52      */

53     SignedMutableBigInteger(int val) {
54         super(val);
55     }
56
57     /**
58      * Construct a new MutableBigInteger with a magnitude equal to the
59      * specified MutableBigInteger.
60      */

61     SignedMutableBigInteger(MutableBigInteger JavaDoc val) {
62         super(val);
63     }
64
65    // Arithmetic Operations
66

67    /**
68      * Signed addition built upon unsigned add and subtract.
69      */

70     void signedAdd(SignedMutableBigInteger JavaDoc addend) {
71         if (sign == addend.sign)
72             add(addend);
73         else
74             sign = sign * subtract(addend);
75
76     }
77
78    /**
79      * Signed addition built upon unsigned add and subtract.
80      */

81     void signedAdd(MutableBigInteger JavaDoc addend) {
82         if (sign == 1)
83             add(addend);
84         else
85             sign = sign * subtract(addend);
86         
87     }
88
89    /**
90      * Signed subtraction built upon unsigned add and subtract.
91      */

92     void signedSubtract(SignedMutableBigInteger JavaDoc addend) {
93         if (sign == addend.sign)
94             sign = sign * subtract(addend);
95         else
96             add(addend);
97         
98     }
99
100    /**
101      * Signed subtraction built upon unsigned add and subtract.
102      */

103     void signedSubtract(MutableBigInteger JavaDoc addend) {
104         if (sign == 1)
105             sign = sign * subtract(addend);
106         else
107             add(addend);
108         if (intLen == 0)
109              sign = 1;
110     }
111
112     /**
113      * Print out the first intLen ints of this MutableBigInteger's value
114      * array starting at offset.
115      */

116     public String JavaDoc toString() {
117         BigInteger JavaDoc b = new BigInteger JavaDoc(this, sign);
118         return
119             b.toString();
120     }
121
122 }
123
Popular Tags