================================ WEEK 5: ADDENDA ============================= In these supplementary notes for the Week 5 lecture, we have the two algorithms for adding and multiplying floating-point numbers seen in class. Floating-point addition ----------------------- Given: base b, floating-point numbers: u = f_u * b^e_u, that is u = (f_u, e_u) v = f_v * b^e_v, that is v = (f_v, e_v) p-digit mantissa Find the sum w = u (+) v Algorithm: A1. [Unpack] Separate exponent and fraction parts of the representation of u and v: u v ------------------ exponent e_u e_v mantissa f_u f_v A2. [Assume e_u >= e_v] If e_u < e_v, then interchange roles of u and v. A3. [Set e_w] e_w = e_u. A4. [Test e_u - e_v] If e_u - e_v >= p+2 (that is, e_u >> e_v), then set f_w = f_u. Go to Step A7. A5. [Scale right] Shift f_v to the right e_u - e_v places; this in reality means that f_v is being divided by b^(e_u-e_v). A6. [Add] Set f_w = f_u + f_v. A7. [Normalize] Here round the last (that is, the p-th digit) of the mantissa with respect to the value of the p+1-th digit, and truncate the mantissa to keep only its first p digits. Example: Given: base 10 u = 0.3472e+2, v = 0.11202-1 4-digit mantissa Find the sum w = u (+) v A1. [Unpack] u v ------------------ exponent +2 -1 mantissa 0.3472 0.1120 A2. [Assume e_u >= e_v] We in fact have e_u >= e_v; therefore no interchange of the roles of u and v. A3. [Set e_w] e_w = +2. A4. [Test e_u - e_v] We don't have e_u >> e_v); thus we don't set f_w = 0.3472 and we go to the next step which is A5. A5. [Scale right] Shift 0.1120 to the right 2 - (-1) places; this in reality means that 0.1120 is being divided by 10^(2-(-1)) = 10^3. So we get f_v = 0.1120/(10^3) = 0.0001120. A6. [Add] Set f_w = 0.3472 + 0.0001120 = 0.3473120. A7. [Normalize] Here round the 4-th digit of the mantissa with respect to the value of the 5-th digit, and truncate the mantissa to keep only its first 4 digits. So the final result is: f_w = 0.3473 e_w = +2 w = 0.3473e+2 Floating-point multiplication ----------------------------- Given: base b, floating-point numbers: u = f_u * b^e_u, that is u = (f_u, e_u) v = f_v * b^e_v, that is v = (f_v, e_v) p-digit mantissa Find the product w = u (x) v (or u (*) v) Algorithm: M1. [Unpack] Separate exponent and fraction parts of the representation of u and v: u v ------------------ exponent e_u e_v mantissa f_u f_v M2. [Operate] Set e_w = e_u + e_v f_w = f_u * f_v Truncate f_w to p+2 digit at this point if necessary. M3. [Normalize] Same as for the operation (+). Example: Given: base 10 u = 0.1344e+2, v = 0.71881e+1 4-digit mantissa Find the product w = u (*) v M1. [Unpack] u v ------------------ exponent +2 +1 mantissa 0.1344 0.71881 M2. [Operate] Set e_w = 2+1 f_w = 0.1344 * 0.71881 = 0.09660672 M3. [Normalize] We get w = 0.9661e+2