Carnegie MellonIntroduction to Computer Systems15-213/18-243, spring 20094thLecture, Jan. 22ndInstructors:Gregory Kesden and Markus PüschelCarnegie MellonLast Time: Integers Representation: unsigned and signed Conversion, casting Bit representation maintained but reinterpreted Expanding, truncating Truncating = mod Addition, negation, multiplication, shifting Operations are mod 2w “Ring” properties hold Associative, commutative, distributive, additive 0 and inverse Ordering properties do not hold u > 0 does not mean u + v > v u, v > 0 does not mean u · v > 0Carnegie MellonToday: Floating Point Background: Fractional binary numbers IEEE floating point standard: Definition Example and properties Rounding, addition, multiplication Floating point in C SummaryCarnegie MellonFractional binary numbers What is 1011.101?Carnegie Mellon• • •b–1.Fractional Binary Numbers Representation Bits to right of “binary point” represent fractional powers of 2 Represents rational number:bibi–1b2b1b0b–2b–3b–j• • •• • •1242i–12i• • •1/21/41/82–jbk2kk jiCarnegie MellonFractional Binary Numbers: Examples Value Representation5-3/42-7/863/64 Observations Divide by 2 by shifting right Multiply by 2 by shifting left Numbers of form 0.111111…2are just below 1.0 1/2 + 1/4 + 1/8 + … + 1/2i+ … 1.0 Use notation 1.0 –101.11210.11120.1111112Carnegie MellonRepresentable Numbers Limitation Can only exactly represent numbers of the form x/2k Other rational numbers have repeating bit representations Value Representation1/3 0.0101010101[01]…21/5 0.001100110011[0011]…21/10 0.0001100110011[0011]…2Carnegie MellonToday: Floating Point Background: Fractional binary numbers IEEE floating point standard: Definition Example and properties Rounding, addition, multiplication Floating point in C SummaryCarnegie MellonIEEE Floating Point IEEE Standard 754 Established in 1985 as uniform standard for floating point arithmetic Before that, many idiosyncratic formats Supported by all major CPUs Driven by numerical concerns Nice standards for rounding, overflow, underflow Hard to make fast in hardware Numerical analysts predominated over hardware designers in defining standardCarnegie Mellon Numerical Form: (–1)sM 2E Sign bit s determines whether number is negative or positive Significand M normally a fractional value in range [1.0,2.0). Exponent E weights value by power of two Encoding MSB s is sign bit s exp field encodes E (but is not equal to E) frac field encodes M (but is not equal to M)Floating Point Representations exp fracCarnegie MellonPrecisions Single precision: 32 bits Double precision: 64 bits Extended precision: 80 bits (Intel only)s exp fracs exp fracs exp frac1 8 231 11 521 15 63 or 64Carnegie MellonNormalized Values Condition: exp 000…0 and exp 111…1 Exponent coded as biased value: E = Exp – Bias Exp: unsigned value exp Bias = 2e-1- 1, where e is number of exponent bits Single precision: 127 (Exp: 1…254, E: -126…127) Double precision: 1023 (Exp: 1…2046, E: -1022…1023) Significand coded with implied leading 1: M = 1.xxx…x2 xxx…x: bits of frac Minimum when 000…0 (M = 1.0) Maximum when 111…1 (M = 2.0 – ) Get extra leading bit for “free”Carnegie MellonNormalized Encoding Example Value: Float F = 15213.0; 1521310= 111011011011012 = 1.11011011011012x 213 SignificandM = 1.11011011011012frac = 110110110110100000000002 ExponentE = 13Bias = 127Exp = 140 = 100011002 Result:0 10001100 11011011011010000000000 s exp fracCarnegie MellonDenormalized Values Condition: exp = 000…0 Exponent value: E = –Bias + 1 (instead of E = 0 – Bias) Significand coded with implied leading 0: M = 0.xxx…x2 xxx…x: bits of frac Cases exp = 000…0, frac = 000…0 Represents value 0 Note distinct values: +0 and –0 (why?) exp = 000…0, frac 000…0 Numbers very close to 0.0 Lose precision as get smaller EquispacedCarnegie MellonSpecial Values Condition: exp = 111…1 Case: exp = 111…1, frac = 000…0 Represents value (infinity) Operation that overflows Both positive and negative E.g., 1.0/0.0 = 1.0/ 0.0 = + , 1.0/ 0.0 = Case: exp = 111…1, frac 000…0 Not-a-Number (NaN) Represents case when no numeric value can be determined E.g., sqrt(–1),Carnegie MellonVisualization: Floating Point Encodings+0+Denorm +Normalized-Denorm-Normalized+0NaNNaNCarnegie MellonToday: Floating Point Background: Fractional binary numbers IEEE floating point standard: Definition Example and properties Rounding, addition, multiplication Floating point in C SummaryCarnegie MellonTiny Floating Point Example 8-bit Floating Point Representation the sign bit is in the most significant bit. the next four bits are the exponent, with a bias of 7. the last three bits are the frac Same general form as IEEE Format normalized, denormalized representation of 0, NaN, infinitys exp frac1 4 3Carnegie MellonDynamic Range (Positive Only)s exp frac E Value0 0000 000 -6 00 0000 001 -6 1/8*1/64 = 1/5120 0000 010 -6 2/8*1/64 = 2/512…0 0000 110 -6 6/8*1/64 = 6/5120 0000 111 -6 7/8*1/64 = 7/5120 0001 000 -6 8/8*1/64 = 8/5120 0001 001 -6 9/8*1/64 = 9/512…0 0110 110 -1 14/8*1/2 = 14/160 0110 111 -1 15/8*1/2 = 15/160 0111 000 0 8/8*1 = 10 0111 001 0 9/8*1 = 9/80 0111 010 0 10/8*1 = 10/8…0 1110 110 7 14/8*128 = 2240 1110 111 7 15/8*128 = 2400 1111 000 n/a infclosest to zerolargest denormsmallest normclosest to 1 belowclosest to 1 abovelargest normDenormalizednumbersNormalizednumbersCarnegie MellonDistribution of Values 6-bit IEEE-like format e = 3 exponent bits f = 2 fraction bits Bias is 23-1-1 = 3 Notice how the distribution gets denser toward zero. -15 -10 -5 0 5 10 15Denormalized Normalized Infinitys exp frac1 3 2Carnegie MellonDistribution of Values (close-up view) 6-bit IEEE-like format e = 3 exponent bits f = 2 fraction bits Bias is 3-1 -0.5 0 0.5 1Denormalized Normalized Infinitys exp frac1 3 2Carnegie MellonInteresting NumbersDescription exp frac Numeric Value Zero 00…00 00…00 0.0 Smallest Pos. Denorm. 00…00 00…01 2– {23,52}x 2– {126,1022} Single 1.4 x 10–45 Double 4.9 x 10–324
View Full Document