unused unused unused unused unused unused

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

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

Binary Addition & Subtraction. Unsigned and Sign & Magnitude numbers


COMP2611: Computer Organization. Data Representation

Chapter 4. Operations on Data

Number Systems. Both numbers are positive

Inf2C - Computer Systems Lecture 2 Data Representation

LAB WORK NO. 2 THE INTERNAL DATA REPRESENTATION

Introduction to Computers and Programming. Numeric Values

Signed umbers. Sign/Magnitude otation

Operations On Data CHAPTER 4. (Solutions to Odd-Numbered Problems) Review Questions

Chapter 3: Arithmetic for Computers

Number Systems and Binary Arithmetic. Quantitative Analysis II Professor Bob Orr

CHAPTER 5: Representing Numerical Data

Floating-point Arithmetic. where you sum up the integer to the left of the decimal point and the fraction to the right.

COMP Overview of Tutorial #2

ECE232: Hardware Organization and Design

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

Floating Point Arithmetic

Module 2: Computer Arithmetic

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

CS 101: Computer Programming and Utilization

Number Systems. Decimal numbers. Binary numbers. Chapter 1 <1> 8's column. 1000's column. 2's column. 4's column

Chapter 2. Data Representation in Computer Systems

Numerical computing. How computers store real numbers and the problems that result

Introduction to Scientific Computing Lecture 1

Real Numbers finite subset real numbers floating point numbers Scientific Notation fixed point numbers

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

CS 265. Computer Architecture. Wei Lu, Ph.D., P.Eng.

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

±M R ±E, S M CHARACTERISTIC MANTISSA 1 k j

Floating Point Numbers. Lecture 9 CAP

Divide: Paper & Pencil

IEEE Standard for Floating-Point Arithmetic: 754

CPE 323 REVIEW DATA TYPES AND NUMBER REPRESENTATIONS IN MODERN COMPUTERS

The type of all data used in a C (or C++) program must be specified

CPE 323 REVIEW DATA TYPES AND NUMBER REPRESENTATIONS IN MODERN COMPUTERS

The type of all data used in a C++ program must be specified

Chapter Three. Arithmetic

CHW 261: Logic Design

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

Number Systems Standard positional representation of numbers: An unsigned number with whole and fraction portions is represented as:

Signed Binary Numbers

Real Numbers finite subset real numbers floating point numbers Scientific Notation fixed point numbers

10.1. Unit 10. Signed Representation Systems Binary Arithmetic

Up next. Midterm. Today s lecture. To follow

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

Number Systems. Binary Numbers. Appendix. Decimal notation represents numbers as powers of 10, for example

Numbers and Computers. Debdeep Mukhopadhyay Assistant Professor Dept of Computer Sc and Engg IIT Madras

Number System. Introduction. Decimal Numbers

Number Systems (2.1.1)

MACHINE LEVEL REPRESENTATION OF DATA

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

MC1601 Computer Organization

CS101 Lecture 04: Binary Arithmetic

Chapter 2: Number Systems

Computer (Literacy) Skills. Number representations and memory. Lubomír Bulej KDSS MFF UK

3.1 DATA REPRESENTATION (PART C)

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

UNIT 7A Data Representation: Numbers and Text. Digital Data

Organisasi Sistem Komputer

IT 1204 Section 2.0. Data Representation and Arithmetic. 2009, University of Colombo School of Computing 1

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

9/3/2015. Data Representation II. 2.4 Signed Integer Representation. 2.4 Signed Integer Representation

ECE 2020B Fundamentals of Digital Design Spring problems, 6 pages Exam Two 26 February 2014

Computer Organisation CS303

Floating-point representations

Floating-point representations

3 Data Storage 3.1. Foundations of Computer Science Cengage Learning

COMPUTER ORGANIZATION AND ARCHITECTURE

Floating-Point Arithmetic

CHAPTER 2 Data Representation in Computer Systems

FLOATING POINT NUMBERS

CHAPTER 2 Data Representation in Computer Systems

