Big Numbers

   

Core Java™ 2: Volume I - Fundamentals
By Cay S. Horstmann, Gary Cornell
Table of Contents
Chapter 3.  Fundamental Programming Structures 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, called BigInteger and BigDecimal. These are classes for manipulating numbers with an arbitrarily long sequence of digits. The BigInteger class implements arbitrary precision integer arithmetic, and BigDecimal does the same for floating-point numbers.

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

 BigInteger a = BigInteger.valueOf(100); 

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) 

graphics/cplus_icon.gif

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 themselves.

Example 3-6 shows a modification of the lottery odds program of Example 3-5, updated to work 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 716395843461995557415116222540092933411717612789263493493351 013459481104668848. Good luck!

The program in Example 3-5 computed the following statement:

 lotteryOdds = lotteryOdds * (n - i + 1) / i; 

When using big numbers, the equivalent statement becomes:

 lotteryOdds = lotteryOdds.multiply(BigInteger.valueOf(n - i + 1))    .divide(BigInteger.valueOf(i)); 
Example 3-6 BigIntegerTest.java
  1. import javax.swing.*;  2. import java.math.*;  3.  4. public class BigIntegerTest  5. {  6.    public static void main(String[] args)  7.    {  8.       String input = JOptionPane.showInputDialog  9.          ("How many numbers do you need to draw?"); 10.       int k = Integer.parseInt(input); 11. 12.       input = JOptionPane.showInputDialog 13.          ("What is the highest number you can draw?"); 14.       int n = Integer.parseInt(input); 15. 16.       /* 17.          compute binomial coefficient 18.          n * (n - 1) * (n - 2) * . . . * (n - k + 1) 19.          ------------------------------------------- 20.          1 * 2 * 3 * . . . * k 21.       */ 22. 23.       BigInteger lotteryOdds = BigInteger.valueOf(1); 24. 25.       for (int i = 1; i <= k; i++) 26.          lotteryOdds = lotteryOdds 27.             .multiply(BigInteger.valueOf(n - i + 1)) 28.             .divide(BigInteger.valueOf(i)); 29. 30.       System.out.println("Your odds are 1 in " + lotteryOdds + 31.         ". Good luck!"); 32. 33.       System.exit(0); 34.    } 35. } 

java.math.BigInteger 1.1

graphics/api_icon.gif
  • BigInteger add(BigInteger other)

  • BigInteger subtract(BigInteger other)

  • BigInteger multiply(BigInteger other)

  • BigInteger divide(BigInteger other)

  • BigInteger mod(BigInteger other)

    Return 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.

java.math.BigDecimal 1.1

graphics/api_icon.gif
  • BigDecimal add(BigDecimal other)

  • BigDecimal subtract(BigDecimal other)

  • BigDecimal multiply(BigDecimal other)

  • BigDecimal divide(BigDecimal other, int roundingMode)

    Return the sum, difference, product, or quotient of this big decimal and other. To compute the quotient, you need to supply a rounding mode. The mode BigDecimal.ROUND_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)

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


       
    Top
     



    Core Java 2(c) Volume I - Fundamentals
    Building on Your AIX Investment: Moving Forward with IBM eServer pSeries in an On Demand World (MaxFacts Guidebook series)
    ISBN: 193164408X
    EAN: 2147483647
    Year: 2003
    Pages: 110
    Authors: Jim Hoskins

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net