Mastering Multiprecision Arithmetic
|
|
- Maurice Owens
- 5 years ago
- Views:
Transcription
1 Mastering Multiprecision Arithmetic Norman Ramsey April 2018 Introduction Uncivilized programming languages provide crappy integer arithmetic. You better hope your results fit in a machine word if they don t, your program might silently produce wrong answers. You can do this easily enough using Impcore or µscheme. Civilized languages provide integer arithmetic that works on any integer up to the size you can fit in RAM. By this standard, Standard ML is not quite civilized: arbitrary-precision arithmetic is in the specification as module IntInf, but implementations don t have to provide it. Full Scheme, on the other hand, is supercivilized if you re interested in arithmetic, it s worth studying. Arithmetic plays a prominent role in the last two projects, for these reasons: Civilized arithmetic is a topic that every educated computer scientist should know about if only so that you can insist on it for your projects. Don t tolerate uncivilized arithmetic! Arithmetic brilliantly illustrates the relative strengths and weaknesses of the two major ways of abstracting over data: abstract data types versus objects. Basics A natural number is represented by the sum 0 i<n x i b i. b is called the base of natural numbers. Each x i is called a digit. It satisfies the invariant 0 x i < b. Hardware integer arithmetic universally uses base b = 2. But software arithmetic, which you will implement, typically uses a base b = 2 k, where k is chosen as large as possible, subject to the constraint that (b 1) 2 can be computed without overflow. Be cautious! Moscow ML provides only 31 bits of integer precision. To help you get started, it is reasonable to begin with val base = 10 Once your code is working, you ll want to replace 10 with a larger number. Performance matters. Larger b gives better performance. Choice of representation A natural number should be represented by a sequence of digits. But this still leaves you with interesting design choices: Do you prefer an array or a list? If you prefer an array, do you want a mutable array (type 'a array) or an immutable array (type 'a vector)? If you prefer a list, do you prefer to store the digits of a number in big-endian order (most significant digit first) or little-endian order (least significant digit first)? (If you prefer an array, the only order that makes sense is to store the least significant digit x 0 in slot 0, and in general, to store digit x i in slot i. Anything else would be confusing.) If you prefer an array, how will be you be sure it contains enough digits to hold the results? If you prefer a list, how will you make sure you produce a list with the right number of digits? Some implementations of arithmetic operations, especially multiplication, can leave leading zeroes in a representation. The number of leading zeroes must be controlled so that it doesn t grow without bound. Do you prefer a representation invariant that eliminates leading zeroes? If not, how will you track the number of leading zeroes in each representation? Will you store it in the representation itself, or will you compute it dynamically? The beauty of data abstraction is that the interface isolates these decisions, so you can easily change them. Representing individual digits You can represent a digit by a value of type int. But I recommend against it: a value of type int is too easily confused with an array index. Instead, I recommend that you define datatype digit = D of int and use digit list, digit array, or digit vector in your representation. You will have to do a little extra pattern matching, but in order to avoid confusing an index with the digit at that index, the extra pattern matching is well worth it. 1
2 Mutable representation? Really? provided p is not too large as above *) If you scrutinize the types and contracts of the functions in the NATURAL interface, you ll see that the nat abstraction is immutable: nothing in the interface allows a client to change the value of a natural number. So why would it be OK to choose a mutable representation, like a digit array? Simple: you re allowed to pick any representation you like, because the details are sealed behind the interface. As long as you don t make a silly mistake like allow x /+/ y to mutate x or y, no client program can tell that your representation is mutable. In industrial implementations of multiprecision numbers, mutable representations are quite popular: they minimize allocation and they use memory efficiently. It s fine to pick a mutable representation, as long as no client code can tell the difference. Tackling arithmetic in Standard ML If you study the NATURAL signature, you should observe that the first step in creating any natural number is always to call of_int, and the only way to get information out of a natural number is to call decimals. But you will have an easier time if you start with two auxiliary functions, which are closely related. If your representation is immutable, here are their specifications: val addint : nat * int -> nat (* addint (x, p) returns a natural number that represents x + p, provided the following condition holds: *) for every digit x_i in x, (p + x_i) can be computed without overflow val shiftadd : int * int * nat -> nat (* shiftadd (p, n, x) = p * power (base, n) + x, provided the same condition above holds *) These two functions are related by this algebraic law: shiftadd (p, 0, x) = addint (x, p) This law can be used in either direction: you can use shiftadd to implement addint, or you can use addint to implement shiftadd. Which direction is easier depends on your choice of representation. If your representation is mutable, the types and contracts will be slightly different: val addint : nat * int -> unit (* addint (x, p) overwrites x with x + p, provided p is not too large as above *) Implementing function of_int Once you have addint, you can use it to implement of_int. Depending on the mutability of your chosen representation, the details vary. Here s the immutable version: fun of_int n = (* immutable representation *) if n < 0 then raise Negative else let val zero =... something suitable... in addint (zero, n) end The mutable version is the same, except you call addint for side effect and then return zero. Implementing short division using arrays In short division, a multi-digit number is divided by a single digit. The algorithm is one you learned in primary school. The algorithm is discussed in an excerpt from the Spring 2017 edition of Programming Languages: Build, Prove, and Compare. (The excerpt is appended to this handout.) Also, a full implementation is presented in David R. Hanson s C Interfaces and Implementations, on pages 311 and 312 (the XP_quotient function). 1 What Ramsey calls a remainder r i is called carry by Hanson. Short division loops down from most significant digit to least significant digit. If you re dividing x by d, at each step i we have q i = (r in b + x i ) div d r out = (r in b + x i ) mod d This algorithm works for any d in the range 0 < d b. If you are using an array representation, you can write the loop using higher-order function Array.foldri. Try help "Array"; in Moscow ML. The accumulating parameter holds the remainder r in. The final remainder out is returned with the quotient. Implementing short division inductively If you are using a little-endian list representation, short division can work with the same inductive structure we use for addition, subtraction, and multiplication. Here s a mathematical sketch. We are given a natural number n and a one-digit divisor v. We want to find quotient q (a natural number) and remainder r (a machine integer in the range 0 r < v) such that val shiftadd : int * int * nat -> unit (* shiftadd (p, n, x) overwrites x with p * power (base, n) + x, n = q v + r. 1 If you took COMP 40 at Tufts, you may have this book. If not, it is part of the university s Safari subscription, so your access to it is already paid for. 2
3 We calculate quotient and remainder inductively, using the proof system for natural numbers base b. In the base case, n = 0, q = 0, and r = 0. In the inductive step, n = m b + d, where b is the base of natural numbers, and d is a digit in the range 0 d < b. To solve this case, we have to solve two smaller division problems: Find q and r such that m = q v+r and 0 r < v. This problem can be solved by a recursive call to the short divider. Find q 0 and r such that r b + d = q 0 v + r and 0 r < v. Provided (b 1) (v 1) can be represented as a machine integer, this problem can be solved by using div and mod on machine integers. We then combine the solutions to get q = q b + q 0, which is a natural number. We can prove this solution correct using an equational proof equating q v + r to m b + d, in five steps. This information can be expressed neatly using algebraic laws: 0 div v = 0 0 mod v = 0 (m b + d) div v = (m div v) b + ((m mod v) b + d) div v (m b + d) mod v = ((m mod v) b + d) mod v These laws must not be used directly for implementation. Why not? Because in a direct implementation, the inductive case calls both div and mod recursively. That s an exponential number of calls. For implementation, you need to compute both m mod v and m div b in a single call. Using short division for base conversion To convert a number from a large base to a smaller base, you continually short-divide by the small base until you run out of digits. Each short division produces a remainder, which is a digit of the result. The least-significant digit is produced first. Here s an example using repeated division by 2, converting decimal 11 to binary: 11 2 = 5 remainder = 2 remainder = 1 remainder = 0 remainder 1 Reading off the digits in order says that decimal 11 is binary There is a similar problem on the homework, where you complete this table: 13 2 = q 0 remainder r 0 q 0 2 = q 1 remainder r 1 q 1 2 = q 2 remainder r 2 q 2 2 = q 3 remainder r 3 The converted numeral is the sequence of digits r 3 r 2 r 1 r 0 (in traditional big-endian order). Implementing decimals When you have a large base, short division is the way you implement decimals. You have to get this right or your code won t pass any test cases. Here is part of a definition of decimals, using a mutable representation: fun reverse_decimals x = if... x is zero... then [] else let val { quotient = q, remainder = r } = divdestroy (x, 10) in r :: reverse_decimals q end Function divdestroy divides x by 10 and overwrites x. Code for an immutable representation is about the same. Implementing addition and subtraction If you are using an array to represent a sum or product, you will need to provide it with enough digits in advance: For addition, you may need as many digits as are in the larger addend, plus one more. For subtraction, you may need as many digits as are in the minuend (left-hand operand). Subtraction may leave you with a representation that contains leading zeroes. If leading zeroes violate your representation invariant, you ll have to do something about them. Both addition and subtraction work from the least significant digit to the most significant digit. Array users can use Array.foldli or Vector.foldli. The borrowing needed to subtract is slightly more fiddly than the carrying needed to add. I found it useful to define a helper function that subtracts a borrow bit from a single natural number. 3
4 Addition For addition, the accumulating parameter is a carry in. The key step is to produce a sum of digits from the two addends, plus the carry in. Part of that some becomes a digit of the result, and part becomes the carry out. Code might look something like this: let val s = x_i + y_i + carry_in val carry_out = s div base val sum_i = s mod base in... do something with sum_i and carry_out... end What happens in the... depends on your representation, but sum_i becomes a digit in the output, and carry_out becomes the carry_in on the next cycle. The final carry out should be zero. If not, you didn t allocate enough digits, and there is a bug in your code. Subtraction Subtraction uses the same looping structure as addition, but the computation is a little more complicated. If the difference goes negative, we have to borrow a from the next most significant digit (the borrow_out ). let val initial_diff = x_i - y_i - borrow_in val (borrow_out, diff_i) = if initial_diff < 0 then (1, initial_diff + base) else (0, initial_diff) in... do something with diff_i and borrow_out... end If your final borrow_out is nonzero, the result of subtraction is negative, and you need to raise the Negative exception. Implementing comparison The final operation you meet to implement compares two natural numbers and returns EQUAL, LESS, or GREATER. You can approach the problem the direct way or the indirect way. The direct approach looks at the digits. If x = x b + x 0 and y = y b + y 0 then you first compare the more significant digits x and y. If they are EQUAL, return Int.compare (x 0, y 0 ). Otherwise, x and y compare the same as x and y. In the indirect approach you implement < by using /-/ and seeing if it raises Negative. Then you use < to implement compare. The problem with the direct approach is that depending on your representation, comparing a long number with a short number could be tricky especially if your representation permits leading zeroes. The problem with the indirect approach is that it allocates at least one natural number, then throws it away. 2 Implementing multiplication Done carefully, multiplication is the easiest operation to implement. The equation for a product is x y = 0 i<n 0 j<m (x i y j ) b i+j. The number of digits needed to hold the double sum is potentially the total number of digits in x and y together: m + n. And the algorithm couldn t be simpler: it s a doubly nested loop in which each iteration adds the partial product (x i y j ) b i+j to a running total. Call shiftadd(x i y j, i + j, total). I recommend using Array.foldli or Vector.foldli on an array representation, and foldli or foldri on a list representation. (You will have to define foldli or foldri on lists.) The accumulating parameter passed to the fold is the running total or if you are using a mutable representation, the best accumulating parameter is the empty tuple (value () of type unit). 2 While I find the indirect approach intellectually unsatisfying, I can t rebut the argument that says "the garbage collector is good at reclaiming short-lived objects; shut up and let it do its job. 4
5 604 CHAPTER 8. CLU, ABSTRACT DATA TYPES, AND MODULES Sidebar: Notation: Multiplication, visible and invisible Mathematicians and physicists often multiply quantities simply by placing one next to another; for example, in the famous equation E = mc 2, m and c 2 are multiplied. But in a textbook on programming languages, this notational convention will not do. First, it is better for multiplication to be visible than to be invisible. And second, when one name is placed next to another, it usually means function application at least that s what it means in ML, Haskell, and the lambda calculus. Among the conventional infix operators, * is more suited to code than to mathematics, and the symbol is better reserved to denote a Cartesian product in a type system. In this book, on the rare occasions when we need to multiply numbers, I write an infix, so Einstein s famous equation would be written E = m c 2. Completing the implementation of int-set and measuring the effect of the optimized union operation is the subject of Exercise 7 on page Arbitrary-precision integer arithmetic Values of type int aren t true integers; they are machine integers. For much of the history of computing, a typical machine integer has been represented by a half word or full word of the hardware of the day. In the 21st century, that usually means 32 or 64 bits. Using 64 or even 32 bits is good enough for most purposes, but some computations need more precision; examples include some cryptographic computations as well as exact rational arithmetic. In many languages, arbitrary-precision integers are available as an abstraction, and in highly civilized languages like Scheme, Smalltalk, and Python, arbitrary precision is the default. You can implement this abstraction. In this chapter, I encourage you to implement it using abstract data types, and in Chapter 11, I encourage you to implement it again using objects. The similarities and differences illuminate what abstract data types are good at and what objects are good at. An implementation of arbitrary-precision arithmetic begins with natural numbers the nonnegative integers. And the standard representation is a sequence of digits indexed from zero. A natural number X is represented by a sequence of digits x 0,...,x n. The representation s meaning depends on the choice of a base, written b. In everyday life, we use base b = 10, and we write the most significant digit x n on the left. In hardware, our computers famously use base b = 2; the very word bit is a contraction of binary digit. Whatever b is, the digits x 0,...,x n stand for the number X = n x i b i. i=0 Moreover, the representation satisfies the invariant that for each i, 0 x i < b. Algorithms for addition, subtraction, multiplication, and division are independent of b. For COMP 105, Tufts University, Spring 2017 only --- do no
6 8.9. INSPECTING MULTIPLE REPRESENTATIONS 605 Addition and subtraction When we want to add an n-digit number X to an m-digit number Y, we compute ( n ( m ) X +Y = x i b )+ i y j b j = = i=0 max(m,n) i=0 max(m,n) i=0 j=0 x i b i +y i b i (x i +y i ) b i Unfortunately, the sum of two digits is not necessarily a digit: we might have x i +y i > b. For example, if b = 10, the sum 7+8 is not a digit. But we can express the sum if we use an additional carry bit: = There may be a carry in as well; here is an example in the style of elementary school: The superscript 1 over the 7 is the carry bit from adding 3 and 9. In the general case, we add X and Y as follows: at every nonnegative index i we add input digits x i and y i to the carry in c i, and we produce output digit z i and carry out c i+1, which satisfy the equation x i +y i +c i = z i +c i+1 b. Because for any nonnegative z, z = z mod b+(zdivb) b, the equation is satisfied by z i = (x i +y i +c i ) mod b c i+1 = (x i +y i +c i )divb The initial carry in c 0 is zero. In the specific case shown above, z 0 +c 1 b = x 0 +y 0 +c 0, where x 0 = 3,y 0 = 9,c 0 = 0,z 0 = 2,c 1 = 1 z 1 +c 2 b = x 1 +y 1 +c 1, where x 1 = 7,y 1 = 8,c 1 = 1,z 1 = 6,c 2 = 1 z 2 +c 3 b = x 2 +y 2 +c 2, where x 2 = 0,y 2 = 0,c 2 = 1,z 2 = 1,c 2 = 0 In Exercises 39 and 40, you implement the general case in µclu. Once all the input digits and carries are zero, you re done. Subtraction is like addition, except the carry bit is called a borrow, and digit z is computed from the difference x i y i c i. If this difference is negative, you must borrow b from a more significant digit, exploiting the identity z i+1 b i+1 +z i b i = (z i+1 1) b i+1 +(z i +b) b i. For COMP 105, Tufts University, Spring 2017 only --- do no
7 606 CHAPTER 8. CLU, ABSTRACT DATA TYPES, AND MODULES Multiplication To compute a product, we compute ( ) ( ) X Y = x i b i y j b j = i i (x i y j ) b i+j j Again, to satisfy the representation invariant, the partial product (x i y j ) b i+j has to be split into two digits ((x i y j ) mod b) b i+j + ((x i y j )divb) b i+j+1. Then all the partial products are added. Here s an example: j Short division Longdivision, whereyoudivideonebignumberbyanother, isbeyondthe scopeofthisbook. Consult Hanson (1996) or Brinch Hansen (1994). But short division, where you divide a big number by a digit, is within the scope of the book, and it is useful for implementing print. If X is a large natural number and d is a digit, we can compute quotient Q, which is large, and remainder r, which is small. The algorithm uses these variables: b Base of multiprecision arithmetic X Dividend d Divisor d < b Q Quotient r Remainder, always 0 r < d x i Digit i of dividend, 0 x i < b q i Digit i of quotient, 0 q i < b r i Remainder in at digit i, 0 r i < d Remainder out at digit i, 0 r i < d r i We are given b, d, and all the x i s, and we want to find r, all the q i s, and on the way, all the r i s and r i s. Here are the equations: X = i x i b i Q = i q i b i q i = (r i b+x i)divd r = r 0 r i = (r i b+x i ) mod d r i 1 = r i r n = 0 For COMP 105, Tufts University, Spring 2017 only --- do no
8 8.9. INSPECTING MULTIPLE REPRESENTATIONS 607 The basic law of short division, justified by the definitions of q i and r i, is q i d+r i = b r i +x i. Theq i sandr i sarecomputedfromthemostsignificanttotheleastsignificant. Forexample, 738 divided by 4 is 184, with remainder (remainder 2) The answer is computed by a loop that iterates three times: once to compute each pair of q i and r i. The steps of the computation may be summarized as follows: 1. X = d = 4 3. n = 2 4. r 2 = 0 5. x 2 = 7 6. q 2 = (0 10+7)div4 = 1 7. r 2 = (0 10+7) mod 4 = 3 8. x 1 = 3 9. q 1 = (3 10+3)div4 = r 1 = (3 10+3) mod 4 = x 0 = q 0 = (1 10+8)div4 = r 0 = (1 10+8) mod 4 = r = r 0 = 2 Implementing arithmetic using abstract data types When we re implementing numbers, from complex numbers to fractions to arbitraryprecision integers, the representation is hidden. Client code that uses natural numbers doesn t know that the representation is a sequence of digits interpreted with respect to a fixed base b. But when we use abstract data types to implement the abstraction, as in CLU, the implementation code knows the representation of every argument of abstract type, so adding, subtracting, and multiplying pairs of numbers is easy: the static type guarantees the representation we expect. Using abstract data types to implement arithmetic also has a cost: the static type system prevents numbers of different types from interoperating. For example, there is no way to subtract a machine integer from an arbitrary-precision natural number you must first coerce the machine integer to a natural number. This limitation makes such mixed arithmetic much less convenient than it is in an object-oriented language like Smalltalk (Chapter 11, Exercises 32 to 34) Priority queues optimized for merging As a final example of code that benefits from inspecting multiple representations in a single operation, we return to priority queues. The priority queue in Section on page 590 uses a representation that is often taught to undergraduate students: a complete binary tree represented by an array. This representation offers insertion and removal in O(log N) time, but it does not offer a merge operation; the only way to combine two heaps is to remove all the elements from one and add them to the other just like the naïve version of set union above. The underlying arrays aren t sorted, so there s no easy, efficient way to merge them. 8 In many languages, including C and C++, the compiler inserts coercions automatically between, say, integer types and floating-point types. But this privilege cannot be extended to user-defined types like arbitrary-precision integers. For COMP 105, Tufts University, Spring 2017 only --- do no
Program Design with Abstract Data Types
Program Design with Abstract Data Types Norman Ramsey Fall 2018 1 Introduction Our 9-step design process is intended for functions. Abstract data types introduce new problems, which are dealt with by means
More informationCOMP 105 Assignment: Smalltalk
Due Friday, April 15, at 5:59PM. Extended to Sunday, April 17, at 11:59PM. The purpose of this assignment is to help you get acquainted with pure object-oriented programming. The assignment is divided
More informationExcerpt from "Art of Problem Solving Volume 1: the Basics" 2014 AoPS Inc.
Chapter 5 Using the Integers In spite of their being a rather restricted class of numbers, the integers have a lot of interesting properties and uses. Math which involves the properties of integers is
More informationCOMP 105 Assignment: Smalltalk
COMP 105 Assignment: Smalltalk Due Sunday, April 19, at 11:59 PM. The purpose of this assignment is to help you get acquainted with pure object-oriented programming. The assignment is divided into two
More informationProperties and Definitions
Section 0.1 Contents: Operations Defined Multiplication as an Abbreviation Visualizing Multiplication Commutative Properties Parentheses Associative Properties Identities Zero Product Answers to Exercises
More information/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Priority Queues / Heaps Date: 9/27/17
01.433/33 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Priority Queues / Heaps Date: 9/2/1.1 Introduction In this lecture we ll talk about a useful abstraction, priority queues, which are
More informationLecture 1 Contracts. 1 A Mysterious Program : Principles of Imperative Computation (Spring 2018) Frank Pfenning
Lecture 1 Contracts 15-122: Principles of Imperative Computation (Spring 2018) Frank Pfenning In these notes we review contracts, which we use to collectively denote function contracts, loop invariants,
More informationDivisibility Rules and Their Explanations
Divisibility Rules and Their Explanations Increase Your Number Sense These divisibility rules apply to determining the divisibility of a positive integer (1, 2, 3, ) by another positive integer or 0 (although
More informationTwo approaches to writing interfaces
Two approaches to writing interfaces Interface projected from implementation: No separate interface Compiler extracts from implementation (CLU, Java class, Haskell) When code changes, must extract again
More informationCOMP 105 Homework: Type Systems
Due Tuesday, March 29, at 11:59 PM (updated) The purpose of this assignment is to help you learn about type systems. Setup Make a clone of the book code: git clone linux.cs.tufts.edu:/comp/105/build-prove-compare
More informationArithmetic Processing
CS/EE 5830/6830 VLSI ARCHITECTURE Chapter 1 Basic Number Representations and Arithmetic Algorithms Arithmetic Processing AP = (operands, operation, results, conditions, singularities) Operands are: Set
More informationArithmetic and Bitwise Operations on Binary Data
Arithmetic and Bitwise Operations on Binary Data CSCI 2400: Computer Architecture ECE 3217: Computer Architecture and Organization Instructor: David Ferry Slides adapted from Bryant & O Hallaron s slides
More informationChapter 4 Section 2 Operations on Decimals
Chapter 4 Section 2 Operations on Decimals Addition and subtraction of decimals To add decimals, write the numbers so that the decimal points are on a vertical line. Add as you would with whole numbers.
More informationLearning the Binary System
Learning the Binary System www.brainlubeonline.com/counting_on_binary/ Formated to L A TEX: /25/22 Abstract This is a document on the base-2 abstract numerical system, or Binary system. This is a VERY
More informationMAT 003 Brian Killough s Instructor Notes Saint Leo University
MAT 003 Brian Killough s Instructor Notes Saint Leo University Success in online courses requires self-motivation and discipline. It is anticipated that students will read the textbook and complete sample
More informationLecture 1 Contracts : Principles of Imperative Computation (Fall 2018) Frank Pfenning
Lecture 1 Contracts 15-122: Principles of Imperative Computation (Fall 2018) Frank Pfenning In these notes we review contracts, which we use to collectively denote function contracts, loop invariants,
More informationChapter 2. Data Representation in Computer Systems
Chapter 2 Data Representation in Computer Systems Chapter 2 Objectives Understand the fundamentals of numerical data representation and manipulation in digital computers. Master the skill of converting
More informationLecture 2: SML Basics
15-150 Lecture 2: SML Basics Lecture by Dan Licata January 19, 2012 I d like to start off by talking about someone named Alfred North Whitehead. With someone named Bertrand Russell, Whitehead wrote Principia
More informationSigned umbers. Sign/Magnitude otation
Signed umbers So far we have discussed unsigned number representations. In particular, we have looked at the binary number system and shorthand methods in representing binary codes. With m binary digits,
More informationLecture Notes on Contracts
Lecture Notes on Contracts 15-122: Principles of Imperative Computation Frank Pfenning Lecture 2 August 30, 2012 1 Introduction For an overview the course goals and the mechanics and schedule of the course,
More information1 ICS 161: Design and Analysis of Algorithms Lecture notes for January 23, Bucket Sorting
1 ICS 161: Design and Analysis of Algorithms Lecture notes for January 23, 1996 2 Bucket Sorting We ve seen various algorithms for sorting in O(n log n) time and a lower bound showing that O(n log n) is
More informationInternal Data Representation
Appendices This part consists of seven appendices, which provide a wealth of reference material. Appendix A primarily discusses the number systems and their internal representation. Appendix B gives information
More information9/3/2015. Data Representation II. 2.4 Signed Integer Representation. 2.4 Signed Integer Representation
Data Representation II CMSC 313 Sections 01, 02 The conversions we have so far presented have involved only unsigned numbers. To represent signed integers, computer systems allocate the high-order bit
More informationSkill 1: Multiplying Polynomials
CS103 Spring 2018 Mathematical Prerequisites Although CS103 is primarily a math class, this course does not require any higher math as a prerequisite. The most advanced level of mathematics you'll need
More informationLecture 6: Sequential Sorting
15-150 Lecture 6: Sequential Sorting Lecture by Dan Licata February 2, 2012 Today s lecture is about sorting. Along the way, we ll learn about divide and conquer algorithms, the tree method, and complete
More informationDLD VIDYA SAGAR P. potharajuvidyasagar.wordpress.com. Vignana Bharathi Institute of Technology UNIT 1 DLD P VIDYA SAGAR
UNIT I Digital Systems: Binary Numbers, Octal, Hexa Decimal and other base numbers, Number base conversions, complements, signed binary numbers, Floating point number representation, binary codes, error
More informationInteger Multiplication and Division
Integer Multiplication and Division for ENCM 369: Computer Organization Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary Winter Term, 208 Integer
More information(+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)
COMPUTER ARITHMETIC 1. Addition and Subtraction of Unsigned Numbers The direct method of subtraction taught in elementary schools uses the borrowconcept. In this method we borrow a 1 from a higher significant
More informationChapter 5: Computer Arithmetic. In this chapter you will learn about:
Slide 1/29 Learning Objectives In this chapter you will learn about: Reasons for using binary instead of decimal numbers Basic arithmetic operations using binary numbers Addition (+) Subtraction (-) Multiplication
More informationWorking with recursion. From definition to template. Readings: HtDP, sections 11, 12, 13 (Intermezzo 2).
Working with recursion Readings: HtDP, sections 11, 12, 13 (Intermezzo 2). We can extend the idea of a self-referential definition to defining the natural numbers, which leads to the use of recursion in
More informationAbout the Final. Saturday, 7-10pm in Science Center 101. Closed book, closed notes. Not on the final: graphics, file I/O, vim, unix
CS 21 Final Review About the Final Saturday, 7-10pm in Science Center 101 Closed book, closed notes Not on the final: graphics, file I/O, vim, unix Expect Questions That Ask You To: Evaluate Python expressions
More informationWorking with recursion
Working with recursion Readings: HtDP, sections 11, 12, 13 (Intermezzo 2). We can extend the idea of a self-referential definition to defining the natural numbers, which leads to the use of recursion in
More informationTable : IEEE Single Format ± a a 2 a 3 :::a 8 b b 2 b 3 :::b 23 If exponent bitstring a :::a 8 is Then numerical value represented is ( ) 2 = (
Floating Point Numbers in Java by Michael L. Overton Virtually all modern computers follow the IEEE 2 floating point standard in their representation of floating point numbers. The Java programming language
More informationAlgorithms in Systems Engineering IE172. Midterm Review. Dr. Ted Ralphs
Algorithms in Systems Engineering IE172 Midterm Review Dr. Ted Ralphs IE172 Midterm Review 1 Textbook Sections Covered on Midterm Chapters 1-5 IE172 Review: Algorithms and Programming 2 Introduction to
More information6.001 Notes: Section 4.1
6.001 Notes: Section 4.1 Slide 4.1.1 In this lecture, we are going to take a careful look at the kinds of procedures we can build. We will first go back to look very carefully at the substitution model,
More informationLearning Objectives. Binary over Decimal. In this chapter you will learn about:
Ref Page Slide 1/29 Learning Objectives In this chapter you will learn about: Reasons for using binary instead of decimal numbers Basic arithmetic operations using binary numbers Addition (+) Subtraction
More informationIntegers. Dr. Steve Goddard Giving credit where credit is due
CSCE 23J Computer Organization Integers Dr. Steve Goddard goddard@cse.unl.edu http://cse.unl.edu/~goddard/courses/csce23j Giving credit where credit is due Most of slides for this lecture are based on
More informationIntegers Sep 3, 2002
15-213 The course that gives CMU its Zip! Topics Numeric Encodings Unsigned & Two s complement Programming Implications C promotion rules Basic operations Integers Sep 3, 2002 Addition, negation, multiplication
More informationLecture 11 Unbounded Arrays
Lecture 11 Unbounded Arrays 15-122: Principles of Imperative Computation (Spring 2018) Rob Simmons, Frank Pfenning Arrays have efficient O(1) access to elements given an index, but their size is set at
More informationCSCI 1100L: Topics in Computing Lab Lab 11: Programming with Scratch
CSCI 1100L: Topics in Computing Lab Lab 11: Programming with Scratch Purpose: We will take a look at programming this week using a language called Scratch. Scratch is a programming language that was developed
More informationAccuplacer Arithmetic Study Guide
Accuplacer Arithmetic Study Guide I. Terms Numerator: which tells how many parts you have (the number on top) Denominator: which tells how many parts in the whole (the number on the bottom) Example: parts
More informationBasic Arithmetic Operations
Basic Arithmetic Operations Learning Outcome When you complete this module you will be able to: Perform basic arithmetic operations without the use of a calculator. Learning Objectives Here is what you
More informationFixed-Point Math and Other Optimizations
Fixed-Point Math and Other Optimizations Embedded Systems 8-1 Fixed Point Math Why and How Floating point is too slow and integers truncate the data Floating point subroutines: slower than native, overhead
More informationStudent Outcomes. Classwork. Discussion (10 minutes)
Student Outcomes Students know the definition of a number raised to a negative exponent. Students simplify and write equivalent expressions that contain negative exponents. Classwork Discussion (10 minutes)
More informationLecture 19: Signatures, Structures, and Type Abstraction
15-150 Lecture 19: Signatures, Structures, and Type Abstraction Lecture by Dan Licata March 27, 2012 In these lectures, we will discuss the use of the ML module system for structuring large programs. Key
More informationIntro to Algorithms. Professor Kevin Gold
Intro to Algorithms Professor Kevin Gold What is an Algorithm? An algorithm is a procedure for producing outputs from inputs. A chocolate chip cookie recipe technically qualifies. An algorithm taught in
More informationLecture Notes for Chapter 2: Getting Started
Instant download and all chapters Instructor's Manual Introduction To Algorithms 2nd Edition Thomas H. Cormen, Clara Lee, Erica Lin https://testbankdata.com/download/instructors-manual-introduction-algorithms-2ndedition-thomas-h-cormen-clara-lee-erica-lin/
More informationECE260: Fundamentals of Computer Engineering
Arithmetic for Computers James Moscola Dept. of Engineering & Computer Science York College of Pennsylvania Based on Computer Organization and Design, 5th Edition by Patterson & Hennessy Arithmetic for
More informationELEMENTARY NUMBER THEORY AND METHODS OF PROOF
CHAPTER 4 ELEMENTARY NUMBER THEORY AND METHODS OF PROOF Copyright Cengage Learning. All rights reserved. SECTION 4.3 Direct Proof and Counterexample III: Divisibility Copyright Cengage Learning. All rights
More informationNumber Systems and Computer Arithmetic
Number Systems and Computer Arithmetic Counting to four billion two fingers at a time What do all those bits mean now? bits (011011011100010...01) instruction R-format I-format... integer data number text
More informationBinary Search. Roland Backhouse February 5th, 2001
1 Binary Search Roland Backhouse February 5th, 2001 Outline 2 An implementation in Java of the card-searching algorithm is presented. Issues concerning the correctness of the implementation are raised
More informationin this web service Cambridge University Press
978-0-51-85748- - Switching and Finite Automata Theory, Third Edition Part 1 Preliminaries 978-0-51-85748- - Switching and Finite Automata Theory, Third Edition CHAPTER 1 Number systems and codes This
More informationLecture 6: Arithmetic and Threshold Circuits
IAS/PCMI Summer Session 2000 Clay Mathematics Undergraduate Program Advanced Course on Computational Complexity Lecture 6: Arithmetic and Threshold Circuits David Mix Barrington and Alexis Maciel July
More informationELEMENTARY NUMBER THEORY AND METHODS OF PROOF
CHAPTER 4 ELEMENTARY NUMBER THEORY AND METHODS OF PROOF Copyright Cengage Learning. All rights reserved. SECTION 4.3 Direct Proof and Counterexample III: Divisibility Copyright Cengage Learning. All rights
More informationIntro. Scheme Basics. scm> 5 5. scm>
Intro Let s take some time to talk about LISP. It stands for LISt Processing a way of coding using only lists! It sounds pretty radical, and it is. There are lots of cool things to know about LISP; if
More information1 GLUE INTRODUCTION 1
1 GLUE INTRODUCTION 1 1. Introduction. If TEX is being implemented on a microcomputer that does 32-bit addition and subtraction, but with multiplication and division restricted to multipliers and divisors
More informationProject 3: RPN Calculator
ECE267 @ UIC, Spring 2012, Wenjing Rao Project 3: RPN Calculator What to do: Ask the user to input a string of expression in RPN form (+ - * / ), use a stack to evaluate the result and display the result
More informationLecture 1: Overview
15-150 Lecture 1: Overview Lecture by Stefan Muller May 21, 2018 Welcome to 15-150! Today s lecture was an overview that showed the highlights of everything you re learning this semester, which also meant
More informationRepetition Through Recursion
Fundamentals of Computer Science I (CS151.02 2007S) Repetition Through Recursion Summary: In many algorithms, you want to do things again and again and again. For example, you might want to do something
More informationCOE 202: Digital Logic Design Number Systems Part 2. Dr. Ahmad Almulhem ahmadsm AT kfupm Phone: Office:
COE 0: Digital Logic Design Number Systems Part Dr. Ahmad Almulhem Email: ahmadsm AT kfupm Phone: 860-7554 Office: -34 Objectives Arithmetic operations: Binary number system Other number systems Base Conversion
More informationBits, Words, and Integers
Computer Science 52 Bits, Words, and Integers Spring Semester, 2017 In this document, we look at how bits are organized into meaningful data. In particular, we will see the details of how integers are
More information6.1 Evaluate Roots and Rational Exponents
VOCABULARY:. Evaluate Roots and Rational Exponents Radical: We know radicals as square roots. But really, radicals can be used to express any root: 0 8, 8, Index: The index tells us exactly what type of
More informationUNIVERSITY OF CALIFORNIA, SANTA CRUZ BOARD OF STUDIES IN COMPUTER ENGINEERING
UNIVERSITY OF CALIFORNIA, SANTA CRUZ BOARD OF STUDIES IN COMPUTER ENGINEERING CMPE13/L: INTRODUCTION TO PROGRAMMING IN C SPRING 2012 Lab 3 Matrix Math Introduction Reading In this lab you will write a
More informationMath 171 Proficiency Packet on Integers
Math 171 Proficiency Packet on Integers Section 1: Integers For many of man's purposes the set of whole numbers W = { 0, 1, 2, } is inadequate. It became necessary to invent negative numbers and extend
More informationThere are four numeric types: 1. Integers, represented as a 32 bit (or longer) quantity. Digits sequences (possibly) signed are integer literals:
Numeric Types There are four numeric types: 1. Integers, represented as a 32 bit (or longer) quantity. Digits sequences (possibly) signed are integer literals: 1-123 +456 2. Long integers, of unlimited
More informationChapter Binary Representation of Numbers
Chapter 4 Binary Representation of Numbers After reading this chapter, you should be able to: convert a base- real number to its binary representation,. convert a binary number to an equivalent base- number.
More informationCHAPTER 1: INTEGERS. Image from CHAPTER 1 CONTENTS
CHAPTER 1: INTEGERS Image from www.misterteacher.com CHAPTER 1 CONTENTS 1.1 Introduction to Integers 1. Absolute Value 1. Addition of Integers 1.4 Subtraction of Integers 1.5 Multiplication and Division
More informationLanguage Basics. /* The NUMBER GAME - User tries to guess a number between 1 and 10 */ /* Generate a random number between 1 and 10 */
Overview Language Basics This chapter describes the basic elements of Rexx. It discusses the simple components that make up the language. These include script structure, elements of the language, operators,
More information15-122: Principles of Imperative Computation, Fall 2015
15-122 Programming 5 Page 1 of 10 15-122: Principles of Imperative Computation, Fall 2015 Homework 5 Programming: Clac Due: Thursday, October 15, 2015 by 22:00 In this assignment, you will implement a
More informationModule 2: Computer Arithmetic
Module 2: Computer Arithmetic 1 B O O K : C O M P U T E R O R G A N I Z A T I O N A N D D E S I G N, 3 E D, D A V I D L. P A T T E R S O N A N D J O H N L. H A N N E S S Y, M O R G A N K A U F M A N N
More informationModule 2 Congruence Arithmetic pages 39 54
Module 2 Congruence Arithmetic pages 9 5 Here are some excellent websites that can help you on this topic: http://mathcentral.uregina.ca/qq/database/qq.09.98/kupper1.html http://nrich.maths.org/public.viewer.php?obj_id=50
More informationWhat Every Programmer Should Know About Floating-Point Arithmetic
What Every Programmer Should Know About Floating-Point Arithmetic Last updated: October 15, 2015 Contents 1 Why don t my numbers add up? 3 2 Basic Answers 3 2.1 Why don t my numbers, like 0.1 + 0.2 add
More informationCMSC 330: Organization of Programming Languages. Operational Semantics
CMSC 330: Organization of Programming Languages Operational Semantics Notes about Project 4, Parts 1 & 2 Still due today (7/2) Will not be graded until 7/11 (along with Part 3) You are strongly encouraged
More informationcarry in carry 1101 carry carry
Chapter Binary arithmetic Arithmetic is the process of applying a mathematical operator (such as negation or addition) to one or more operands (the values being operated upon). Binary arithmetic works
More informationCS106X Handout 03 Autumn 2012 September 24 th, 2012 Getting Started
CS106X Handout 03 Autumn 2012 September 24 th, 2012 Getting Started Handout written by Julie Zelenski, Mehran Sahami, Robert Plummer, and Jerry Cain. After today s lecture, you should run home and read
More informationModules and Abstract Types
Modules and Abstract Types COMP 105 Assignment Due Wednesday, November 14, 2018 at 11:59PM Contents Overview 1 Setup 2 Dire warnings 2 Reading 3 Reading comprehension 3 Programming, part one: Arbitrary-precision
More informationCS321 Introduction To Numerical Methods
CS3 Introduction To Numerical Methods Fuhua (Frank) Cheng Department of Computer Science University of Kentucky Lexington KY 456-46 - - Table of Contents Errors and Number Representations 3 Error Types
More informationSystems 1. Integers. Unsigned & Twoʼs complement. Addition, negation, multiplication
Systems 1 Integers Topics Numeric Encodings Unsigned & Twoʼs complement Programming Implications C promotion rules Basic operations Addition, negation, multiplication Programming Implications Consequences
More informationCS115 - Module 9 - filter, map, and friends
Fall 2017 Reminder: if you have not already, ensure you: Read How to Design Programs, Intermezzo 3 (Section 18); Sections 19-23. Abstraction abstraction, n. 3a.... The process of isolating properties or
More informationCS 115 Data Types and Arithmetic; Testing. Taken from notes by Dr. Neil Moore
CS 115 Data Types and Arithmetic; Testing Taken from notes by Dr. Neil Moore Statements A statement is the smallest unit of code that can be executed on its own. So far we ve seen simple statements: Assignment:
More informationNumber Systems and Conversions UNIT 1 NUMBER SYSTEMS & CONVERSIONS. Number Systems (2/2) Number Systems (1/2) Iris Hui-Ru Jiang Spring 2010
Contents Number systems and conversion Binary arithmetic Representation of negative numbers Addition of two s complement numbers Addition of one s complement numbers Binary s Readings Unit.~. UNIT NUMBER
More informationChapter 3 Data Representation
Chapter 3 Data Representation The focus of this chapter is the representation of data in a digital computer. We begin with a review of several number systems (decimal, binary, octal, and hexadecimal) and
More informationCSCI-GA Scripting Languages
CSCI-GA.3033.003 Scripting Languages 12/02/2013 OCaml 1 Acknowledgement The material on these slides is based on notes provided by Dexter Kozen. 2 About OCaml A functional programming language All computation
More informationEC121 Mathematical Techniques A Revision Notes
EC Mathematical Techniques A Revision Notes EC Mathematical Techniques A Revision Notes Mathematical Techniques A begins with two weeks of intensive revision of basic arithmetic and algebra, to the level
More informationStatistics Case Study 2000 M. J. Clancy and M. C. Linn
Statistics Case Study 2000 M. J. Clancy and M. C. Linn Problem Write and test functions to compute the following statistics for a nonempty list of numeric values: The mean, or average value, is computed
More information(Refer Slide Time 3:31)
Digital Circuits and Systems Prof. S. Srinivasan Department of Electrical Engineering Indian Institute of Technology Madras Lecture - 5 Logic Simplification In the last lecture we talked about logic functions
More informationLecture 18 Restoring Invariants
Lecture 18 Restoring Invariants 15-122: Principles of Imperative Computation (Spring 2018) Frank Pfenning In this lecture we will implement heaps and operations on them. The theme of this lecture is reasoning
More informationChapter 5: Computer Arithmetic
Slide 1/29 Learning Objectives Computer Fundamentals: Pradeep K. Sinha & Priti Sinha In this chapter you will learn about: Reasons for using binary instead of decimal numbers Basic arithmetic operations
More informationVARIABLES. Aim Understanding how computer programs store values, and how they are accessed and used in computer programs.
Lesson 2 VARIABLES Aim Understanding how computer programs store values, and how they are accessed and used in computer programs. WHAT ARE VARIABLES? When you input data (i.e. information) into a computer
More informationChapter 3: part 3 Binary Subtraction
Chapter 3: part 3 Binary Subtraction Iterative combinational circuits Binary adders Half and full adders Ripple carry and carry lookahead adders Binary subtraction Binary adder-subtractors Signed binary
More informationChapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines.
Chapter 1 Summary Comments are indicated by a hash sign # (also known as the pound or number sign). Text to the right of the hash sign is ignored. (But, hash loses its special meaning if it is part of
More informationNumber Systems CHAPTER Positional Number Systems
CHAPTER 2 Number Systems Inside computers, information is encoded as patterns of bits because it is easy to construct electronic circuits that exhibit the two alternative states, 0 and 1. The meaning of
More informationCSE341: Programming Languages Lecture 9 Function-Closure Idioms. Dan Grossman Winter 2013
CSE341: Programming Languages Lecture 9 Function-Closure Idioms Dan Grossman Winter 2013 More idioms We know the rule for lexical scope and function closures Now what is it good for A partial but wide-ranging
More informationSECTION 5.1. Sequences
SECTION 5.1 Sequences Sequences Problem: count number of ancestors one has 2 parents, 4 grandparents, 8 greatgrandparents,, written in a row as 2, 4, 8, 16, 32, 64, 128, To look for pattern of the numbers,
More informationNumber Systems. Both numbers are positive
Number Systems Range of Numbers and Overflow When arithmetic operation such as Addition, Subtraction, Multiplication and Division are performed on numbers the results generated may exceed the range of
More informationGrade 5. (Paper MCA, items)
Grade 5 Strand 1 Number & Operation (Online MCA, 15 21 items) (Paper MCA, 18-22 items) Standard 5.1.1: Divide multi-digit numbers; solve real-world and mathematical problems using arithmetic. (Online MCA,
More informationKinds Of Data CHAPTER 3 DATA REPRESENTATION. Numbers Are Different! Positional Number Systems. Text. Numbers. Other
Kinds Of Data CHAPTER 3 DATA REPRESENTATION Numbers Integers Unsigned Signed Reals Fixed-Point Floating-Point Binary-Coded Decimal Text ASCII Characters Strings Other Graphics Images Video Audio Numbers
More information\n is used in a string to indicate the newline character. An expression produces data. The simplest expression
Chapter 1 Summary Comments are indicated by a hash sign # (also known as the pound or number sign). Text to the right of the hash sign is ignored. (But, hash loses its special meaning if it is part of
More information1010 2?= ?= CS 64 Lecture 2 Data Representation. Decimal Numbers: Base 10. Reading: FLD Digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
CS 64 Lecture 2 Data Representation Reading: FLD 1.2-1.4 Decimal Numbers: Base 10 Digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 Example: 3271 = (3x10 3 ) + (2x10 2 ) + (7x10 1 ) + (1x10 0 ) 1010 10?= 1010 2?= 1
More informationLecture 3: Recursion; Structural Induction
15-150 Lecture 3: Recursion; Structural Induction Lecture by Dan Licata January 24, 2012 Today, we are going to talk about one of the most important ideas in functional programming, structural recursion
More information