Wednesday, September 22, 2010

java.math.BigInteger

Unbounded range. To work with integers that are larger than 64 bits (the size of a long), use java.math.BigInteger. This class represents unbounded integers and provides a number of methods for doing arithmetic with them.
Overflow in standard arithmetic. The problem with arithmetic using ints (or longs) is that, if the value becomes too large, Java saves only the low order 32 (64 for longs) bits and throws the rest away. For example, we can use the maximum value that can be stored in an int as an example. Both of the following operations can't possibly fit into an int, but the arithmetic doesn't produce an error, it produces a result!

import java.math.BigInteger;

public class TestOverflow {
public static void main(String[] args) {
int bad = 2000000000; //Close to int max value.
System.out.println("bad = " + bad);
System.out.println("bad + 1 = " + (bad + 1));
System.out.println("bad * 3 = " + (bad * 3));
System.out.println("bad * 4 = " + (bad * 4));

BigInteger good = BigInteger.valueOf(2000000000);
System.out.println();
System.out.println("good = " + good);
System.out.println("good.add(BigInteger.ONE) = " + good.add(BigInteger.ONE));
System.out.println("good.multiply(BigInteger.valueOf(3)) = " + good.multiply(BigInteger.valueOf(3)));
System.out.println("good.multiply(BigInteger.valueOf(4)) = " + good.multiply(BigInteger.valueOf(4)));
}
 
Produces this output. If a result exceeds Integer.MAX_VALUE (2,147,483,647), the result will be wrong, but Java doesn't report an error! The BigInteger results are correct. But you do have to put up was some seriously ugly syntax.
bad = 2000000000 bad + 1 = 2000000001 bad * 3 = 1705032704 // Smaller? bad * 4 = -589934592 // Negative?!!! Overflowed into sign bit. good = 2000000000 good.add(BigInteger.ONE) = 2000000001 good.multiply(BigInteger.valueOf(3)) = 6000000000 good.multiply(BigInteger.valueOf(4)) = 8000000000 Omitted features. BigInteger methods ignored in this summary: bit operations, random numbers, and prime testing.

Constructors and Methods

Assume
BigInteger bi, bi1, bi2, bi3, bi4;
BigInteger[] bia; // array holding division result and remainder.
String s; int i; long lng; float f; double d;
Constructors, constants, and static factory methods
bi = new BigInteger(s);Create BigInteger with decimal value represented by decimal String s.
bi = BigInteger.ONE;Predefined value 1.
bi = BigInteger.ZERO;Predefined value 0.
bi = BigInteger.valueOf(lng);Use this factory method to create BigIntegers from numeric expressions. An int parameter will be automatically promoted to long.
Arithmetic operations
bi1bi2.abs();Returns BigInteger absolute value.
bi1bi2.add(bi3);Returns sum of bi2 and bi3.
bi1bi2.divide(bi3);Returns division of bi2 and bi3.
biabi2.divideAndRemainder(bi3);Returns array of two BigIntegers representing the result of division and remainder of bi2 and bi3.
bi1bi2.gcd(bi3);Returns greatest common divisor of bi2 and bi3.
bi1bi2.max(bi3);Returns maximum of bi2 and bi3.
bi1bi2.min(bi3);Returns minimum of bi2 and bi3
bi1bi2.mod(bi3);Returns remainder after dividing bi2 by bi3
bi1bi2.multiply(bi3);Returns product of bi2 and bi3.
bi1bi2.pow(bi3);Returns bi2 to the bi3 power.
bi1bi2.remainder(bi3);Returns remainder of dividing bi2 by bi3. May be negative.
ibi.signum();-1 for neg numbers, 0 for zero, and +1 for positive.
bi1bi2.subtract(bi3);Returns bi2 - bi3.
Conversion to other values
dbi.doubleValue();Returns double value equivalent of bi.
fbi.floatValue();Returns float value equivalent of bi.
ibi.intValue();Returns int value equivalent of bi.
lngbi.longValue();Returns long value equivalent of bi.
sbi.toString();Returns decimal string representation of bi.
sbi.toString(i);Returns string representation of bi in radix i.
Other
bbi1.compareTo(bi2);Returns negative number if bi1<bi2, 0 if bi1==bi2, or positive number if bi1>bi2.

 

No comments:

Post a Comment

Chitika