# Big Numbers Core Java

If the precision of the basic integer and floating -point types is not sufficient, you cancturn to a couple of handy classes in the java.math package: BigInteger and BigDecimal. Thesecare classes for manipulating numbers with an arbitrarily long sequence of digits. The cBig Integer class implements arbitrary precision integer arithmetic, and BigDecimal does thecsame for floating -point numbers.

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

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.

C++ NOTE: Unlike C++, Java has no programmable operator overloading. There was no way for the programmer of the BigInteger class to redefine the + and * operators to give the add and multiply operations of the BigInteger classes. The language designers did overload the + operator to denote concatenation of strings. They chose not to overload other operators, and they did not give Java programmers the opportunity to overload operators in their own classes.

Listing 6 shows a modification of the lottery odds program of Listing 5, updated tonwork with big numbers. For example, if you are invited to participate in a lottery in which you need to pick 60 numbers out of a possible 490 numbers, then this program will tell you that your odds are 1 in

7 1 6 3 9 5 8 4 3 4 6 1 9 9 5 5 5 7 4 1 5 1 1 6 2 2 2 5 4 0 0 9 2 9 3 3 4 1 1 7 1 7 6
1 2 7 8 9 2 6 3 4 9 3 4 9 3 3 5 1 0 1 3 4 5 9 4 8 1 1 0 4 6 6 8 8 4 8. Good luck
!

The program in LotteryOdds.java computed the statement

When big numbers are used, the equivalent statement becomes

lottery Odds = lottery Odds .multiply (BigInteger .valueOf (n - i + 1) ) .divide

• 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.
• 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.
• BigDecimal subtract(BigDecimal other)
• BigDecimal multiply(BigDecimal other)
• BigDecimal divide(BigDecimal other, RoundingMode mode) 5.0
returns the sum, difference, product, or quotient of this big decimal and other.
To compute the quotient, you must supply a rounding mode. The mode
RoundingMode.HALF_UP is the rounding mode that you learned in school (i.e., round down digits 0 . . . 4, round up digits 5 ... 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.

BigIntegerTest.java

0