Using Java 5.0 BigDecimal



Similar documents
Binary Number System. 16. Binary Numbers. Base 10 digits: Base 2 digits: 0 1

This Unit: Floating Point Arithmetic. CIS 371 Computer Organization and Design. Readings. Floating Point (FP) Numbers

ECE 0142 Computer Organization. Lecture 3 Floating Point Representations

Number Representation

Binary Division. Decimal Division. Hardware for Binary Division. Simple 16-bit Divider Circuit

Overview. java.math.biginteger, java.math.bigdecimal. Definition: objects are everything but primitives The eight primitive data type in Java

CHAPTER 5 Round-off errors

Measures of Error: for exact x and approximation x Absolute error e = x x. Relative error r = (x x )/x.

Correctly Rounded Floating-point Binary-to-Decimal and Decimal-to-Binary Conversion Routines in Standard ML. By Prashanth Tilleti

Divide: Paper & Pencil. Computer Architecture ALU Design : Division and Floating Point. Divide algorithm. DIVIDE HARDWARE Version 1

Numbering Systems. InThisAppendix...

Numerical Matrix Analysis

Fast Arithmetic Coding (FastAC) Implementations

NUMBER SYSTEMS. William Stallings

To convert an arbitrary power of 2 into its English equivalent, remember the rules of exponential arithmetic:

CS321. Introduction to Numerical Methods

2010/9/19. Binary number system. Binary numbers. Outline. Binary to decimal

Data Storage 3.1. Foundations of Computer Science Cengage Learning

Monday January 19th 2015 Title: "Transmathematics - a survey of recent results on division by zero" Facilitator: TheNumberNullity / James Anderson, UK

Computer Science 281 Binary and Hexadecimal Review

1. Give the 16 bit signed (twos complement) representation of the following decimal numbers, and convert to hexadecimal:

This 3-digit ASCII string could also be calculated as n = (Data[2]-0x30) +10*((Data[1]-0x30)+10*(Data[0]-0x30));

Today. Binary addition Representing negative numbers. Andrew H. Fagg: Embedded Real- Time Systems: Binary Arithmetic

Number Systems and Radix Conversion

=

Let s put together a Manual Processor

The string of digits in the binary number system represents the quantity

Data Storage. Chapter 3. Objectives. 3-1 Data Types. Data Inside the Computer. After studying this chapter, students should be able to:

CSI 333 Lecture 1 Number Systems

Addition Methods. Methods Jottings Expanded Compact Examples = 15

Computers. Hardware. The Central Processing Unit (CPU) CMPT 125: Lecture 1: Understanding the Computer

DNA Data and Program Representation. Alexandre David

Instruction Set Architecture (ISA)

Solution for Homework 2

Bachelors of Computer Application Programming Principle & Algorithm (BCA-S102T)

Section 1.4 Place Value Systems of Numeration in Other Bases

Base Conversion written by Cathy Saxton

Chapter 2. Binary Values and Number Systems

Useful Number Systems

Advanced Tutorials. Numeric Data In SAS : Guidelines for Storage and Display Paul Gorrell, Social & Scientific Systems, Inc., Silver Spring, MD

on an system with an infinite number of processors. Calculate the speedup of

Lecture 8: Binary Multiplication & Division

Oct: 50 8 = 6 (r = 2) 6 8 = 0 (r = 6) Writing the remainders in reverse order we get: (50) 10 = (62) 8

Lecture 2. Binary and Hexadecimal Numbers

CS101 Lecture 11: Number Systems and Binary Numbers. Aaron Stevens 14 February 2011

Levent EREN A-306 Office Phone: INTRODUCTION TO DIGITAL LOGIC

Java CPD (I) Frans Coenen Department of Computer Science

Pemrograman Dasar. Basic Elements Of Java

HOMEWORK # 2 SOLUTIO

What Every Computer Scientist Should Know About Floating-Point Arithmetic

THE EXACT DOT PRODUCT AS BASIC TOOL FOR LONG INTERVAL ARITHMETIC

Addressing The problem. When & Where do we encounter Data? The concept of addressing data' in computations. The implications for our machine design(s)

Introduction to Xilinx System Generator Part II. Evan Everett and Michael Wu ELEC Spring 2013

Practical Programming, 2nd Edition

Binary Numbering Systems

Digital System Design Prof. D Roychoudhry Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Solve addition and subtraction word problems, and add and subtract within 10, e.g., by using objects or drawings to represent the problem.

Systems I: Computer Organization and Architecture

Chapter 4: Computer Codes

NUMBER SYSTEMS. 1.1 Introduction

Precision & Performance: Floating Point and IEEE 754 Compliance for NVIDIA GPUs

6.4 Normal Distribution

LSN 2 Number Systems. ECT 224 Digital Computer Fundamentals. Department of Engineering Technology

İSTANBUL AYDIN UNIVERSITY

Attention: This material is copyright Chris Hecker. All rights reserved.

THE BINARY NUMBER SYSTEM

Lecture 11: Number Systems

Guidance paper - The use of calculators in the teaching and learning of mathematics

MS ACCESS DATABASE DATA TYPES

Welcome to Basic Math Skills!

Mathematical goals. Starting points. Materials required. Time needed

Chapter 2 Logic Gates and Introduction to Computer Architecture

Zuse's Z3 Square Root Algorithm Talk given at Fall meeting of the Ohio Section of the MAA October College of Wooster

Grade 6 Math Circles. Binary and Beyond

Radicals - Multiply and Divide Radicals

A Short Guide to Significant Figures

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

Computer Networks and Internets, 5e Chapter 6 Information Sources and Signals. Introduction

