A Multiple-Precision Division Algorithm

Similar documents
A Multiple -Precision Division Algorithm. By David M. Smith

Additional Key Words and Phrases: Accuracy, elementary function evaluation, floating point, Fortran, mathematical library, portable software

General Terms: Algorithms, exponential integral function, multiple precision

Divide: Paper & Pencil

Learning Log Title: CHAPTER 3: ARITHMETIC PROPERTIES. Date: Lesson: Chapter 3: Arithmetic Properties

Bits, Words, and Integers

DLD VIDYA SAGAR P. potharajuvidyasagar.wordpress.com. Vignana Bharathi Institute of Technology UNIT 1 DLD P VIDYA SAGAR

COMPUTER ARCHITECTURE AND ORGANIZATION. Operation Add Magnitudes Subtract Magnitudes (+A) + ( B) + (A B) (B A) + (A B)

Chapter 03: Computer Arithmetic. Lesson 09: Arithmetic using floating point numbers

Chapter 4 Section 2 Operations on Decimals

Floating Point Arithmetic

Module 2: Computer Arithmetic

Number Systems. Both numbers are positive

Floating Point. The World is Not Just Integers. Programming languages support numbers with fraction

Arithmetic and Bitwise Operations on Binary Data

Arithmetic Processing

Section A Arithmetic ( 5) Exercise A

Computer Architecture and Organization

Integer Multiplication and Division

MATH LEVEL 2 LESSON PLAN 5 DECIMAL FRACTIONS Copyright Vinay Agarwala, Checked: 1/22/18

Chapter 2. Data Representation in Computer Systems

CO212 Lecture 10: Arithmetic & Logical Unit

By, Ajinkya Karande Adarsh Yoga

Math 340 Fall 2014, Victor Matveev. Binary system, round-off errors, loss of significance, and double precision accuracy.

UNIVERSITY OF MASSACHUSETTS Dept. of Electrical & Computer Engineering. Digital Computer Arithmetic ECE 666

CMPSCI 145 MIDTERM #1 Solution Key. SPRING 2017 March 3, 2017 Professor William T. Verts

(+A) + ( B) + (A B) (B A) + (A B) ( A) + (+ B) (A B) + (B A) + (A B) (+ A) (+ B) + (A - B) (B A) + (A B) ( A) ( B) (A B) + (B A) + (A B)

1620 DATA PRQCESSING SYSTEM BULLETlli. AUTOMATIC FLOATING-POINT OPERATIONS (Special Feature)

ECE260: Fundamentals of Computer Engineering

The Perils of Floating Point

On The Computational Cost of FFT-Based Linear Convolutions. David H. Bailey 07 June 1996 Ref: Not published

The ALU consists of combinational logic. Processes all data in the CPU. ALL von Neuman machines have an ALU loop.

CHAPTER 2 Data Representation in Computer Systems

Floating-point representation

Number Systems and Computer Arithmetic

Chapter 10 - Computer Arithmetic

Math Glossary Numbers and Arithmetic

Graphics calculator instructions

Basic Arithmetic Operations

Number System. Introduction. Natural Numbers (N) Whole Numbers (W) Integers (Z) Prime Numbers (P) Face Value. Place Value

COMPUTER ORGANIZATION AND ARCHITECTURE

Roundoff Errors and Computer Arithmetic

At the ith stage: Input: ci is the carry-in Output: si is the sum ci+1 carry-out to (i+1)st state

Exponential Notation

Kinds Of Data CHAPTER 3 DATA REPRESENTATION. Numbers Are Different! Positional Number Systems. Text. Numbers. Other

Unit Title Key Concepts Vocabulary CCS

COMPUTER ARITHMETIC (Part 1)

UNIT-III COMPUTER ARTHIMETIC

Internal Data Representation

CS321 Introduction To Numerical Methods

Floating-Point Numbers in Digital Computers

A. Incorrect! To simplify this expression you need to find the product of 7 and 4, not the sum.

Fixed-Point Math and Other Optimizations

Number Systems CHAPTER Positional Number Systems

Floating Point Considerations

NUMBER SCALING FOR THE LGP-27

Watkins Mill High School. Algebra 2. Math Challenge

Course Learning Outcomes for Unit I. Reading Assignment. Unit Lesson. UNIT I STUDY GUIDE Number Theory and the Real Number System

