1/*2* Copyright (c) 1999, 2007, Oracle and/or its affiliates. All rights reserved.3* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.4*5* This code is free software; you can redistribute it and/or modify it6* under the terms of the GNU General Public License version 2 only, as7* published by the Free Software Foundation. Oracle designates this8* particular file as subject to the "Classpath" exception as provided9* by Oracle in the LICENSE file that accompanied this code.10*11* This code is distributed in the hope that it will be useful, but WITHOUT12* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or13* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License14* version 2 for more details (a copy is included in the LICENSE file that15* accompanied this code).16*17* You should have received a copy of the GNU General Public License version18* 2 along with this work; if not, write to the Free Software Foundation,19* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.20*21* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA22* or visit www.oracle.com if you need additional information or have any23* questions.24*/25 26packagejava.math; 27 28/**29* A class used to represent multiprecision integers that makes efficient30* use of allocated space by allowing a number to occupy only part of31* an array so that the arrays do not have to be reallocated as often.32* When performing an operation with many iterations the array used to33* hold a number is only increased when necessary and does not have to34* be the same size as the number it represents. A mutable number allows35* calculations to occur on the same number without having to create36* a new number for every step of the calculation as occurs with37* BigIntegers.38*39* Note that SignedMutableBigIntegers only support signed addition and40* subtraction. All other operations occur as with MutableBigIntegers.41*42* @see BigInteger43* @author Michael McCloskey44* @since 1.345*/46 47classSignedMutableBigIntegerextendsMutableBigInteger { 48 49/**50* The sign of this MutableBigInteger.51*/52intsign = 1; 53 54// Constructors55 56/**57* The default constructor. An empty MutableBigInteger is created with58* a one word capacity.59*/60 SignedMutableBigInteger() { 61super(); 62 } 63 64/**65* Construct a new MutableBigInteger with a magnitude specified by66* the int val.67*/68 SignedMutableBigInteger(intval) { 69super(val); 70 } 71 72/**73* Construct a new MutableBigInteger with a magnitude equal to the74* specified MutableBigInteger.75*/76 SignedMutableBigInteger(MutableBigInteger val) { 77super(val); 78 } 79 80// Arithmetic Operations81 82/**83* Signed addition built upon unsigned add and subtract.84*/85voidsignedAdd(SignedMutableBigInteger addend) { 86if(sign == addend.sign) 87 add(addend); 88else89 sign = sign * subtract(addend); 90 91 } 92 93/**94* Signed addition built upon unsigned add and subtract.95*/96voidsignedAdd(MutableBigInteger addend) { 97if(sign == 1) 98 add(addend); 99else100 sign = sign * subtract(addend); 101 102 } 103 104/**105* Signed subtraction built upon unsigned add and subtract.106*/107voidsignedSubtract(SignedMutableBigInteger addend) { 108if(sign == addend.sign) 109 sign = sign * subtract(addend); 110else111 add(addend); 112 113 } 114 115/**116* Signed subtraction built upon unsigned add and subtract.117*/118voidsignedSubtract(MutableBigInteger addend) { 119if(sign == 1) 120 sign = sign * subtract(addend); 121else122 add(addend); 123if(intLen == 0) 124 sign = 1; 125 } 126 127/**128* Print out the first intLen ints of this MutableBigInteger's value129* array starting at offset.130*/131publicString toString() { 132returnthis.toBigInteger(sign).toString(); 133 } 134 135 }