/*************************************************************************** * Copyright (C) 2005 by Jeff Ferr * * root@sat * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * a***************************************************************************/ #ifndef J_BIGINTEGER_H #define J_BIGINTEGER_H #include "jobject.h" #include #include namespace jmath { /** * \brief Immutable arbitrary-precision integers. * Additionally, BigInteger provides operations for modular arithmetic, GCD * calculation, primality testing, prime generation, bit manipulation, * and a few other miscellaneous operations. * Semantics of shift operations extend those of shift operators * to allow for negative shift distances. A right-shift with a negative * shift distance results in a left shift, and vice-versa. The unsigned * right shift operator (>>>) is omitted, as this operation makes * little sense in combination with the "infinite word size" abstraction * provided by this class. * Bit operations operate on a single bit of the two's-complement * representation of their operand. If necessary, the operand is sign- * extended so that it contains the designated bit. None of the single-bit * operations can produce a BigInteger with a different sign from the * BigInteger being operated on, as they affect only a single bit, and the * "infinite word size" abstraction provided by this class ensures that there * are infinitely many "virtual sign bits" preceding each BigInteger. * * \author Jeff Ferr */ class BigInteger : public virtual jcommon::Object{ private: /** * \brief The signum of this BigInteger: -1 for negative, 0 for zero, or * 1 for positive. Note that the BigInteger zero must have * a signum of 0. This is necessary to ensures that there is exactly one * representation for each BigInteger value. */ int signum; /** * \brief The magnitude of this BigInteger, in big-endian order: the * zeroth element of this array is the most-significant int of the * magnitude. The magnitude must be "minimal" in that the most-significant * int (mag[0]) must be non-zero. This is necessary to * ensure that there is exactly one representation for each BigInteger * value. Note that this implies that the BigInteger zero has a * zero-length mag array. */ int *mag; /** * \brief The bitCount of this BigInteger, as returned by bitCount(), or -1 * (either value is acceptable). */ int bitCount = -1; /** * \brief The bitLength of this BigInteger, as returned by bitLength(), or -1 * (either value is acceptable). */ int bitLength = -1; /** * \brief The lowest set bit of this BigInteger, as returned by getLowestSetBit(), * or -2 (either value is acceptable). */ int lowestSetBit = -2; /** * \brief The index of the lowest-order byte in the magnitude of this BigInteger * that contains a nonzero byte, or -2 (either value is acceptable). The * least significant byte has int-number 0, the next byte in order of * increasing significance has byte-number 1, and so forth. */ int firstNonzeroByteNum = -2; /** * \brief The index of the lowest-order int in the magnitude of this BigInteger * that contains a nonzero int, or -2 (either value is acceptable). The * least significant int has int-number 0, the next int in order of * increasing significance has int-number 1, and so forth. */ int firstNonzeroIntNum = -2; /** \brief This mask is used to obtain the value of an int as if it were unsigned. */ const static long LONG_MASK = 0xffffffffL; // Minimum size in bits that the requested prime number has // before we use the large prime number generating algorithms static final int SMALL_PRIME_THRESHOLD = 95; // Certainty required to meet the spec of probablePrime static final int DEFAULT_PRIME_CERTAINTY = 100; static byte * randomBits(int numBits, Random rnd); /** * Find a random number of the specified bitLength that is probably prime. * This method is used for smaller primes, its performance degrades on * larger bitlengths. * * This method assumes bitLength > 1. */ static BigInteger smallPrime(int bitLength, int certainty, Random rnd); static final BigInteger SMALL_PRIME_PRODUCT = valueOf(3L*5*7*11*13*17*19*23*29*31*37*41); /** * Find a random number of the specified bitLength that is probably prime. * This method is more appropriate for larger bitlengths since it uses * a sieve to eliminate most composites before using a more expensive * test. */ static BigInteger largePrime(int bitLength, int certainty, Random rnd); /** * \brief This private constructor translates an int array containing the * two's-complement binary representation of a BigInteger into a * BigInteger. The input array is assumed to be in big-endian * int-order: the most significant int is in the zeroth element. */ BigInteger(int *val); /** * Returns true iff this BigInteger is a Lucas-Lehmer probable prime. * * The following assumptions are made: * This BigInteger is a positive, odd number. */ boolean passesLucasLehmer(); /** * Computes Jacobi(p,n). * Assumes n positive, odd, n>=3. */ static int jacobiSymbol(int p, BigInteger n); static BigInteger lucasLehmerSequence(int z, BigInteger k, BigInteger n); /** * Returns true iff this BigInteger passes the specified number of * Miller-Rabin tests. This test is taken from the DSA spec (NIST FIPS * 186-2). * * The following assumptions are made: * This BigInteger is a positive, odd number greater than 2. * iterations<=50. */ boolean passesMillerRabin(int iterations); /** * This private constructor differs from its public cousin * with the arguments reversed in two ways: it assumes that its * arguments are correct, and it doesn't copy the magnitude array. */ BigInteger(int[] magnitude, int signum); /** * This private constructor is for internal use and assumes that its * arguments are correct. */ BigInteger(byte[] magnitude, int signum); /** * This private constructor is for internal use in converting * from a MutableBigInteger object into a BigInteger. */ BigInteger(MutableBigInteger val, int sign); /** * Constructs a BigInteger with the specified value, which may not be zero. */ BigInteger(long val); /** * Returns a BigInteger with the given two's complement representation. * Assumes that the input array will not be modified (the returned * BigInteger will reference the input array if feasible). */ static BigInteger valueOf(int val[]); // Constants /** * Initialize static constant array when class is loaded. */ final static int MAX_CONSTANT = 16; static BigInteger posConst[] = new BigInteger[MAX_CONSTANT+1]; static BigInteger negConst[] = new BigInteger[MAX_CONSTANT+1]; static { for (int i = 1; i <= MAX_CONSTANT; i++) { int[] magnitude = new int[1]; magnitude[0] = (int) i; posConst[i] = new BigInteger(magnitude, 1); negConst[i] = new BigInteger(magnitude, -1); } } /** * Adds the contents of the int arrays x and y. This method allocates * a new int array to hold the answer and returns a reference to that * array. */ static int[] add(int[] x, int[] y); /** * Subtracts the contents of the second int arrays (little) from the * first (big). The first int array (big) must represent a larger number * than the second. This method allocates the space necessary to hold the * answer. */ private static int[] subtract(int[] big, int[] little); /** * Returns a BigInteger whose value is (this * val). * * @param val value to be multiplied by this BigInteger. * @return this * val */ public BigInteger multiply(BigInteger val); /** * Multiplies int arrays x and y to the specified lengths and places * the result into z. */ private int[] multiplyToLen(int[] x, int xlen, int[] y, int ylen, int[] z); /** * Returns a BigInteger whose value is (this2). * * @return this2 */ private BigInteger square(); /** * Squares the contents of the int array x. The result is placed into the * int array z. The contents of x are not changed. * The algorithm used here is adapted from Colin Plumb's C library. * Technique: Consider the partial products in the multiplication * of "abcde" by itself: * * a b c d e * * a b c d e * ================== * ae be ce de ee * ad bd cd dd de * ac bc cc cd ce * ab bb bc bd be * aa ab ac ad ae * * Note that everything above the main diagonal: * ae be ce de = (abcd) * e * ad bd cd = (abc) * d * ac bc = (ab) * c * ab = (a) * b * * is a copy of everything below the main diagonal: * de * cd ce * bc bd be * ab ac ad ae * * Thus, the sum is 2 * (off the diagonal) + diagonal. * * This is accumulated beginning with the diagonal (which * consist of the squares of the digits of the input), which is then * divided by two, the off-diagonal added, and multiplied by two * again. The low bit is simply a copy of the low bit of the * input, so it doesn't need special care. */ private static final int[] squareToLen(int[] x, int len, int[] z); /** * Left shift int array a up to len by n bits. Returns the array that * results from the shift since space may have to be reallocated. */ static int[] leftShift(int[] a, int len, int n); // shifts a up to len right n bits assumes no leading zeros, 0=0; i--) { if (bytesCopied == 4) { nextInt = mag[intIndex--]; bytesCopied = 1; } else { nextInt >>>= 8; bytesCopied++; } result[i] = (byte)nextInt; } return result; } /** * \brief A constructor for internal use that translates the sign-magnitude * representation of a BigInteger into a BigInteger. It checks the * arguments and copies the magnitude so this constructor would be * safe for external use. */ BigInteger(int signum, int *magnitude); // Create an integer with the digits between the two indexes // Assumes start < end. The result may be negative, but it // is to be treated as an unsigned value. int parseInt(char[] source, int start, int end); // Multiply x array times word y in place, and add word z static void destructiveMulAdd(int[] x, int y, int z); /** * \brief Translates the decimal std::string representation of a BigInteger into a * BigInteger. The std::string representation consists of an optional minus * sign followed by a sequence of one or more decimal digits. The * character-to-digit mapping is provided by Character.digit. * The std::string may not contain any extraneous characters (whitespace, for * example). * * \param val decimal std::string representation of BigInteger. */ BigInteger(std::string val); /** * \brief Constructs a randomly generated BigInteger, uniformly distributed over * the range 0 to (2numBits - 1), inclusive. * The uniformity of the distribution assumes that a fair source of random * bits is provided in rnd. Note that this constructor always * constructs a non-negative BigInteger. * * \param numBits maximum bitLength of the new BigInteger. * \param rnd source of randomness to be used in computing the new BigInteger. */ BigInteger(int numBits, Random rnd); /** * \brief Constructs a randomly generated positive BigInteger that is probably * prime, with the specified bitLength.

