MLSYS ENGINEERING

9.4. Arithmetic

We will first see how floating-point multiplication works. To make it easier to follow, we use the decimal format introduced in Decimal floating-point. Multiplying two floating-point numbers is straightforward because each field is handled mostly independently:

sign exponent mantissa + 5 3 1 2 (+1.2 × 103) − 5 2 2 3 (−2.3 × 102) − 5 5 2 8 (−2.76 → −2.8) × + (add) × (multiply)
Figure 36. Floating-point multiplication multiplies mantissas and adds exponents.

As shown in Figure 36, the process breaks down into three simple steps:

  1. Sign: The signs are multiplied together. A positive number multiplied by a negative number produces a negative result.
  2. Exponent: The exponents are added together (3 + 2 = 5).
  3. Mantissa: The mantissas are multiplied together (1.2 × 2.3 = 2.76). Since our decimal format only stores two mantissa digits, the hardware rounds 2.76 up to 2.8.

If the mantissa product reaches 10 or more, like 4.0 × 4.0 = 16, the hardware shifts it back to 1.6 and adds 1 to the exponent.

Now let's see how floating-point addition works. As shown in Figure 37, to add two numbers like 1.2 × 103 and -2.3 × 102, the hardware follows three steps:

sign exponent mantissa + 5 3 1 2 (+1.2 × 103) − 5 2 2 3 (−2.3 × 102) 1. Align exponent − 5 3 0 2 3 (−0.23 × 103) 2. Add mantissas 1.20 − 0.23 = 0.97 + 5 3 0 9 7 (+0.97 × 103) 3. Normalize + 5 2 9 7 (+9.7 × 102)
Figure 37. Floating-point addition aligns exponents before adding mantissas.
  1. Align exponents: First, the hardware aligns the exponents. Shifting -2.3 × 102 right by one position gives -0.23 × 103, so the sum becomes (1.20 - 0.23) × 103.
  2. Add mantissas: With both numbers sharing the exponent 103, their mantissas are added directly: 1.20 - 0.23 = 0.97.
  3. Normalize: Shift back to standard scientific notation from 0.97 × 103 to 9.7 × 102.