4 Operations On Data 4.1. Foundations of Computer Science Cengage Learning

CHAPTER 2 Data Representation in Computer Systems

5.4 Dividing Decimals

Logic, Words, and Integers

CHW 261: Logic Design

1. NUMBER SYSTEMS USED IN COMPUTING: THE BINARY NUMBER SYSTEM

In this lesson, we will use the order of operations to evaluate and simplify expressions that contain numbers and variables.

Numerical Methods in Scientific Computation

2 Computation with Floating-Point Numbers

Divisibility Rules and Their Explanations

SWALLOW SCHOOL DISTRICT CURRICULUM GUIDE. Stage 1: Desired Results

Floating-Point Numbers in Digital Computers

Timing for Ripple Carry Adder

Mathematical preliminaries and error analysis

Floating-Point Data Representation and Manipulation 198:231 Introduction to Computer Organization Lecture 3

Accuplacer Arithmetic Study Guide

1.1 Review of Place Value

Radix Conversion in an Unnormalized. Arithmetic System. By N. Metropolis and R. L. Ashenhurst

Gateway Regional School District VERTICAL ALIGNMENT OF MATHEMATICS STANDARDS Grades 3-6

Chapter 3 Arithmetic for Computers (Part 2)

THE LOGIC OF COMPOUND STATEMENTS

Digital Fundamentals. CHAPTER 2 Number Systems, Operations, and Codes

Rui Wang, Assistant professor Dept. of Information and Communication Tongji University.

Excerpt from: Stephen H. Unger, The Essence of Logic Circuits, Second Ed., Wiley, 1997

Adding Binary Integers. Part 5. Adding Base 10 Numbers. Adding 2's Complement. Adding Binary Example = 10. Arithmetic Logic Unit

Montana City School GRADE 5

KNOWLEDGE OF NUMBER SENSE, CONCEPTS, AND OPERATIONS

4/8/17. Admin. Assignment 5 BINARY. David Kauchak CS 52 Spring 2017

Math in MIPS. Subtracting a binary number from another binary number also bears an uncanny resemblance to the way it s done in decimal.

EE260: Logic Design, Spring n Integer multiplication. n Booth s algorithm. n Integer division. n Restoring, non-restoring

Computer Architecture Chapter 3. Fall 2005 Department of Computer Science Kent State University

MA 1128: Lecture 02 1/22/2018

!"!!!"!!"!! = 10!!!!!(!!) = 10! = 1,000,000

EXAMPLE 1. Change each of the following fractions into decimals.

Signed umbers. Sign/Magnitude otation

Errors in Computation

CIV Module Unit Session Learning Objectives

Reals 1. Floating-point numbers and their properties. Pitfalls of numeric computation. Horner's method. Bisection. Newton's method.

Fraction to Percents Change the fraction to a decimal (see above) and then change the decimal to a percent (see above).

4 Operations On Data 4.1. Foundations of Computer Science Cengage Learning

A.1 Numbers, Sets and Arithmetic

Mathematics Assessment Anchors and Eligible Content

Transcription:

Digital Commons@ Loyola Marymount University and Loyola Law School Mathematics Faculty Works Mathematics 1-1-1996 A Multiple-Precision Division Algorithm David M. Smith Loyola Marymount University, dsmith@lmu.edu Repository Citation Smith, David M., "A Multiple-Precision Division Algorithm" (1996). Mathematics Faculty Works. 2. http://digitalcommons.lmu.edu/math_fac/2 Recommended Citation Smith, D. M. A Multiple-Precision Division Algorithm, Mathematics of Computation. vol. 65 (1996) pp. 157-163. This Article is brought to you for free and open access by the Mathematics at Digital Commons @ Loyola Marymount University and Loyola Law School. It has been accepted for inclusion in Mathematics Faculty Works by an authorized administrator of Digital Commons@Loyola Marymount University and Loyola Law School. For more information, please contact digitalcommons@lmu.edu.