* * It is recommended that the {@link #probablePrime probablePrime} * method be used in preference to this constructor unless there * is a compelling need to specify a certainty. * * \param bitLength bitLength of the returned BigInteger. * \param certainty a measure of the uncertainty that the caller is * willing to tolerate. The probability that the new BigInteger * represents a prime number will exceed * (1 - 1/2certainty). The execution time of * this constructor is proportional to the value of this parameter. * \param rnd source of random bits used to select candidates to be * tested for primality. */ public BigInteger(int bitLength, int certainty, Random rnd); /** * \brief Returns true if this BigInteger is probably prime, * false if it's definitely composite. * * This method assumes bitLength > 2. * * \param certainty a measure of the uncertainty that the caller is * willing to tolerate: if the call returns true * the probability that this BigInteger is prime exceeds * (1 - 1/2certainty). The execution time of * this method is proportional to the value of this parameter. * \return true if this BigInteger is probably prime, * false if it's definitely composite. */ boolean primeToCertainty(int certainty); /** * The BigInteger constant two. (Not exported.) */ private static final BigInteger TWO = valueOf(2); // bitsPerDigit in the given radix times 1024 // Rounded up to avoid underallocation. private static long bitsPerDigit[] = { 0, 0, 1024, 1624, 2048, 2378, 2648, 2875, 3072, 3247, 3402, 3543, 3672, 3790, 3899, 4001, 4096, 4186, 4271, 4350, 4426, 4498, 4567, 4633, 4696, 4756, 4814, 4870, 4923, 4975, 5025, 5074, 5120, 5166, 5210, 5253, 5295}; static int bitCnt(int val); static int trailingZeroCnt(int val); static int bitCnt(int val); static int trailingZeroCnt(int val); /* * Returns -1, 0 or +1 as big-endian unsigned int array arg1 is * less than, equal to, or greater than arg2. */ private static int intArrayCmp(int[] arg1, int[] arg2); public: /** * \brief Translates a byte array containing the two's-complement binary * representation of a BigInteger into a BigInteger. The input array is * assumed to be in big-endian byte-order: the most significant * byte is in the zeroth element. */ BigInteger(byte *val); /** * \brief Translates the sign-magnitude representation of a BigInteger into a * BigInteger. The sign is represented as an integer signum value: -1 for * negative, 0 for zero, or 1 for positive. The magnitude is a byte array * in big-endian byte-order: the most significant byte is in the * zeroth element. A zero-length magnitude array is permissible, and will * result inin a BigInteger value of 0, whether signum is -1, 0 or 1. * * \param signum signum of the number (-1 for negative, 0 for zero, 1 * for positive). * \param magnitude big-endian binary representation of the magnitude of * the number. */ BigInteger(int signum, byte *magnitude); /** * \brief Translates the std::string representation of a BigInteger in the specified * radix into a BigInteger. The std::string representation consists of an * optional minus sign followed by a sequence of one or more digits in the * specified radix. The character-to-digit mapping is provided by * Character.digit. The std::string may not contain any extraneous * characters (whitespace, for example). * * \param val std::string representation of BigInteger. * \param radix radix to be used in interpreting val. */ BigInteger(std::string val, int radix); // Constructs a new BigInteger using a char array with radix=10 BigInteger(char[] val); /** * \brief Returns a positive BigInteger that is probably prime, with the * specified bitLength. The probability that a BigInteger returned * by this method is composite does not exceed 2-100. * * \param bitLength bitLength of the returned BigInteger. * \param rnd source of random bits used to select candidates to be * tested for primality. * \return a BigInteger of bitLength bits that is probably prime */ public static BigInteger probablePrime(int bitLength, Random rnd); /** * \brief Returns the first integer greater than this BigInteger that * is probably prime. The probability that the number returned by this * method is composite does not exceed 2-100. This method will * never skip over a prime when searching: if it returns p, there * is no prime q such that this < q < p. * * \return the first integer greater than this BigInteger that is probably prime. */ BigInteger nextProbablePrime(); /** * \brief Returns a BigInteger whose value is equal to that of the * specified long. This "static factory method" is * provided in preference to a (long) constructor * because it allows for reuse of frequently used BigIntegers. * * \param val value of the BigInteger to return. * \return a BigInteger with the specified value. */ static BigInteger valueOf(long val); /** * The BigInteger constant zero. */ static final BigInteger ZERO = new BigInteger(new int[0], 0); /** * The BigInteger constant one. */ static final BigInteger ONE = valueOf(1); /** * The BigInteger constant ten. */ static final BigInteger TEN = valueOf(10); /** * \brief Returns a BigInteger whose value is (this + val). * * \param val value to be added to this BigInteger. * \return this + val */ BigInteger add(BigInteger val); /** * Returns a BigInteger whose value is (this / val). * * \param val value by which this BigInteger is to be divided. * \return this / val */ BigInteger divide(BigInteger val); /** * Returns an array of two BigIntegers containing (this / val) * followed by (this % val). * * \param val value by which this BigInteger is to be divided, and the remainder computed. * \return an array of two BigIntegers: the quotient (this / val) * is the initial element, and the remainder (this % val) * is the final element. */ BigInteger[] divideAndRemainder(BigInteger val); /** * Returns a BigInteger whose value is (this % val). * * @param val value by which this BigInteger is to be divided, and the * remainder computed. * @return this % val * @throws ArithmeticException val==0 */ BigInteger remainder(BigInteger val); /** * \brief Returns a BigInteger whose value is (thisexponent). * Note that exponent is an integer rather than a BigInteger. * * \param exponent exponent to which this BigInteger is to be raised. * \return thisexponent */ BigInteger pow(int exponent); /** * \brief Returns a BigInteger whose value is the greatest common divisor of * abs(this) and abs(val). Returns 0 if * this==0 && val==0. * * \param val value with which the GCD is to be computed. * \return GCD(abs(this), abs(val)) */ BigInteger gcd(BigInteger val); /** * Returns a BigInteger whose value is the absolute value of this * BigInteger. * * @return abs(this) */ BigInteger abs(); /** * Returns a BigInteger whose value is (-this). * * @return -this */ BigInteger negate(); /** * Returns the signum function of this BigInteger. * * @return -1, 0 or 1 as the value of this BigInteger is negative, zero or * positive. */ int signum(); /** * \brief Returns a BigInteger whose value is (this mod m). This method * differs from remainder in that it always returns a * non-negative BigInteger. * * \param m the modulus. * \return this mod m */ BigInteger mod(BigInteger m); /** * \brief Returns a BigInteger whose value is * (thisexponent mod m). (Unlike pow, this * method permits negative exponents.) * * \param exponent the exponent. * \param m the modulus. * \return thisexponent mod m */ BigInteger modPow(BigInteger exponent, BigInteger m); static int[] bnExpModThreshTable = {7, 25, 81, 241, 673, 1793, Integer.MAX_VALUE}; // Sentinel /** * Returns a BigInteger whose value is (this-1 mod m). * * @param m the modulus. * @return this-1 mod m. * @throws ArithmeticException m <= 0, or this BigInteger * has no multiplicative inverse mod m (that is, this BigInteger * is not relatively prime to m). */ public BigInteger modInverse(BigInteger m); /** * Returns a BigInteger whose value is (this << n). * The shift distance, n, may be negative, in which case * this method performs a right shift. * (Computes floor(this * 2n).) * * @param n shift distance, in bits. * @return this << n * @see #shiftRight */ BigInteger shiftLeft(int n); /** * Returns a BigInteger whose value is (this >> n). Sign * extension is performed. The shift distance, n, may be * negative, in which case this method performs a left shift. * (Computes floor(this / 2n).) * * @param n shift distance, in bits. * @return this >> n * @see #shiftLeft */ BigInteger shiftRight(int n); int[] javaIncrement(int[] val); /** * \brief Returns a BigInteger whose value is (this & val). (This * method returns a negative BigInteger if and only if this and val are * both negative.) * * \param val value to be AND'ed with this BigInteger. * \return this & val */ BigInteger and(BigInteger val); /** * \brief Returns a BigInteger whose value is (this | val). (This method * returns a negative BigInteger if and only if either this or val is * negative.) * * \param val value to be OR'ed with this BigInteger. * \return this | val */ BigInteger or(BigInteger val); /** * \brief Returns a BigInteger whose value is (this ^ val). (This method * returns a negative BigInteger if and only if exactly one of this and * val are negative.) * * \param val value to be XOR'ed with this BigInteger. * \return this ^ val */ BigInteger xor(BigInteger val); /** * Returns a BigInteger whose value is (~this). (This method * returns a negative value if and only if this BigInteger is * non-negative.) * * @return ~this */ BigInteger not(); /** * Returns a BigInteger whose value is (this & ~val). This * method, which is equivalent to and(val.not()), is provided as * a convenience for masking operations. (This method returns a negative * BigInteger if and only if this is negative and val is * positive.) * * \param val value to be complemented and AND'ed with this BigInteger. * \return this & ~val */ BigInteger andNot(BigInteger val); /** * \brief Returns true if and only if the designated bit is set. * (Computes ((this & (1<<n)) != 0).) * * \param n index of bit to test. * \return true if and only if the designated bit is set. */ boolean testBit(int n); /** * Returns a BigInteger whose value is equivalent to this BigInteger * with the designated bit set. (Computes (this | (1<<n)).) * * @param n index of bit to set. * @return this | (1<<n) */ BigInteger setBit(int n); /** * \brief Returns a BigInteger whose value is equivalent to this BigInteger * with the designated bit cleared. * (Computes (this & ~(1<<n)).) * * \param n index of bit to clear. * \return this & ~(1<<n) */ BigInteger clearBit(int n); /** * \brief Returns a BigInteger whose value is equivalent to this BigInteger * with the designated bit flipped. * (Computes (this ^ (1<<n)).) * * \param n index of bit to flip. * \return this ^ (1<<n) */ BigInteger flipBit(int n); /** * \brief Returns the index of the rightmost (lowest-order) one bit in this * BigInteger (the number of zero bits to the right of the rightmost * one bit). Returns -1 if this BigInteger contains no one bits. * (Computes (this==0? -1 : log2(this & -this)).) * * Initialize lowestSetBit field the first time this method is * executed. This method depends on the atomicity of int modifies; * without this guarantee, it would have to be synchronized. * * \return index of the rightmost one bit in this BigInteger. */ int getLowestSetBit(); /** * \brief Returns the number of bits in the minimal two's-complement * representation of this BigInteger, excluding a sign bit. * For positive BigIntegers, this is equivalent to the number of bits in * the ordinary binary representation. (Computes * (ceil(log2(this < 0 ? -this : this+1))).) * * Initialize bitLength field the first time this method is executed. * This method depends on the atomicity of int modifies; without * this guarantee, it would have to be synchronized. * * \return number of bits in the minimal two's-complement * representation of this BigInteger, excluding a sign bit. */ int bitLength(); /** * Returns the number of bits in the two's complement representation * of this BigInteger that differ from its sign bit. This method is * useful when implementing bit-vector style sets atop BigIntegers. * * Initialize bitCount field the first time this method is executed. * This method depends on the atomicity of int modifies; without * this guarantee, it would have to be synchronized. * * @return number of bits in the two's complement representation * of this BigInteger that differ from its sign bit. */ int bitCount(); /** * Returns a BigInteger whose value is (this - val). * * @param val value to be subtracted from this BigInteger. * @return this - val */ public BigInteger subtract(BigInteger val); /** * \brief Returns true if this BigInteger is probably prime, * false if it's definitely composite. If * certainty is <= 0, true is * returned. * * \param certainty a measure of the uncertainty that the caller is * willing to tolerate: if the call returns true * the probability that this BigInteger is prime exceeds * (1 - 1/2certainty). The execution time of * this method is proportional to the value of this parameter. * \return true if this BigInteger is probably prime, * false if it's definitely composite. */ boolean isProbablePrime(int certainty) { /** * Compares this BigInteger with the specified BigInteger. This method is * provided in preference to individual methods for each of the six * boolean comparison operators (<, ==, >, >=, !=, <=). The * suggested idiom for performing these comparisons is: * (x.compareTo(y) <op> 0), * where <op> is one of the six comparison operators. * * @param val BigInteger to which this BigInteger is to be compared. * @return -1, 0 or 1 as this BigInteger is numerically less than, equal * to, or greater than val. */ int compareTo(BigInteger val); /** * Compares this BigInteger with the specified jcommon::Object *for equality. * * @param x jcommon::Object *to which this BigInteger is to be compared. * @return true if and only if the specified jcommon::Object *is a * BigInteger whose value is numerically equal to this BigInteger. */ boolean equals(jcommon::Object *x); /** * \brief Returns the minimum of this BigInteger and val. * * \param val value with which the minimum is to be computed. * \return the BigInteger whose value is the lesser of this BigInteger and * val. If they are equal, either may be returned. */ BigInteger min(BigInteger val); /** * \brief Returns the maximum of this BigInteger and val. * * \param val value with which the maximum is to be computed. * \return the BigInteger whose value is the greater of this and * val. If they are equal, either may be returned. */ BigInteger max(BigInteger val); /** * \brief Returns the hash code for this BigInteger. * * \return hash code for this BigInteger. */ int hashCode(); /** * \brief Returns the std::string representation of this BigInteger in the * given radix. If the radix is outside the range from {@link * Character#MIN_RADIX} to {@link Character#MAX_RADIX} inclusive, * it will default to 10 (as is the case for * Integer.tostd::string). The digit-to-character mapping * provided by Character.forDigit is used, and a minus * sign is prepended if appropriate. (This representation is * compatible with the {@link #BigInteger(std::string, int) (std::string, * int)} constructor.) * * \param radix radix of the std::string representation. * @return std::string representation of this BigInteger in the given radix. */ std::string tostd::string(int radix); /** * Returns the decimal std::string representation of this BigInteger. * The digit-to-character mapping provided by * Character.forDigit is used, and a minus sign is * prepended if appropriate. (This representation is compatible * with the {@link #BigInteger(std::string) (std::string)} constructor, and * allows for std::string concatenation with Java's + operator.) * * @return decimal std::string representation of this BigInteger. * @see Character#forDigit * @see #BigInteger(java.lang.std::string) */ std::string tostd::string(); /** * Returns a byte array containing the two's-complement * representation of this BigInteger. The byte array will be in * big-endian byte-order: the most significant byte is in * the zeroth element. The array will contain the minimum number * of bytes required to represent this BigInteger, including at * least one sign bit, which is (ceil((this.bitLength() + * 1)/8)). (This representation is compatible with the * {@link #BigInteger(byte[]) (byte[])} constructor.) * * \return a byte array containing the two's-complement representation of this BigInteger. */ byte[] toByteArray(); /** * \brief Converts this BigInteger to an int. This * conversion is analogous to a narrowing * primitive conversion from long to * int as defined in the Java Language * Specification: if this BigInteger is too big to fit in an * int, only the low-order 32 bits are returned. * Note that this conversion can lose information about the * overall magnitude of the BigInteger value as well as return a * result with the opposite sign. * * \return this BigInteger converted to an int. */ int intValue(); /** * Converts this BigInteger to a long. This * conversion is analogous to a narrowing * primitive conversion from long to * int as defined in the Java Language * Specification: if this BigInteger is too big to fit in a * long, only the low-order 64 bits are returned. * Note that this conversion can lose information about the * overall magnitude of the BigInteger value as well as return a * result with the opposite sign. * * @return this BigInteger converted to a long. */ long longValue(); /** * \brief Converts this BigInteger to a float. This * conversion is similar to the narrowing * primitive conversion from double to * float defined in the Java Language * Specification: if this BigInteger has too great a magnitude * to represent as a float, it will be converted to * {@link Float#NEGATIVE_INFINITY} or {@link * Float#POSITIVE_INFINITY} as appropriate. Note that even when * the return value is finite, this conversion can lose * information about the precision of the BigInteger value. * * \return this BigInteger converted to a float. */ float floatValue(); /** * \brief Converts this BigInteger to a double. This * conversion is similar to the narrowing * primitive conversion from double to * float defined in the Java Language * Specification: if this BigInteger has too great a magnitude * to represent as a double, it will be converted to * {@link Double#NEGATIVE_INFINITY} or {@link * Double#POSITIVE_INFINITY} as appropriate. Note that even when * the return value is finite, this conversion can lose * information about the precision of the BigInteger value. * * \return this BigInteger converted to a double. */ double doubleValue(); }; }; #endif