BigInteger multiply(BigInteger multiplicand)

BigInteger negate()

Return a new Biglnteger instance that contains the product of the current value and the argument value. This method throws NullPointerException when multiplicand is null.

Return a new BigInteger instance that contains the negative of the current value.

Biglnteger remainder(BigInteger Return a new Biglnteger instance that contains the remainder divisor) of the current value divided by the argument value. This method throws NullPointerException when divisor is null, and ArithmeticException when divisor represents 0.

BigInteger subtract(BigInteger subtrahend)

Return a new BigInteger instance that contains the current value minus the argument value. This method throws NullPointerException when subtrahend is null.

String toString()

Return a string representation of this BigInteger.

The best way to get comfortable with BigInteger is to try it out. Listing 6-5 uses this class in a factorial() method comparison context.

Listing 6-5. Comparing factorial() methods class FactComp {

public static void main(String[] args) {

System.out.println(factorial(12));

System.out.println();

System.out.println(factorial(20L));

System.out.println();

System.out.println(factorial(170.0));

System.out.println();

System.out.println(factorial(new BigInteger("170"))); System.out.println(); System.out.println(factorial(25.0)); System.out.println();

System.out.println(factorial(new BigInteger("25")));

public static int factorial(int n) {

if (n == 0) return 1; else return n*factorial(n-1);

public static long factorial(long n) {

if (n == 0) return 1; else return n*factorial(n-1);

public static double factorial(double n) {

if (n == 1.0) return 1.0; else return n*factorial(n-1);

public static BigInteger factorial(BigInteger n) {

if (n.equals(BigInteger.ZERO))

return BigInteger.ONE; else return n.multiply(factorial(n.subtract(BigInteger.ONE)));

Listing 6-5 compares four versions of the recursive factorial() method. This comparison reveals the largest argument that can be passed to each of the first three methods before the returned factorial value becomes meaningless, because of limits on the range of values that can be accurately represented by the numeric type.

The first version is based on int and has a useful argument range of 0 through 12. Passing any argument greater than 12 results in a factorial that cannot be represented accurately as an int.

You can increase the useful range of factorial(), but not by much, by changing the parameter and return types to long. After making these changes, you will discover that the upper limit of the useful range is 20.

To further increase the useful range, you might create a version of factorial() whose parameter and return types are double. This is possible because whole numbers can be represented exactly as doubles. However, the largest useful argument that can be passed is 170.0. Anything higher than this value results in factorial() returning +infinity.

It is possible that you might need to calculate a higher factorial value, perhaps in the context of calculating a statistics problem involving combinations or permutations. The only way to accurately calculate this value is to use a version of factorial() based on BigInteger.

When you run the previous application, it generates the following output:

479001600

7257415615307998967396728211129263114716991681296451376543577798900561843401706157852350 7492426174595114909912378385207766660225654427530253289007732075109024004302800582956039 6661259965825710439855829425756896631343961226257109494680671120556888045719334021266145 2800000000000000000000000000000000000000000

1.5511210043330986E25

The first three values represent the highest factorials that can be returned by the int-based, long-based, and double-based factorial() methods. The fourth value represents the BigInteger equivalent of the highest double factorial.

Notice that the double method fails to accurately represent 170! (! is the math symbol for factorial). Its precision is simply too small. Although the method attempts to round the smallest digit, rounding does not always workâthe number ends in 7994 instead of 7998. Rounding is only accurate up to argument 25.0, as the last two output lines reveal.

NOTE: RSA encryption, BigDecimal, and factorial are practical examples of BigInteger's usefulness. However, you can also use Biglnteger in unusual ways. For example, my February 2006 JavaWorld article titled "Travel Through Time with Java"

(http://www.javaworld.com/javaworld/jw-02-2006/jw-0213-funandgames.html), a part of my Java Fun and Games series, used Biglnteger to store an image as a very large integer. The idea was to experiment with BigInteger methods to look for images of people and places that existed in the past, will exist in the future, or might never exist. If this craziness appeals to you, check out my article.

Was this article helpful?

## Post a comment