MATHEMATICS OF COMPUTATION Volume 65, Number 213 January 1996, Pages 157-163 A MULTIPLE-PRECISION DIVISION ALGORITHM DAVID M. SMITH ABSTRACT. The classical algorithm for multiple -precision division normalizes digits during each step and sometimes makes correction steps when the initial guess for the quotient digit turns out to be wrong. A method is presented that runs faster by skipping most of the intermediate normalization and recovers from wrong guesses without separate correction steps. 1. INTRODUCTION Knuth [3] described classical algorithms for multiplication and division using t digits of precision with base-b arithmetic. These methods require 0(t2) operations. Knuth also surveyed several algorithms with faster asymptotic running time. Bailey's multiple-precision package [1] included routines based on FFT methods for use at very high precision, as well as 0(t2) routines. The FFT methods are faster asymptotically, but because the algorithms are complicated, the classical 0(t2) methods are more efficient when the precision is less than a few thousand digits. For multiplication, Brent's package [2] improved on the classical methods by restricting b so that integer overflow could not occur for numbers less than 8b2. This allowed his routine to do eight steps of the multiplication before normalizing, resulting in significantly faster execution. The FM multiplication routine [7] was based on this idea and reduced the number of normalizations still further. Here ''normalizing" refers to the act of restoring the individual digits of an intermediate result to the range [0, b - 1]. It is more difficult to skip normalization during division, because the quotient digit q used in each "multiply and subtract" step is a guess. Early routines ([3, 4, 5, 6, 8]) used integer arithmetic to estimate q. Bailey used double precision to estimate q, which made the probability of an incorrect q much smaller. Brent used Newton iteration and multiplication to avoid the long-division algorithm, but this made division much slower than multiplication. The early routines normalized the partial result at each step so that cases where q was wrong could be recognized and corrected. This correction took the form of an extra "addback" step that was done so the value of q would be correct before the next multiply and subtract step was taken. Since a large base was usually used, the correction step was needed only rarely. This means that eliminating the correction step makes the division routine only slightly faster. The time spent normalizing partial results could be greater than the time spent generating them, so minimizing the normalization is important. Received by the editor June 17, 1994 and, in revised form, February 12, 1995. 1991 Mathematics Subject Classification. Primary 65-04, 65D15. 157 (@)1996 American Mathematical Society

158 D. M. SMITH In version 1.0 of the FM package, the division routine normalized only a few leading digits of the partial result so that q could be verified or a correction step taken. Bailey's division routine eliminated the correction step, but normalized each partial result. This paper presents a division algorithm that eliminates most of the intermediate normalization and does no separate correction steps. This algorithm still requires 0(t2) steps, but is often two or three times faster than previous 0(t2) methods ([1, 2, 7]). 2. EXAMPLES The method is similar enough to the standard long-division algorithm that the differences can be seen using the example quotient 7r/e. A floating-point division will be done, although the method works in the same way for multiple-precision integer division. Commonly used bases are in the range [10', 10']. The examples use b = 104, t = 5, and compute six digits of the quotient to simulate the guard digits carried by division routines. To begin, the numerator is copied into a work array, W, with zero digits padded at the end to leave room for guard digit calculations. The numbers are stored in a floating-point format where the first word is the exponent (as a power of b) followed by the digits of the fraction part: n= 1 3 1415 9265 3589 7932 d= 1 2 7182 8182 8459 0452 As the division proceeds, the "active" part of the work array shifts to the right, and most routines store the quotient digits in the left part of the array. The first word of W is the exponent of the quotient: W= - 1 0 3 1415 9265 3589 7932 0 0 0 In this example, four words of each number are used to generate the floatingpoint approximations. Since they will be divided to get the quotient digits, both can be scaled by any convenient factor. The numerator value, xn, will be scaled to be an integer, and the denominator value, Xd, is 271828182.8459. The approximation to each quotient digit is q = LXn/Xdji Step 1. Use words 2 through 5 of W to get xn: xn = 314159265.0, q = 1. Multiply and subtract: W <- W-q * d W is displayed on two lines to show the current quotient and the active part of the array: W= 1 0 1-5767 1083-4870 7480 0 0 0 In step k, Wk+1 is often nonzero. One word of W is normalized by setting Wk+2 = b * Wk+l + Wk+2, and then q is stored in Wk+1: WN= 1 1 1-5767 1083-4870 7480 0 0 0 Step 2. Use words 3 through 6 for xn: xn = 423310825130.0, q = 1557. W= 1 1 1557 1119-11181291 -12744244-13163183 -703764 0 0 Step 3. Use words 4 through 7 for xn: xn = 743444396817.0, q = 2734. W= 1 1 1557 2734 3241-32379832 -35532771-23830670 -1235768 0

