How Java BigDecimal works

There some questions in recent minutes about Java BigDecimal.
Here are some clarifications:

> Does Java  BigDecimal track precision, e.g. 1.1 vs 1.100?

Yes.  It uses 'the usual' dual-integer representation for decimals,
with an integer coefficient (a BigInteger object of effecively
unlimited length) and an int holding an integer exponent (power
of 10).  In Java 1.1 through 1.4, the latter could only be 0 or
negative.  In the next version, Java 1.5, the exponent can be
positive, too.

> How do operations like addition and subtraction work on it.

Again, the 'usual' way for decimal arithmetic.  Briefly (not fully
rigorously), in Java 1.1 through 1.4 the rules are:

  * For addition and subtraction:

    If the exponents of the operands differ, the one with the larger
    exponent has its coefficient multiplied by ten (and its exponent
    reduced by 1) until the exponents are the same.

    The coefficients are now added or subtracted (an integer
    operation) to give the result coefficient.  The result exponent
    is that of the operands (after alignment).

  * For multiplication:

    The result coefficient is the product of the operand coefficients
    (again, simple integer multiplication).  The result exponent is
    the product of the operand exponents.

  * For division, the division is done to give a result of a given
    exponent (scale).

These rules are pretty much universal for arbitrary precision decimal
arithmetic (algorism), though the rules for division can vary ('when
to stop'?).  However, they need modification when there is limited
coefficient length or processing time available.  For example, without
modification, the results of multiplications just keep getting longer.

Java 1.5, therefore, adds the concept of a working precision.  The
results are as described above but are subject to a user-specified
working precision and are rounded to that precision if the result
would be too long.

For example, if the working precision were 9 digits:

  123.4567 * 123.4567 => 15241.5568  (instead of 15241.55677489)

There are 7 rounding modes available.

The arithmetic in C# and .Net is a subset of this, except that the
'working precision' is a binary limit, not an integral number of
digits (it is between 28 and 29 digits, 96 bits).

For more detailed descriptions of decimal arithmetic, see:

  http://www2.hursley.ibm.com/decimal/decarith.html

(also available there in .PS and .PDF).


Mike

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Mike Cowlishaw, IBM Fellow
IBM UK (MP5), PO Box 31, Birmingham Road, Warwick, CV34 5JL
mailto:mfc@uk.ibm.com  --  http://www2.hursley.ibm.com

Received on Sunday, 24 November 2002 12:49:55 UTC