Quick viewing(Text Mode)

Using the Java 5.0 Bigdecimal Class

Using the Java 5.0 Bigdecimal Class

Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, IBM Corporation

Using Java 5.0 BigDecimal

Mike Cowlishaw IBM Fellow

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

Mike Cowlishaw — Using Java 5.0 BigDecimal Page 1 Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, IBM Corporation

Overview ƒ Background ¾ Why decimal arithmetic is important ¾ New standards for decimal formats, arithmetic, and hardware

ƒ Java 5.0 BigDecimal (what’s new, what’s fast?)

ƒ Questions?

Copyright © IBM Corporation 2004. All rights reserved.

Mike Cowlishaw — Using Java 5.0 BigDecimal Page 2 Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, IBM Corporation

Origins of Decimal Arithmetic ƒ Decimal (base 10) arithmetic has been used for thousands of years

ƒ Algorism (Indo-Arabic place value system) in use since 800 AD

ƒ Mechanical calculators were decimal

Mike Cowlishaw — Using Java 5.0 BigDecimal Page 3 Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, IBM Corporation

Early Computers ƒ Often derived from mechanical decimal calculator designs

ƒ Often decimal (even addresses)

ƒ But binary was shown to be more efficient ¾minimal storage space ¾more reliable (20% fewer components)

Mike Cowlishaw — Using Java 5.0 BigDecimal Page 4 Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, IBM Corporation

Today’s Computer Arithmetic

ALU FPU

Integer Floating-point arithmetic arithmetic (IEEE 754)

byte, short, int, single, float, long, etc. double, quad, etc.

Mike Cowlishaw — Using Java 5.0 BigDecimal Page 5 Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, IBM Corporation

Today’s Computer Arithmetic

ALU FPU

12 x 12 → 144 1.2 x 1.2 → 1.44

byte, short, int, single, float, long, etc. double, quad, etc.

Mike Cowlishaw — Using Java 5.0 BigDecimal Page 6 Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, IBM Corporation

1.2 x 1.2 = 1.44 ? ƒ Binary fractions cannot exactly represent most decimal fractions (e.g., 0.1 requires an infinitely long binary fraction)

ƒ 1.2 in a 32-bit binary float is actually: 1.2000000476837158203125

ƒ and this squared is: 1.440000057220458984375

Mike Cowlishaw — Using Java 5.0 BigDecimal Page 7 Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, IBM Corporation

Why Not Just Round ? ƒ Rounding hides but does not help ¾ obscures the slight inaccuracies ¾ errors accumulate [double rounding]

Decimal Java float (binary) 9 9 0.9 0.9 0.09 0.089999996 0.009 0.009

Mike Cowlishaw — Using Java 5.0 BigDecimal Page 8 Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, IBM Corporation

Where It Costs Real Money… ƒ Add 5% sales tax to a $ 0.70 telephone call, rounded to the nearest cent

ƒ 1.05 x 0.70 using binary double is exactly

0.73499999999999998667732370449812151491641998291015625

(should have been 0.735)

ƒ rounds to $ 0.73, instead of $ 0.74

Mike Cowlishaw — Using Java 5.0 BigDecimal Page 9 Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, IBM Corporation

Hence… ƒ Binary floating-point cannot be used for commercial or human-centric applications ¾ cannot meet legal and financial requirements

ƒ Decimal data and arithmetic are pervasive

ƒ 55% of numeric data in databases are decimal (and a further 43% integers)

Mike Cowlishaw — Using Java 5.0 BigDecimal Page 10 Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, IBM Corporation

Why Floating-point Decimal? ƒ Traditional integer arithmetic with ‘manual’ scaling is awkward and error-prone

ƒ Floating-point is increasingly necessary ¾interest calculated daily ¾telephone calls priced by the second ¾taxes more finely specified ¾financial analysis, etc.

Mike Cowlishaw — Using Java 5.0 BigDecimal Page 11 Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, IBM Corporation

So now we know what to do… ƒ but…

Mike Cowlishaw — Using Java 5.0 BigDecimal Page 12 Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, IBM Corporation

... but it’s very, very slow … For example, typical Java BigDecimal add is 1,708 cycles, hardware might take 8 cycles software penalty add 210x – 560x

quantize 90x – 200x

multiply 40x – 190x divide 260x – 290x

penalty = Java BigDecimal cycles ÷ DFPU clock cycles

Mike Cowlishaw — Using Java 5.0 BigDecimal Page 13 Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, IBM Corporation

Effect on Real Applications ƒ The ‘telco’ billing application 1,000,000 calls (two minutes) read from file, priced, taxed, and printed

C, # Java packages BigDecimal % execution time in decimal 72 – 78% 93.2% operations

Mike Cowlishaw — Using Java 5.0 BigDecimal Page 14 Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, IBM Corporation

Effect on Real Applications [2] ƒ A “ Web Warehouse” benchmark uses float binary for currency and tax calculations

ƒ We added realistic decimal processing…

orders per 69% of second workload float binary 3,862 is decimal arithmetic decimal 1,193