ECE 2030D Computer Engineering Spring problems, 5 pages Exam Two 8 March 2012

fractional quantities are typically represented in computers using floating point format this approach is very much similar to scientific notation

Representing and Manipulating Floating Points. Jo, Heeseung

15213 Recitation 2: Floating Point

Bits, Words, and Integers

Chapter 5 : Computer Arithmetic

Integers. N = sum (b i * 2 i ) where b i = 0 or 1. This is called unsigned binary representation. i = 31. i = 0

CO212 Lecture 10: Arithmetic & Logical Unit

Digital Arithmetic. Digital Arithmetic: Operations and Circuits Dr. Farahmand

Set Theory in Computer Science. Binary Numbers. Base 10 Number. What is a Number? = Binary Number Example

Floating Point Numbers

ecture 25 Floating Point Friedland and Weaver Computer Science 61C Spring 2017 March 17th, 2017

Floating Point. CSE 351 Autumn Instructor: Justin Hsia

IBM 370 Basic Data Types

C NUMERIC FORMATS. Overview. IEEE Single-Precision Floating-point Data Format. Figure C-0. Table C-0. Listing C-0.

8/30/2016. In Binary, We Have A Binary Point. ECE 120: Introduction to Computing. Fixed-Point Representations Support Fractions

ECE 2020B Fundamentals of Digital Design Spring problems, 6 pages Exam Two Solutions 26 February 2014

CS 61C: Great Ideas in Computer Architecture Performance and Floating Point Arithmetic

Floating-Point Arithmetic

CHAPTER 1 Numerical Representation

CMPSCI 145 MIDTERM #2 SPRING 2017 April 7, 2017 Professor William T. Verts NAME PROBLEM SCORE POINTS GRAND TOTAL 100

Digital Fundamentals

Computer Arithmetic Floating Point

Foundations of Computer Systems

Floating-Point Arithmetic

Transcription:

BCD numbers. In some applications, such as in the financial industry, the errors that can creep in due to converting numbers back and forth between decimal and binary is unacceptable. For these applications it is common to represent decimal numbers in format called Binary Coded Decimal, or BCD. In this format, each decimal digit is converted to its binary equivalent. Thus, each decimal digit uses 4 bits, and only the binary values 0000 thru 1001 are used, as in the following table. 0000 0 1000 8 0001 1 1001 9 0010 2 1010 unused 0011 3 1011 unused 0100 4 1100 unused 0101 5 1101 unused 0110 6 1110 unused 0111 7 1111 unused Since each decimal digit uses 4-bits, a byte can only hold two digits. A 5 digit decimal number uses 2 ½ bytes. It is common to allocate space for an odd number of digits; this leaves ½ of a byte left over for the sign so that BCD numbers often are expressed in sign & magnitude format. (It is also possible to use 10's complement or 9's complement formats.) Examples: 21. 94 10 = 1001 0100 BCD 22. 28 10 = 0010 1000 BCD 23. -92 10 = 1111 0000 1001 0010 BCD The above examples are more properly called Packed BCD numbers because two digits are packed into a single byte. This format is usually used for storing BCD numbers, but for actually performing arithmetic an unpacked BCD format is generally used. This format devotes an entire byte to each decimal digit, with the high order four bits equal to 0's, except for the first byte, which might indicate the sign of the number in sign & NTC 1/23/05 35

magnitude notation Examples: 24. 94 10 = 00001001 00000100 BCD 25. 28 10 = 00000010 00001000 BCD 26. -92 10 = 11111001 00000010 BCD Practice Problems - BCD format 1. Express the following Decimal numbers in packed BCD format: a. 0 b. 392 c. 59138 d. -3 2. Express the following Decimal numbers in unpacked BCD format: a. 1 b. 201 c. 99 d. -999 3. What decimal number, if any, is represented by each of the following packed BCD numbers? a. 0000 0111 0100 0000 c. 0000 0000 0000 1001 b. 1111 0111 0110 0111 d. 0000 1001 1100 0001 4. What is the maximum unsigned decimal number which can be represented in a. 4 bytes unsigned packed BCD notation? b. 12 byte unsigned unpacked BCD notation? Floating point numbers Floating point numbers are used wherever very large or very small numbers need to be manipulated, such as in cosmological physics, or quantum physics, or other scientific disciplines. This notation is also called scientific notation, or engineering notation. In general usage, a floating point number is represented by a fraction multiplied by the base raised to a power. For instance, given the fixed point number NTC 1/23/05 36

