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:
Figure 36. Floating-point multiplication multiplies mantissas and adds exponents.
As shown in Figure 36, the process breaks down into three simple steps:
- Sign: The signs are multiplied together. A positive number multiplied by a negative number produces a negative result.
- Exponent: The exponents are added together (3 + 2 = 5).
- 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:
Figure 37. Floating-point addition aligns exponents before adding mantissas.
- 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.
- Add mantissas: With both numbers sharing the exponent 103, their mantissas are added directly: 1.20 - 0.23 = 0.97.
- Normalize: Shift back to standard scientific notation from 0.97 × 103 to 9.7 × 102.