Computer Science 210 Computer Organization Floating Point Representation
Real Numbers Format 1:. Examples: 0.25, … Format 2 (normalized form):. × Example: 2.5 × In mathematics, infinite range and infinite precision (“uncountably infinite”)
math.pi >>> import math >>> math.pi >>> print(math.pi) >>> print("%.50f" % math.pi) Looks like about 48 places of precision (in base 10 )
IEEE Standard Single precision: 32 bits Double precision: 64 bits 2.5 × Reserve some bits for the significand (the digits to the left of ×) and some for the exponent (the stuff to the right of ×) Double precision uses 53 bits for the significand, 11 bits for the exponent, and one sign bit Approximate double precision range is to
IEEE Single Precision Format 32 bits Roughly (-1) S x F x 2 E F is related to the significand E is related to the exponent Rough range Small fractions 2 x Large fractions 2 x S Exponent Significand
Fractions in Binary In general, 2 -N = 1/2 N = 1 × 2 -1 = 1 × ½ = = 1 × 2 -2 = 1 × ¼ = = 1 × ½ + 1 × ¼ =
Decimal to Binary Conversion (Whole Numbers) While N > 0 do Set N to N/2 (whole part) Record the remainder (1 or 0) Set A to remainders in reverse order
Decimal to Binary - Example Example: Convert to binary N Rem N Rem =
Decimal to Binary - Fractions While N > 0 (or enough bits) do Set N to N*2 (whole part) Record the whole number part (1 or 0) Set N to fraction part Set bits to sequence of whole number parts (in order obtained)
Decimal fraction to binary - Example Example: Convert to binary N Whole Part =
Decimal fraction to binary - Example Example: Convert to binary N Whole Part = … 2
Round-Off Errors >>> >>> print("%.48f" % 0.1) >>> print("%.48f" % 0.25) >>> print("%.48f" % 0.3) Caused by conversion of decimal fractions to binary
Scientific Notation - Decimal Number Normalized Scientific x ,326,043, x 10 9
Floating Point IEEE Single Precision Standard (32 bits) Roughly (-1) S x F x 2 E –F is related to significand –E is related to exponent Rough range –Small fractions 2 x –Large fractions 2 x S Exponent Significand
Floating Point – Exponent Field This comes before significand for sorting purposes With 8 bit exponent range would be –128 to 127 Note: -1 would be and with simple sorting would appear largest. For this reason, we take the exponent, add 127 and represent this as unsigned. This is called bias 127. Then exponent field (255) would represent = 128. Also (0) would represent = Range of exponents is -127 to 128
Floating Point – Significand Normalized form: … x 2 E Hidden bit trick: Since the bit to left of binary point is always 1, why store it? We don’t. Number = (-1) S x (1+Significand) x 2 E-127
Floating Point Example: Convert to IEEE Step 1. Convert to binary: Step 2. Normalize: x 2 8 Step 3. Compute biased exponent in binary: = 135 Step 4. Write the floating point representation: or 439C7000 in hexadecimal
Floating Point Example: Convert IEEE … to decimal Step 1. Sign bit is 1; so number is negative Step 2. Exponent field is or 129; so actual exponent is 2 Step 3. Significand is …; so 1 + Significand is … Step 4. Number = (-1) S x (1+Significand) x 2 E-127 = (-1) 1 x (1.010) x 2 2 = -101 = -5