3456.231, this could be represented in scientific (floating point) notation as 3.456231 x 10 3 [N12] It could also have been represented by 34.56231 x 10 2 or 3456231.0 x 10-3. It is conventional, however, to normalize a number so that there is just one non-zero digit to the left of the decimal point. Hence, [N12] is the correct form of this number. Note that both the number itself as well as the exponent may be negative. E.g. -3.4 x 10-53 Nomenclature: The normalized digits are referred to as the mantissa (I have taken some liberties here, as usually the mantissa only refers to the digits to the right of the decimal point). The exponent is called the exponent. The advantage of this representation is the range of numbers that can be represented. Remember that an n-digit decimal integer is restricted to the range 0 through 10 n -1. (e.g. a 3 digit number goes from 0 through 999). If the n digits are all fractional digits, the numbers representable are 0 through 10 n -1/10 n = 1-10 -n. (Note that only numbers with n significant digits can be represented, so the entire continuum of fractions in this range can not be represented. A three digit fraction can represent.000 through.999, but not, say,.9985). In floating point notation, in addition to the number of significant digits of the mantissa, the decimal point can be placed anywhere in the allowable range of the exponent. In a computer system, the exponent as well as the mantissa is held in a storage location or register with limited extent. Let s assume that (still considering decimal numbers) the mantissa is given by 4 digits and the exponent is given 2 digits. Then the numbers that can be represented in this format (ignoring the signs for the time being are) 0.000 through 9.999 for the mantissa, and 00 through 99 for the exponent. The smallest number (ignoring signs) is 0.000 x 10 0 and the largest is 9.999 x 10 99. If we sketch out the numbers on the real line, we see that there are gaps where nonrepresentable numbers exist. For instance, none of the numbers between 1.000 x 10 2 and 1.001 x 10 2 can be represented. Even more interesting is the fact that none of the numbers immediately following 0.000 x 10 0 (zero) are representable, such as 0.0001, so the first gap is right at the origin of the real line. For each exponent, the gaps are uniformly spaced and of uniform size, but as the exponents increase the gaps get bigger. In general, every individual number is separated by from every other one by a gap. Additionally, every exponent range is separated from each other by a gap. As numbers NTC 1/23/05 37

move farther from zero, these gaps get larger. Example: consider 1.000 x 10 0 and 1.001 x 10 0 ; they differ by.001. But 1.000 x 10 2 and 1.001 x 10 2 differ by.001 x 10 2 =.1 We should expect something like this since it should be obvious that in a finite number of bits only a finite number of numbers can be represented, despite the fact that in any range of real numbers there are an infinite number of them. We will now discuss the actual representation of floating point numbers in a computer system - binary floating point numbers and how they are stored in hardware registers. Consider the number 10110.0101 2 = 1.01100101 x 2 4. To implement this in computer hardware we need to specify the following: a. The number of bits allocated to the mantissa b. The number of bits allocated to the exponent c. How the sign of the number will be represented d. How the sign of the exponent will be represented. In general, all of this information is kept in a single register, divided up into sections, called fields: Sign Exponent Mantissa The sign shown is a single bit and refers to the sign of the entire number (or, equivalently, of the mantissa.) The exponent is in biased notation. The reason for this is as follows: consider two decimal floating point numbers which you wish to add together, say 3.2 x 10 3 and 4.0 x 10 5. Before they can be added, they must be made to have the same exponent. (i.e. we need to align the decimal points). We usually choose the number with the smaller exponent and increase it to match the other exponent, moving the decimal point to the left a corresponding number of positions. (Moving the decimal point left is accomplished in practice by shifting the fraction to the right; in this process some digits are lost as they are shifted off the right end of the register - the least significant ones.) Therefore, we need to easily be able to increase one or the other of the exponents by a (usually) small amount, regardless of whether it is positive or negative, and even if the sign changes during the process, as it might. See Table TN3 to see that, of the different number representations, only the biased form provides this capability. The mantissa, or fraction, is the normalized form of the given digits, or as many of them NTC 1/23/05 38

