Faculty of Computer Science © 2006 CMPUT 229 Floating Point Representation Operating with Real Numbers
© 2006 Department of Computing Science CMPUT 229 Reading Material This set of slides is based on the texts by Patt and Patel and by Patterson and Hennessy. The topics covered in these slides are presented in Section 4.9 of Clements’ textbook.
© 2006 Department of Computing Science CMPUT 229 Representing Large and Small Numbers How would you represent a number such as in binary? The range (10 23 ) of this number is greater than the range of the 32-bits representation that we have used for integers (2 31 2.14 ). However the precision (6023) of this number is quite small, and can be expressed in a small number of bits. The solution is to use a floating point representation. A floating point representation allocates some bits for the range of the value, some bits for precision, and one bit for the sign. Patt/Patel, pp. 32
© 2006 Department of Computing Science CMPUT 229 Floating Point Representation Most standard floating point representation use: 1 bit for the sign (positive or negative) 8 bits for the range (exponent field) 23 bits for the precision (fraction field) Sexponentfraction 2381 Patt/Patel, pp. 33
© 2006 Department of Computing Science CMPUT 229 Floating Point Representation (example) Sexponentfraction 2381 Thus the exponent is given by: Patt and Patel, pp. 34
© 2006 Department of Computing Science CMPUT 229 Floating Point Representation (example) Sexponentfraction What is the decimal value of the following floating point number? exponent exponent = =(128-8)+3=120+3=123 Patt and Patel, pp. 34
© 2006 Department of Computing Science CMPUT 229 Floating Point Representation (example) Sexponentfraction What is the decimal value of the following floating point number? exponent exponent = =131 Patt and Patel, pp. 35
© 2006 Department of Computing Science CMPUT 229 Floating Point Representation (example) Sexponentfraction What is the decimal value of the following floating point number? exponent exponent =128+2=130 Patt and Patel, pp. 35
© 2006 Department of Computing Science CMPUT 229 Floating Point Sexponentfraction 2381 What is the largest number that can be represented using a 32-bit floating point number using the IEEE 754 format above? exponent exponent =254 Patt and Patel, pp. 35
© 2006 Department of Computing Science CMPUT 229 Floating Point Sexponentfraction 2381 What is the largest number that can be represented in 32 bits floating point using the IEEE 754 format above? exponent actual exponent = = 127 Patt and Patel, pp. 35
© 2006 Department of Computing Science CMPUT 229 Floating Point Sexponentfraction 2381 What is the smallest number (closest to zero) that can be represented in 32 bits floating point using the IEEE 754 format above? exponent actual exponent =0-126 = -126 Patt and Patel, pp. 35
© 2006 Department of Computing Science CMPUT 229 Special Floating Point Representations In the 8-bit field of the exponent we can represent numbers from 0 to 255. We studied how to read numbers with exponents from 0 to 254. What is the value represented when the exponent is 255 (i.e )? An exponent equal 255 = in a floating point representation indicates a special value. When the exponent is equal 255 = and the fraction is 0, the value represented is infinity. When the exponent is equal 255 = and the fraction is non-zero, the value represented is Not a Number (NaN). Hen/Patt, pp. 301
© 2006 Department of Computing Science CMPUT 229 Double Precision 32-bit floating point representation is usually called single precision representation. A double precision floating point representation requires 64 bits. In double precision the following number of bits are used: 1 sign bit 11 bits for exponent 52 bits for fraction (also called significand)
© 2006 Department of Computing Science CMPUT 229 Floating Point Addition (Decimal) How do we perform the following addition? Step 1: Align decimal point of the number with smaller exponent (notice lost of precision) 10 1 Step 2: Add significands: 10 1 = 10 1 Step 3: Renormalize the result: 10 1 = 10 2 Step 3: Round-off the result to the representation available: 10 2 = 10 2 Hen/Patt, pp. 281
© 2006 Department of Computing Science CMPUT 229 Floating Point Addition (Example) Convert the numbers and to floating point binary representation, and then perform the binary floating-point addition of these numbers. Which number should have its significand adjusted? Hen/Patt, pp. 283
© 2006 Department of Computing Science CMPUT 229 Floating Point Multiplication (Decimal) Assume that we only can store four digits of the significand and two digits of the exponent in a decimal floating point representation. How would you multiply by in this representation? Step 1: Add the exponents: new exponent = = 5 Step 2: Multiply the significands: Step 3: Normalize the product: 10 5 = 10 6 Step 4: Round-off the product: 10 6 = 10 6 Hen/Patt, pp. 286