Mike Cowlishaw — Using Java 5.0 BigDecimal Page 15 Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, IBM Corporation

Hardware is on the way… ƒ A 2 x to 10 x performance improvement in applications makes hardware support very attractive

ƒ IBM is building Decimal Floating-Point (DFP) hardware into future processors

ƒ Critical precondition was IEEE 754 Standardization ― fortunately under revision (754r) since 2001

Mike Cowlishaw — Using Java 5.0 BigDecimal Page 16 Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, IBM Corporation

IEEE 754 Agreed Draft ƒ Now has decimal floating-point formats and arithmetic ¾ suitable for mathematical applications, too

ƒ Fixed-point and integer arithmetic are subsets (no normalization)

ƒ Compression maximizes precision and exponent range of formats

Mike Cowlishaw — Using Java 5.0 BigDecimal Page 17 Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, IBM Corporation

IEEE 754r Formats

size (bits) digits exponent range

32 7 -95 to +96

64 16 -383 to +384

128 34 -6143 to +6144

Mike Cowlishaw — Using Java 5.0 BigDecimal Page 18 Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, IBM Corporation

The Java BigDecimal Class

Mike Cowlishaw — Using Java 5.0 BigDecimal Page 19 Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, IBM Corporation

java.math.BigDecimal ƒ In Java since 1.1 (JDBC) ¾ BigInteger and int scale

ƒ Arbitrary precision BigDecimal z = x.multiply(y); // 1.2 x 1.2 Î 1.44

ƒ Eight rounding modes

Mike Cowlishaw — Using Java 5.0 BigDecimal Page 20 Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, IBM Corporation

Constructors ƒ BigDecimal( String )

ƒ BigDecimal( double ) ¾ exact conversion

ƒ BigDecimal( BigInteger [, int ] )

ƒ valueOf( long [, int ] )

Mike Cowlishaw — Using Java 5.0 BigDecimal Page 21 Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, IBM Corporation

Arithmetic ƒ add, subtract, multiply

ƒ divide (with given rounding and scale)

ƒ abs, negate

ƒ compareTo, min, max (and equals, hashcode)

Mike Cowlishaw — Using Java 5.0 BigDecimal Page 22 Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, IBM Corporation

Miscellaneous ƒ signum (returns sign)

ƒ scale, unscaledValue

ƒ setScale (with rounding) = IEEE quantize BigDecimal a=new BigDecimal(“0.735”); setScale(a, 2, ROUND_HALF_EVEN); Î 0.74

ƒ movePointLeft, movePointRight

Mike Cowlishaw — Using Java 5.0 BigDecimal Page 23 Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, IBM Corporation

Conversions ƒ toString, toBigInteger

ƒ intValue, longValue (byteValue and shortValue inherited) ¾ these quietly decapitate !

ƒ floatValue, doubleValue

Mike Cowlishaw — Using Java 5.0 BigDecimal Page 24 Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, IBM Corporation

BigDecimal 1.1 Problems ƒ No rounding control; results get longer and longer (and slower)

ƒ Dangerous when converting, no exponential

ƒ Important methods missing (remainder, pow, round, etc.)

ƒ Hard to use and not intuitive (esp. divide)

Mike Cowlishaw — Using Java 5.0 BigDecimal Page 25 Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, IBM Corporation

BigDecimal 5.0 Solution ƒ Core concept: Arithmetic operations depend on ¾ numbers (many instances) ¾ the context in which operations are effected

ƒ This is mirrored by the implementation: ¾ enhanced BigDecimal for the numbers; allows both positive and negative scale (e.g., 1.3E+9) ¾ new MathContext for the context

Mike Cowlishaw — Using Java 5.0 BigDecimal Page 26 Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, IBM Corporation

java.math.MathContext ƒ Immutable context object

ƒ Allows future extensions

ƒ Two properties: ¾ precision (where to round) ¾ rounding mode new mathContext(7, RoundingMode.HALF_EVEN)

Mike Cowlishaw — Using Java 5.0 BigDecimal Page 27 Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, IBM Corporation

Using MathContext BigDecimal A = new BigDecimal(“2”); BigDecimal B = new BigDecimal(“3”); MathContext mc = new MathContext(7, RoundingMode.HALF_EVEN);

BigDecimal C = A.divide(B, mc);

Î C has the value 0.6666667

Mike Cowlishaw — Using Java 5.0 BigDecimal Page 28 Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, IBM Corporation

java.math.RoundingMode ƒ Immutable enumeration, with constants: UP, DOWN, CEILING, FLOOR, HALF_UP, HALF_DOWN, HALF_EVEN, UNNECESSARY

ƒ equals, hashcode, toString, valueOf(String)

ƒ Old rounding mode int constants are still in BigDecimal

Mike Cowlishaw — Using Java 5.0 BigDecimal Page 29 Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, IBM Corporation

New Constructors ƒ BigDecimal( int ), BigDecimal( long )

ƒ BigDecimal( char[ ] ) … and sub-array