as will fit in the allocated space. Remember that a normalized number, in our discussion, has a single non-zero digit to the left of the binary point. Since there is only one possible non-zero value, one, some savings are realized by not bothering to actually waste a bit on that digit - it is always assumed, or implied, that a 1 precedes the rest of the fraction. This allows an extra bit of significance to be maintained in the mantissa field of the register. A typical floating point number in this format would look like this: 0 011001 01001111000000000 (or 329E00 in hexadecimal notation) where the sign is +, the exponent is 011001 (we haven t specified the bias) and the mantissa is 1.01001111000000000 (note that we have added the implied one to the left of the binary point.) Notice that this format does not allow you to represent a value of zero! If we are given 0 000000 00000000000000000 we should interpret this as an exponent of 000000 but a mantissa of 1.00000000000000000. It is usual, however, to treat zero as a special case: When all the bits in the representation are 0's, as in this last example, then the number is assumed to be zero and no implied 1 is included when interpreting the number. Although computer designers can arbitrarily choose the size of the register to be used as well as the size of the fields, most modern systems follow the standards established by the IEEE (Institute of Electrical and Electronic Engineers). This standard provides two formats, a single precision format and a double precision format. They are summarized in table TN4. When all of the bits in the format are 0's, the number is assumed to be zero. Feature Single Precision Format Double Precision Format Register Length 32 bits 64 bits Mantissa 23 + 1 implied 52 + 1 implied Exponent, Bias 8 bits, 127 11 bits, 1023 Table. TN4. IEEE Floating Point Standard NTC 1/23/05 39

Conversion of Decimal numbers to Binary Floating Point The following steps are required in order to convert a decimal number to its binary floating point representation. a. Convert the decimal number to a binary number (This includes either expanding 10 x into decimal form, if x is small, or converting it to 2 y ) b. Put the binary number into floating-point form c. Normalize the binary number d. Convert the exponent to binary, and add the bias e. Specify the sign as a binary digit In the following examples, we ll assume a floating point format with a 16 bit register to hold the FP number, having 1 sign bit 4 exponent bits, with a bias of 7 11 mantissa bits, plus 1 implied bit Note that the exponent range in this format is -7 through +8. This means that the smallest (absolute value) number representable is 1.00000000000 x 2-7 = 1/128 =.0078125 = 7.8125 x 10-3, and the largest is 1.11111111111 x 2 8 = (1 + (2 12-1)/2 12 ) x 2 8 = (1 + 4095/4096) x 256 = 511.9375 = 5.119375 x 10 2 Example 27. Convert 54.23 to the above binary format. a. 54 = 110110.23 =.0011101011... (11101011 repeats) 54.23 = 110110.0011101011... b. and c. 1.101100011101011.. x 2 5 (note that 15 fraction bits are shown; only 11 of them will be retained in the specified format.) d. 5 = 0101; add the bias of 7: 0101 +111 = 1100. e. Sign = 0 NTC 1/23/05 40