A MULTIPLE-PRECISION DIVISION ALGORITHM 159 Step 4. Use words 5 through 8 for x,: x, = 2661448459330.0, q = 9790. W= 1 1 1557 2734 9790 10588-105844551 -103932450-84049378 -4425080 Step 5. Use words 6 through 9 for x,: x, = 2505491450622.0, q = 9217. W 1 1 1557 2734 9790 9217 17015-170128944 -159462872-82391683 Step 6. Use words 7 through 10 for x,: xn = 510888888317.0, q = 1879. W= 1 1 1557 2734 9790 9217 1879 17298-172957850 -97765661 Now the result can be rounded to five digits and the value 1.1557273497909217 returned. This example shows that the active part of W can be kept unnormalized. When xn is computed using double-precision arithmetic, four words of W are essentially normalized during the calculation. No attempt is made to check that the quotient digit q is correct. The next example shows what would happen if q were one too high in step 3 above. The first two steps are the same as before. Step 3. Use q = 2735. W 1 1 1557 2735 3239-32387014 -35540953-23839129 -1236220 0 Step 4. Use words 5 through 8 for xn: xn -56833369129.0, q -210. W 1 1 1557 2735-210 3406-34032733 -22120909-540170 -94920 Step 5. Use words 6 through 9 for xn: xn 2505491450622.0, q 9217. W 1 1 1557 2735-210 9217 8833-88317403 -74873324-77871683 Step 6. Use words 7 through 10 for xn: xn 510888888317.0, q 1879. W 1 1 1557 2735-210 9217 1879 8839-88368302 -93245661 This shows that an incorrect q leads to unnormalized quotient digits later, so a final pass is needed to normalize W before returning the result. This gives the same value as in the first example. Step 4 does the missing correction from step 3 at the same time as the normal multiply and subtract operation from step 4. The negative q = -210 = -10,000 + 9790 can be interpreted as subtracting 1 from the previous q while also using q 9790 for the current step. The final normalization pass moves the -1 to the correct word. 3. DETAILS OF THE ALGORITHM The algorithm will be given using Mathematica [9]. This version does not handle various special cases such as zero denominator, and has not been tuned for efficiency. Both arguments are assumed to be positive, with length at least five. No provision is made for normalizing the digits of W to avoid overflow. If rounding is to be fairly accurate, the base b should be at least 100. For a full implementation, see the Fortran subroutine in FMLIB 1.1. RealW[ j_ ] := N[ ((W[[j-l]]*b + W[[j]])*b + W[[j+l]])*b + If[ Length[W]>=j+2, W[[j+2]1, 0 1 1;

A MULTIPLE-PRECISION DIVISION ALGORITHM 161 Using four words of W to compute x, gives good accuracy for q. Suppose n is the exact value of the active part of W, scaled so that xn is an integer. Then during step k, n = 3 2 ~~~~Wk?5 Wk+1 b + Wk+2 b + Wk+3 b + Wk+4 + b The goal is to approximate n/d, where d is the scaled denominator d =d2 b2+ d3 b + d4 + d5 + d6 + b b2 Because d is normalized, d > b2. The quantities xn and xd are defined as the first four terms of these sums. We will bound the error in the estimate q. Suppose that the value q used in step k - 1 is no more than one in error. Then the correct quotient digit q* = [n/dj for step k satisfies -b < q* < 2b. So q* < n/d < q* + 1 implies In/dl < 2b + 1. After step k, IWi I < k b2. This means In-xnI < 2 k b. Since Id-xd < 1/ b < dlb3, we have X <d In< +2 k b < 2 b + 3 + 3 k Xd d (I1-b3) b The error in q is n Xn X d ( Xn) xn (dxd) < X x d -Xd d Xd dxd - d Xd d 2k 2k+4 <+ When a large base is used (b4 > T), it often happens that xn and xd are not as accurate as assumed above, because of double-precision rounding errors. In this case, we can assume that the two values actually computed, Yn and Yd, agree with xn and xd to about double precision accuracy. Then Xn - Yn < and~~ Xd -Yd< Xn - and X b2 - Here the quotient digit used is q - LYn/Yd j. Then Yn Xn < Xn Yn-Xn + Xn Yd-Xd < 4 + b1;+13 Yd Xd Yd O Xn Yd Xd b b The inequalities above assume b > 2, and the bound on the error in q could be made slightly smaller for the case where b is large. For the usual case where b > 104, having q - q* K 1 in step k - 1 implies that the quotient digit used in step k is also no more than one in error. This indicates that when b is large, using the unnormalized values in W gives an estimate q that is never more than one in error, and the probability of q being wrong is reasonably small. The algorithm is still correct when the base is small, even though quotient digits may be used that are more than one in error. The final normalization pass takes

162 D. M. SMITH longer because more unnormalized digits are generated initially. Another possible strategy for dealing with a small base is to use more than four words to generate xn and Xd. The other practical matter that must be considered for a routine using this algorithm involves avoiding overflow or loss of precision. This can occur when the unnormalized intermediate results become too large. Suppose W is implemented as an integer array, b = 104, and the overflow threshold is 231_I = 2,147,483,647. Since 21b2 will not overflow and each q is less than 2b, 10 steps can be taken before normalizing the active part of W. As implemented in [7], the routine uses the digits being multiplied to get a sharper upper bound on the elements of W. The average q is about b/2, so there are approximately 40 steps between normalization. In a more recent version of [7], W is a double precision array. The threshold for representing integers exactly in double precision is T = 2-1 9.01 x 1015 on most 32-bit machines. In this case the commonly used base is 107, so about 180 steps can be done before normalizing. Each time the digits of W are normalized to avoid overflow, the value of k is reset to 1 in the above bounds on the error in q. This means the accuracy of the quotient digits does not degrade much, even if high precision is used. The main reason for guaranteeing that q is within one of the correct value is to avoid cases where the next q cannot correct an error in the previous step. Suppose in the double-precision case that b = 107 and q is 100 in error. In the next step, q will be about loob in magnitude. Since the elements of d to be multiplied by q may be as large as b - 1, these products might exceed T. This could result in some products not being exact. To normalize W at this point would not solve the problem a more complicated correction step would be needed. When smaller bases are used for the arithmetic, correspondingly larger errors in q can be tolerated by the algorithm. 4. CONCLUSION This technique for eliminating most of the intermediate digit normalization operations can be used to perform multiple-precision division faster than previous methods. A routine implementing this method was compared to the division algorithm used in FMLIB 1.0, which normalizes several digits of the intermediate result at each step. Running on a 68040 Macintosh, the new version was 2.2 times faster at 40 significant digits, and 1.8 times faster at 500 digits. Similar speed improvements were found in testing on other types of computer. The theoretical complexity for division is the same as that for multiplication, but in most multiple-precision packages division is much slower, even at high precision. In tests of this algorithm, the ratio of running times for division and multiplication ranged from 1.54 at 40 decimal digits to 1.19 at 1,000 digits. Since the intermediate results are not normalized often, the main difference in running time is due to the double precision calculation of xn and q at each step. This means that at high precision, division is almost as fast as multiplication. REFERENCES 1. D.H. Bailey, Algorithm 719: Multiprecision translation and execution of FORTRAN programs, ACM Trans. Math. Software 19 (1993), 288-319. 2. R.P. Brent, A Fortran multiple-precision arithmetic package, ACM Trans. Math. Software 4 (1978), 57-70.

A MULTIPLE-PRECISION DIVISION ALGORITHM 163 3. D.E. Knuth, The art of computer programming, Vol. 2: Seminumerical algorithms, 2nd ed., Addison-Wesley, Reading, MA, 1981. MR 83i:68003 4. E.V. Krishnamurthy and S.K. Nandi, On the normalization requirement of divisor in divideand-correct methods, Comm. ACM 10 (1967), 809-813. 5. C.J. Mifsud, A multiple-precision division algorithm, Comm. ACM 13 (1970), 666-668. 6. D.A. Pope and M.L. Stein, Multiple precision arithmetic, Comm. ACM 3 (1960), 652-654. MR 22:7277 7. D.M. Smith, Algorithm 693: A Fortran package forfloating-point multiple-precision arithmetic, ACM Trans. Math. Software 17 (1991), 273-283. 8. M.L. Stein, Divide-and-correct methods for multiple precision division, Comm. ACM 7 (1964), 472-474. 9. S. Wolfram, Mathematica: A system for doing mathematics by computer, 2nd ed., Addison- Wesley, Redwood City, CA, 1991. DEPARTMENT OF MATHEMATICS, LOYOLA MARYMOUNT UNIVERSITY, Los ANGELES, CALIFORNIA 90045 E-mail address: dsmithiolmumnailmu. edu