ƒ All old and new constructors may take a MathContext, for rounding on construction

ƒ New valueOf( double ) … rounds as Double

Mike Cowlishaw — Using Java 5.0 BigDecimal Page 30 Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, IBM Corporation

New Arithmetic ƒ New methods: simpler divide, remainder, divideToIntegralValue, divideAndRemainder, pow, plus, round

ƒ All arithmetic methods may take a MathContext

ƒ setScale may now take a RoundingMode

Mike Cowlishaw — Using Java 5.0 BigDecimal Page 31 Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, IBM Corporation

Miscellaneous ƒ New methods: ¾ precision ¾ ulp (unit in last place) ¾ scaleByPowerOf10( int ) ¾ stripTrailingZeros

ƒ Useful constants ¾ ZERO ¾ ONE ¾ TEN

Mike Cowlishaw — Using Java 5.0 BigDecimal Page 32 Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, IBM Corporation

New Conversions ƒ String/char constructors accept E+n etc.

ƒ toEngineeringString for exponent is multiple of 3 (12.3E+6 rather than 1.23E+7), and new toCharArray for efficiency

ƒ Exact, safe, integer conversions: toBigIntegerExact, intValueExact, longValueExact, shortValueExact, byteValueExact

Mike Cowlishaw — Using Java 5.0 BigDecimal Page 33 Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, IBM Corporation

Performance ƒ The internal representation (binary BigInteger) is inherently slow for conversions and rounding (base change)

ƒ However, the class will be able to take advantage of hardware DFP without recompilation

ƒ Especially if use right-size MathContext

Mike Cowlishaw — Using Java 5.0 BigDecimal Page 34 Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, IBM Corporation

Preferred MathContexts ƒ The MathContext class provides contexts which match the IEEE 754r sizes and default rounding (HALF_EVEN):

DECIMAL32 (7 digits) DECIMAL64 (16 digits) DECIMAL128 (34 digits)

ƒ (Also UNLIMITED, to match old behavior)

Mike Cowlishaw — Using Java 5.0 BigDecimal Page 35 Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, IBM Corporation

Taking Advantage of Hardware ƒ BigDecimal BigInteger objects are expensive lookaside 1 lookaside 2 BigDecimal lookaside 3 char [ ] lookaside 4 scale int 1 lookaside 5 precision .. int 2 .. ref lookaside 6 ref

Mike Cowlishaw — Using Java 5.0 BigDecimal Page 36 Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, IBM Corporation

Taking Advantage of Hardware ƒ BigDecimal with hardware lookaside BigDecimal (managed by the JIT scale compiler) precision

ƒ BigInteger only (ref) created when Hardware number is too large lookaside for hardware (rare) storage

Mike Cowlishaw — Using Java 5.0 BigDecimal Page 37 Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, IBM Corporation

Summary ƒ Major enhancements to the BigDecimal class make it much more useful and easier to use

ƒ New MathContext and RoundingMode classes give better control of arithmetic

ƒ Hardware on the way will dramatically improve performance

Mike Cowlishaw — Using Java 5.0 BigDecimal Page 38 Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, IBM Corporation

Questions?

Google: decimal arithmetic

Mike Cowlishaw — Using Java 5.0 BigDecimal Page 39 Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, IBM Corporation

How Computers Compute ƒ Binary arithmetic will continue to be used, but, perhaps …

“in the relatively distant future, the continuing decline in the cost of processors and of memory will result (in applications intended for human interaction) in the displacement of substantially all binary floating-point arithmetic by decimal”

Professor W. Kahan, UCB

Mike Cowlishaw — Using Java 5.0 BigDecimal Page 40 Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, IBM Corporation

IEEE 754r Format Details

Mike Cowlishaw — Using Java 5.0 BigDecimal Page 41 Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, IBM Corporation Common ‘shape’ for All Formats

Sign Comb. field Exponent Coefficient

ƒ Sign and combination field fit in first byte ¾ combination field (5 bits) combines 2 bits of the exponent (0−2), first digit of the coefficient (0−9), and the two special values ¾ allows ‘bulk initialization’ to zero, NaNs, and ± Infinity by byte replication

Mike Cowlishaw — Using Java 5.0 BigDecimal Page 42 Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, IBM Corporation

Exponent continuation

Sign Comb. field Exponent Coefficient

exponent Format bias normal range bits 32-bit 2+6 101 -95 to +96 64-bit 2+8 398 -383 to +384 128-bit 2+12 6176 -6143 to +6144

(All ranges larger than binary in same format.)

Mike Cowlishaw — Using Java 5.0 BigDecimal Page 43 Colorado Software Summit: October 24 – 29, 2004 © Copyright 2004, IBM Corporation

Coefficient continuation

Sign Comb. field Exponent Coefficient

ƒ – 3 digits in each group of 10 bits (6, 15, or 33 in all)

ƒ Derived from Chen-Ho encoding, which uses a Huffman code to allow expansion or compression in 2–3 gate delays

Mike Cowlishaw — Using Java 5.0 BigDecimal Page 44