In floating point hardware format, 54.23 = 0 1100 10110001110 (or 658E 16 ) Notice that the 1 to the left of the binary point is not included. Example 28. Convert -3.75 x 10 2 to binary FP format a. The easy way to do this is to write the number in integer format (375) and convert it to binary. In practice, however, the exponents are likely to be quite large and it would not be feasible to take this approach. Let s develop a general rule for converting 10 x to 2 y. We want 10 x = 2 y ; that is, we want to solve this equation for y in terms of x. Take the log to the base 2 of both sides log 2 10 x = log 2 2 y xlog 2 10 = ylog 2 2 = y Thus, any exponent of 10 can be converted to an exponent of 2 by multiplying the decimal exponent by log 2 10, which is roughly equal to 3.322... So, for this example, we could say 3.75 x 10 2 = 3.75 x 2 2 x 3.322 = 3.75 x 2 6.644. Since we don t allow for fractional exponents, we will have to make the following adjustment: and 2 6.644 = 2 6 x 2.644 = 2 6 x 1.563 (using a calculator) 3.75 x 10 2 = 3.75 x 1.563 x 2 6 = 5.86 x 2 6. Converting 5.86 to binary gives us 101.11011100001 x 2 6 [Note that when converting a fraction from decimal to binary we can stop multiplying by two when we have generated enough digits (counting both the integer and fraction portions of the number) to fill the mantissa field of the FLP format.] b. and c. 1.01110111000 x 2 8 NTC 1/23/05 41

d. 4. Exponent 8 biased by 7 is 15 = 1111. e. Sign (-) is 1. The final result is 1 1111 01110111000. (FBB8 16 ) Note that by converting 10 2 to 2 y, we introduced several rounding and truncation errors. Compare the result above with what we get by simply converting 375: a. 375 = 101110111 b. and c. 1.0111011 x 2 8 d. Exponent is still 8 --> 1111 in biased form e. Sign = 1 The final result is 1 1111 01110110000, (FBB0 16 ) which differs from our previous result only in the eighth fractional position, so we are off by 2-8 or 1/256 =.0039. Convert binary floating point numbers to Decimal numbers The procedure is a. Convert the exponent to decimal and subtract the bias b. Evaluate 2 x if possible (x is small) or convert to 10 y c. Convert the mantissa to decimal and add 1 (to restore the implied digit) d. Combine the sign and the results of steps 1 through 3 into the decimal form of the number Example 29. Convert 1 0001 01010000000 (8A80 16 ) to a decimal number. a. The exponent is 0001 2 = 1 10 ; subtracting the bias (7) gives -6. b. 2-6 = 1/2 6 = 1/64 =.015625. Alternatively, 2-6 = 10 y ; taking the log 10 of both sides gives log 10 2-6 = log 10 10 y = y -6log 10 2 = -6 x.301 = -1.806 = y [That is, just as an exponent of 10 can be converted to an exponent of 2 by multiplying by log 2 10 = 3.322.., we can convert an exponent of 2 back to an exponent of 10 by multiplying by log 10 2 =.301.] NTC 1/23/05 42

10-1.806 = 10-1 x 10 -.806 =.1 x.156315 =.0156315. Compare this with.015625; we will continue with the latter, since it contains no rounding or truncation errors. c..0101 = 5/16 =.3125; restoring the implied one = 1.3125 d. 1.3125 x.015625 =.02051 = 2.051 x 10-2. (Either form is acceptable) The concepts involved in the last two examples are important, whereas the actual numerical manipulations are merely tedious. The following practice problems represent the kinds of questions one might actually be asked to answer on an exam or in real life. Practice Problems - Binary Floating Point Numbers 1. Using the floating point register format given above for the examples, show the register contents for the following binary floating point numbers: a. 101.1101 x 2 3 b. -.001111 x 2 8 c. 11.0011 x 2-6 2. What is the minimum number of exponent bits required to accommodate the following binary numbers (review the section on biased binary numbers, if necessary.) a. 1.0 x 2 324 b. 1110010.00111 x 2-133 3. What is the binary floating point number (in the form b.bbb... x 2 e ) represented by each of the following FLP register contents a. 0 01110 010010010(five bit exponent, bias = 15) b. 1 11111111 00000000 (eight bit exponent, bias = 127) c. 3B1CFF 16 (six bit exponent, bias = 31) 4. Express the following binary floating point number in IEEE floating point format 11011110.0010010 x 2-75 5. The following hex number represents the contents of an IEEE floating point register. Express this in normalized binary floating point form ( b.bbb... x 2 e ) 9F380000 NTC 1/23/05 43