Next Generation GPU Architecture Code-named Fermi

COMP 250 Fall 2012 lecture 2 binary representations Sept. 11, 2012

Introduction to RISC Processor. ni logic Pvt. Ltd., Pune

CALCULATIONS & STATISTICS

CPEN Digital Logic Design Binary Systems

.NET Standard DateTime Format Strings

Contents. 9-1 Copyright (c) N. Afshartous

Expert Reference Series of White Papers. Simple Tricks To Ace the Subnetting Portion of Any Certification Exam COURSES.

CS201: Architecture and Assembly Language

The programming language C. sws1 1

Session 7 Fractions and Decimals

Zero: If P is a polynomial and if c is a number such that P (c) = 0 then c is a zero of P.

Figure 1. A typical Laboratory Thermometer graduated in C.

Everything you wanted to know about using Hexadecimal and Octal Numbers in Visual Basic 6

The gas can has a capacity of 4.17 gallons and weighs 3.4 pounds.

High-Precision C++ Arithmetic

Converting Models from Floating Point to Fixed Point for Production Code Generation

NUMBER SYSTEMS APPENDIX D. You will learn about the following in this appendix:

Subnetting Examples. There are three types of subnetting examples I will show in this document:

FLOATING-POINT ARITHMETIC IN AMD PROCESSORS MICHAEL SCHULTE AMD RESEARCH JUNE 2015

Chapter 5 Instructor's Manual

Transcription:

Using Java 5.0 BigDecimal Mike Cowlishaw IBM Fellow http://www2.hursley.ibm.com/decimal Mike Cowlishaw Using Java 5.0 BigDecimal Page 1

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

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

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

Today s Computer Arithmetic ALU Integer arithmetic FPU Floating-point arithmetic (IEEE 754) byte, short, int, long, etc. single, float, double, quad, etc. Mike Cowlishaw Using Java 5.0 BigDecimal Page 5

Today s Computer Arithmetic ALU FPU 12 x 12 144 1.2 x 1.2 1.44 byte, short, int, long, etc. single, float, double, quad, etc. Mike Cowlishaw Using Java 5.0 BigDecimal Page 6

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

Why Not Just Round? Rounding hides but does not help obscures the slight inaccuracies errors accumulate [double rounding] Decimal 9 0.9 0.09 0.009 Java float (binary) 9 0.9 0.089999996 0.009 Mike Cowlishaw Using Java 5.0 BigDecimal Page 8

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

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

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

but So now we know what to do Mike Cowlishaw Using Java 5.0 BigDecimal Page 12

... but it s very, very slow For example, typical Java BigDecimal add is 1,708 cycles, hardware might take 8 cycles software penalty add quantize multiply divide 210x 560x 90x 200x 40x 190x 260x 290x penalty = Java BigDecimal cycles DFPU clock cycles Mike Cowlishaw Using Java 5.0 BigDecimal Page 13

Effect on Real Applications The telco billing application 1,000,000 calls (two minutes) read from file, priced, taxed, and printed % execution time in decimal operations C, C# packages 72 78% Java BigDecimal 93.2% Mike Cowlishaw Using Java 5.0 BigDecimal Page 14

Effect on Real Applications [2] A Web Warehouse benchmark uses float binary for currency and tax calculations We added realistic decimal processing float binary decimal orders per second 3,862 1,193 69% of workload is decimal arithmetic Mike Cowlishaw Using Java 5.0 BigDecimal Page 15

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

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

IEEE 754r Formats size (bits) 32 64 128 digits 7 16 34 exponent range -95 to +96-383 to +384-6143 to +6144 Mike Cowlishaw Using Java 5.0 BigDecimal Page 18

The Java BigDecimal Class Mike Cowlishaw Using Java 5.0 BigDecimal Page 19

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

Constructors BigDecimal( String ) BigDecimal( double ) exact conversion BigDecimal( BigInteger [, int ] ) valueof( long [, int ] ) Mike Cowlishaw Using Java 5.0 BigDecimal Page 21

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

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

Conversions tostring, tobiginteger intvalue, longvalue (bytevalue and shortvalue inherited) these quietly decapitate! floatvalue, doublevalue Mike Cowlishaw Using Java 5.0 BigDecimal Page 24

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

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

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

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

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

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

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

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

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

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

Preferred MathContexts The MathContext class provides contexts which match the IEEE 754r sizes and default rounding (HALF_EVEN): DECIMAL32 DECIMAL64 DECIMAL128 (7 digits) (16 digits) (34 digits) (Also UNLIMITED, to match old behavior) Mike Cowlishaw Using Java 5.0 BigDecimal Page 35

Taking Advantage of Hardware BigDecimal objects are expensive BigDecimal scale precision ref BigInteger lookaside 1 lookaside 2 lookaside 3 lookaside 4 lookaside 5 lookaside 6 ref char [ ] int 1.. int 2.. Mike Cowlishaw Using Java 5.0 BigDecimal Page 36

Taking Advantage of Hardware BigDecimal with hardware lookaside (managed by the JIT compiler) BigInteger only created when number is too large for hardware (rare) BigDecimal scale precision (ref) Hardware lookaside storage Mike Cowlishaw Using Java 5.0 BigDecimal Page 37

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

Questions? Google: decimal arithmetic Mike Cowlishaw Using Java 5.0 BigDecimal Page 39

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

IEEE 754r Format Details Mike Cowlishaw Using Java 5.0 BigDecimal Page 41

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

Exponent continuation Sign Comb. field Exponent Coefficient Format exponent bits bias normal range 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

Coefficient continuation Sign Comb. field Exponent Coefficient Densely Packed Decimal 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