Big Numbers in Java

If the precision of the basic integer and floating-point types is not sufficient, you can turn to a couple of handy classes in the java.math package: BigInteger and BigDecimat. These are classes for manipulating numbers with an arbitrarily long sequence of digits. The BigInteger class implements arbitrary-precision integer arithmetic, and BigDecimat does the same for floating-point numbers.

Use the static vatueOf method to turn an ordinary number into a big number:

BigInteger a = BigInteger.vatueOf(100);

For longer numbers, use a constructor with a string parameter:

BigInteger reallyBig

= new BigInteger(“222232244629420445529739893461909967206666939096499764990979600”);

There are also constants BigInteger.ZERO, BigInteger.ONE, BigInteger.TEN, and, since Java 9, BigInteger.TWO.

Unfortunately, you cannot use the familiar mathematical operators such as + and * to combine big numbers. Instead, you must use methods such as add and multiply in the big number classes.

BigInteger c = a.add(b); // c = a + b

BigInteger d = c.multiply(b.add(BigInteger.valueOf(2))); // d = c * (b + 2)

Listing 3.6 shows a modification of the lottery odds program of Listing 3.5, updated to work with big numbers. For example, if you are invited to partic­ipate in a lottery in which you need to pick 60 numbers out of a possible 490 numbers, you can use this program to tell you your odds of winning. They are 1 in 716395843461995557415116222540092933411717612789263493493351013459481104668848. Good luck!

The program in Listing 3.5 computed the statement

totteryOdds = totteryOdds * (n – i + 1) / i;

When big numbers are used, the equivalent statement becomes

lotteryOdds

= totteryOdds.muttipty(BigInteger.vatueOf(n – i + 1)).divide(BigInteger.vatueOf(i));

java.math.Biglnteger 1.1

• BigInteger subtract(BigInteger other)
• BigInteger multiply(BigInteger other)
• BigInteger divide(BigInteger other)
• BigInteger mod(BigInteger other)

returns the sum, difference, product, quotient, and remainder of this big integer and other.

• BigInteger sqrt() 9

yields the square root of this BigInteger.

• int compareTo(BigInteger other)

returns 0 if this big integer equals other, a negative result if this big integer is less than other, and a positive result otherwise.

• static BigInteger valueOf(long x)

returns a big integer whose value equals x.

java.math.BigDecimal 1.1

• BigDecimal subtract(BigDecimal other)
• BigDecimal multiply(BigDecimal other)
• BigDecimal divide(BigDecimal other)
• BigDecimal divide(BigDecimal other, RoundingMode mode) 5

returns the sum, difference, product, or quotient of this big decimal and other. The first divide method throws an exception if the quotient does not have a finite decimal expansion. To obtain a rounded result, use the second method. The mode RoundingMode.HALF_UP is the rounding mode that you learned in school: round down the digits 0 to 4, round up the digits 5 to 9. It is appropriate for routine calculations. See the API documentation for other rounding modes.

• int compareTo(BigDecimal other)

returns 0 if this big decimal equals other, a negative result if this big decimal is less than other, and a positive result otherwise.

• static BigDecimal valueOf(long x)
• static BigDecimal valueOf(long x, int scale)

returns a big decimal whose value equals x or x / 10scale.

Source: Horstmann Cay S. (2019), Core Java. Volume I – Fundamentals, Pearson; 11th edition.