Class BigInt
Class BigInt
java.lang.Object
|
+----java.lang.Number
|
+----BigInt
- final class BigInt
- extends Number
Multiple precision integer arithmetic.
The BigInt class implements integers with unbounded precision.
BigInts support the Classical Algorithms of addition, subtraction,
multiplication and division giving a quotient and remainder.
BigInts are a subclass of java.lang.Number so they
support the conversion operations intValue(),
doubleValue etc., just like the other forms of `boxed' number
(classes Integer, Float, Long and Double). It is a pity that
`Integer' was used as the name for a boxed int. `Int' would
have been a more regular name, and it would have left the name
`Integer' open for multiple precision integers which better model the
mathematical notion of integer.
The algorithms are taken from
Knuth, Donald E., "The Art of Computer Programming",
volume 2, "Seminumerical Algorithms"
section 4.3.1, "Multiple-Precision Arithmetic".
The addition, subtraction and multiplication algorithms are relatively
simple, but you will probably want to read this text before trying to
understand the division algorithm.
BigInts behave like numbers in that they are never modified after
creation.
For example
BigInt x = BigInt.valueOf("1000000000000000");
BigInt y = x.add(BigInt.ONE);
System.out.println(x);
prints 1000000000000000.
Operations do not always return new BigInt objects. For example, when
adding zero to a thousand digit number, it is more efficient to return
the thousand digit number than to make a copy.
You should never rely on Java's pointer equality operator (==)
to compare BigInts in any way.
Implementing these algorithms in Java is quite slow. Ideally, the
core methods should be native methods. A Just-In-Time compiler would
help, but unless it could lift array bounds checking out of loops,
native methods would win every time.
- See Also:
- Number
-
NEGATIVE_ONE
-
-
ONE
-
-
ZERO
-
-
BigInt(long)
- Construct a BigInt with a given long value.
-
abs()
- Return the integer with the same magnitude and positive sign.
-
add(BigInt)
- Return the sum of two BigInts.
-
add(BigInt, BigInt)
- Return the sum of two BigInts.
-
compare(BigInt)
- Compare to another BigInt.
-
compare(BigInt, BigInt)
- Compare two BigInt
-
dbg()
-
-
divide(BigInt, BigInt)
- Divide two BigInts, producing the quotient and remainder.
-
doubleValue()
- Returns the value of the number as a double.
-
equals(Object)
- Compare two BigInts for numerical equality.
-
floatValue()
- Returns the value of the number as a float.
-
hashCode()
-
-
intValue()
- Returns the value of the number as an int.
-
lessThan(BigInt)
- Ordering comparison.
-
longValue()
- Returns the value of the number as a long.
-
multiply(BigInt)
- Return this scaled by multiplicand.
-
multiply(long)
- Return this scaled by multiplicand.
-
multiply(BigInt, BigInt)
- Return product of two BigInts.
-
negate()
- Return the integer with the same magnitude and opposite sign.
-
quotient(BigInt, BigInt)
- Divide two BigInts, producing the quotient.
-
random(Random, BigInt)
- Generate a random BigInt.
-
random_digit_exponential(Random, BigInt)
- Used for testing divide.
-
remainder(BigInt, BigInt)
- Divide two BigInts, returning the remainder
-
subtract(BigInt)
- Return the difference of two BigInts.
-
subtract(BigInt, BigInt)
- Return the difference of two BigInts.
-
test()
- Test the sign of BigInt.
-
toString()
- Convert to a radix-10 String.
-
toString(int)
- Convert to a String.
-
valueOf(String)
- Convert the String into a BigInt integer.
-
valueOf(String, int)
- Convert the String into a BigInt integer.
ZERO
public final static BigInt ZERO
ONE
public final static BigInt ONE
NEGATIVE_ONE
public final static BigInt NEGATIVE_ONE
BigInt
public BigInt(long n)
- Construct a BigInt with a given long value.
BigInt fifty = new BigInt(50);
- See Also:
- valueOf
hashCode
public int hashCode()
- Overrides:
- hashCode in class Object
equals
public boolean equals(Object obj)
- Compare two BigInts for numerical equality.
Note: equals will return false if obj is not a BigNum,
even if obj makes sense as an integer:
BigInt.valueOf("100").equals(BigInt.valueOf("100")) true
BigInt.valueOf("100").equals(Integer.valueOf("100")) false
- Overrides:
- equals in class Object
lessThan
public boolean lessThan(BigInt comparand)
- Ordering comparison.
test
public int test()
- Test the sign of BigInt.
- Returns:
- -1 if it is negative, 0 if it is zero, and 1 if it is positive.
compare
public int compare(BigInt comparand)
- Compare to another BigInt.
- Parameters:
- comparand - a BigInt to compare against.
- Returns:
- -1 if less than comparand, 0 if equal to comparand,
and 1 if greater than comparand.
compare
public static int compare(BigInt x,
BigInt y)
- Compare two BigInt
- Parameters:
- x - first BigInt
- y - second BigInt
- Returns:
- -1 if x<y, 0 if x=y, and 1 if x>y
add
public BigInt add(BigInt addend)
- Return the sum of two BigInts.
add
public static BigInt add(BigInt x,
BigInt y)
- Return the sum of two BigInts.
negate
public BigInt negate()
- Return the integer with the same magnitude and opposite sign.
abs
public BigInt abs()
- Return the integer with the same magnitude and positive sign.
subtract
public BigInt subtract(BigInt y)
- Return the difference of two BigInts.
subtract
public static BigInt subtract(BigInt x,
BigInt y)
- Return the difference of two BigInts.
multiply
public BigInt multiply(BigInt multiplicand)
- Return this scaled by multiplicand.
multiply
public BigInt multiply(long multiplicand)
- Return this scaled by multiplicand.
multiply
public static BigInt multiply(BigInt x,
BigInt y)
- Return product of two BigInts.
divide
public static BigInt[] divide(BigInt numerator,
BigInt denominator)
- Divide two BigInts, producing the quotient and remainder.
This is more efficient than computing the values separately.
- Returns:
- an array of two BigInts, with the quotient at index 0, and
the remainder at index 1.
quotient
public static BigInt quotient(BigInt numerator,
BigInt denominator)
- Divide two BigInts, producing the quotient.
- See Also:
- divide
remainder
public static BigInt remainder(BigInt numerator,
BigInt denominator)
- Divide two BigInts, returning the remainder
- See Also:
- divide
valueOf
public static BigInt valueOf(String s) throws NumberFormatException
- Convert the String into a BigInt integer. The radix is assumed to be 10.
- Parameters:
- s - the String containing the number
- Throws: NumberFormatException
- The String cannot be parsed as an integer.
valueOf
public static BigInt valueOf(String s,
int radix) throws NumberFormatException
- Convert the String into a BigInt integer.
- Parameters:
- s - the String containing the number
- radix - the radix to be used
- Throws: NumberFormatException
- The String cannot be parsed as an integer.
toString
public String toString()
- Convert to a radix-10 String.
- Overrides:
- toString in class Object
toString
public String toString(int radix)
- Convert to a String.
- Parameters:
- radix - the radix to be used
dbg
public String dbg()
intValue
public int intValue()
- Returns the value of the number as an int.
- Throws: ArithmeticException
- The number is too great to be represented as an int.
- Overrides:
- intValue in class Number
longValue
public long longValue()
- Returns the value of the number as a long.
- Throws: ArithmeticException
- The number is too big to be represented as a long.
- Overrides:
- longValue in class Number
floatValue
public float floatValue()
- Returns the value of the number as a float.
This may involve loss of precision or overflow.
- Overrides:
- floatValue in class Number
doubleValue
public double doubleValue()
- Returns the value of the number as a double.
This may involve loss of precision or overflow.
- Overrides:
- doubleValue in class Number
random
public static BigInt random(Random rng,
BigInt limit)
- Generate a random BigInt.
- Parameters:
- rng - the random number generator to use.
It is called an unspecified number of times.
- limit - specifies the range of the result.
- Returns:
- a BigInt in the range 0..limit-1 inclusive.
random_digit_exponential
public static BigInt random_digit_exponential(Random rng,
BigInt limit)
- Used for testing divide. Generates numbers with an exponential-like
distribution. Not otherwise useful.