The 64 bits in the long
type correspond to about 19 decimal digits. There are applications,
however, that deal with numbers containing many more digits than
provided by these types. For example, cryptography requires very
large prime numbers, as many as 2000 bits are used, for the public/private
key encryption algorithms.
Floating point values can represent large values
but with limited precision. The double
type contains a 53 bit signed significand, which translates to 15
to 17 digits decimal precision.
As we discussed in Chapter 2, it is oftent the
case that a finite fraction in decimal can not be represented by
a finite number of digits in the binary base (such as 1/10). So
there can be a loss of precision for a decimal value in floating
point format. Techniques to minimize and quantify the accumulation
of such errors over repeated calculations are available from error
analysis methods. A brute force approach, however, is simply to
use extremely high precision number representations that avoid the
error build up altogether. Financial processing might use this approach,
for example. Some mathematical exericises, such as calculating PI
to higher and higher precision, require arbitrarily wide fractional
For such applications that require very large
integer values and extremely precise decimal values, the package
provides the BigInteger
classes. Instances of these classes hold values of arbitrary precision.
The values internally are immutable.
objects, once created, they cannot change.
Since the object values are immutable, any operation
on a value BigInteger
can only return a new value. This is something to consider when
implementing algorithms with these classes that involve many operations.
Unneeded values should be de-referenced so that the garbage collector
can retrieve the memory space. Of course, operations with such classes
will be much slower than those with primitive types.
In the following two sections we provide describe
these two classes. See the Java 2 API Specifications, however, for
details and descriptions of all the methods. See also reference
Mak for a more extensive discussion of these classes and for
code that provides additional mathematical operations.
class contains methods that provide all the basic arithmetic operations
such as add, subtract, multiply and divide. For example,
bi1 = new BigInteger(“1143209523490543953412323238479”);
BigInteger bi2 = new BigInteger(“34548738754398454599999997876786578479”);
BigInteger bi3 = bi2.add(bi1);
// = bi2 + bi1
BigInteger bi4 = bi2.divide(bi1); // = bi2 / bi1
Note that bi3
are new BigInteger
objects created by the add()
methods, respectively. There are four BigInteger
instances in the four lines of code above. If bi1
are no longer needed, they could be de-referenced as follows:
bi1 = bi2 =
With no more references to the BigInteger
objects formerly known as bi1
the garbage collector is free to reclaim that memory when needed.
Several other arithmetic methods are included
as well, such as abs(),
Plus there are methods to return values as int,
primitives. These are, of course, narrowing conversions that lose
information about the value if the BigInteger
value is too large to represent as the primitive type. See the API
documentation for full details.
Since the BigInteger
instances are mostly used for encryption tasks, there are several
methods related to prime number generation and modulo arithmetic.
BigInteger probablePrime(int numberBits, Random ran)
This method generates a number that has a high
likelihood of being prime. The arguments
include the bit length of the prime number and a reference to an
instance of the Random class that the method will use to select
candidates for primality. The method
Here the certainty parameter indicates the desired
level of confidence that the number really is prime.
If the probability exceeds (1
, the method returns true.
class, see above, contains methods to carry out various bitwise
operations. For example,
BigInteger shiftLeft(int n)
These return new values shifted left or right
by the number of bits indicated. Other bitwise methods include: and,
We note that BigInteger
which makes arrays of BigInteger
objects sortable with the java.util.Arrays
methods described in Chapter
10: Java : Arrays
Class. and Chapter 8: Tech
: Histogram Sorting. The compareTo()
method, required by Comparable,
is useful for comparing two BigInteger
values outside of the sort()
BigDecimal contains internally
an arbitrary precision integer - unscaledValue
- and a 32 bit scale value - scale.
The value represented by a BigDecimal
object corresponds to
So for this instance of BigDecimal
one = new BigDecimal(new BigInteger(“1”), Integer.MAX_VALUE);
the decimal point is
231-1 (2,147,483,648) places to
the left of the 1. You can
obtain a new BigDecimal
value with the scale increased by n places via the method
Similarly, you can obtain a new value with the
scale decreased by n places using
Note that in both cases the precision of the
unscaled value remains unchanged. Only the decimal point has moved.
Other methods provide the scale value (as an
and the unscaled value (as an instance of BigInteger).
There are also methods that return a float
and a double
type value, with the precision truncated as necessary to fit the
narrower fractional range of these floating point types.
As with BigInteger,
class contains methods that provide all the basic
arithmetic operations such as add, subtract, multiply and divide.
However, for the division, one of eight rounding options must be
chosen. For example,
bd1 = new BigDecimal(
BigDecimal bd2 = new
= bd2 / bd1
BigDecimal bd4 = bd2.divide(bd1, BigDecimal.ROUND_DOWN);
Here the division rounds towards zero. Other
rounding style constants defined in the BigDecimal
class include ROUND_UP,
ROUND_FLOOR, ROUND_CEILING, etc. See the API documentation
for rounding details.
& Web Resources
- Robert Mak, Java Number Cruncher: The Java
Programmer's Guide to Numerical Computing
Latest update: Nov. 19, 2004