1.4.1 - Floating-point arithmetic

1.4.1 - Floating-point arithmetic

Adding floating-point bit patterns requires more than adding the two mantissa fields. This part of 1.4.1 shows why exponents must be aligned, how signed mantissas are added or subtracted, and how the result is normalised. You will also explain when limited precision or exponent range prevents an exact result.

Align the scales before combining mantissas

Mantissas can be added directly only when their exponents agree. The reason is the same as adding quantities expressed in different units of scale: m1×2e+m2×2e=(m1+m2)×2em_1\times2^e+m_2\times2^e=(m_1+m_2)\times2^e, but this factorisation fails if the exponents differ.

All stored values in this lesson use six two's-complement mantissa bits, with the point immediately after the sign bit, followed by four two's-complement exponent bits. Mantissa weights are -1, 1/2, 1/4, 1/8, 1/16 and 1/32; exponents range from -8 to +7. We keep extra intermediate digits when necessary and round only at the stated final step. This is the signed-mantissa teaching format, not Python's float layout.

Consider +5.5 = 010110 0011 and +1.5 = 011000 0001. The first mantissa is 0.6875 scaled by 232^3; the second is 0.75 scaled by 212^1. Use the larger exponent, +3. Increasing the second exponent by two requires dividing its mantissa by four: 011000 → 001100 → 000110. It becomes 0.1875, and 0.1875×8=1.50.1875\times8=1.5. Now the two mantissas have equal scale.

For a negative mantissa, an arithmetic right shift copies the sign bit into the new leading position. -1.5 = 101000 0001 aligns as 101000 → 110100 → 111010 at exponent +3; 111010 means -0.1875. Inserting leading zeros instead would turn it positive. If a shift discards nonzero digits, keep them as guard digits in the working when the task permits; premature truncation can change the sum.

Add signed mantissas and normalise the sum

After alignment, add the mantissas in binary and retain a correct signed intermediate value. For the +5.5 and +1.5 example, 010110 + 000110 = 011100. This mantissa is 0.875, already normalised, so the result is 011100 0011, or 7.

Negative operands use the same two's-complement mechanism. For -5.5 + -1.5, alignment gives 101010 + 111010 = 1 100100. The carry is discarded, leaving mantissa -0.875, so 100100 0011 represents -7. For -5.5 + +1.5, 101010 + 000110 = 110000, meaning -0.5 at exponent 3. It is unnormalised because it starts 11; shift left to 100000 and reduce the exponent to 2. The final 100000 0010 represents -4.

An intermediate sum may need more integer space than a stored mantissa permits. For +6 + +5 at exponent +3, the mantissas are 0.11000 and 0.10100. Their mathematical sum is 1.01100 = 1.375. Keep a wider signed working field, written 01.01100, so the positive sign is retained. Shift the point one place left to 0.101100, increase the exponent to +4, and store the exact six-bit mantissa 010110. The result 010110 0100 represents 11. Treating the original six-bit carry pattern 101100 as a negative mantissa would be a fixed-width overflow error.

The sequence is therefore align, calculate with enough intermediate space, normalise, then store at the final precision. Decode the final pair to check sign and magnitude against the original operands.

Subtract by negating the aligned subtrahend

Floating-point subtraction uses the same scale alignment as addition. To calculate A−BA-B, align the original values, then subtract the mantissas or add the correctly negated subtrahend. Negating a value changes its mantissa's sign, not its exponent.

For +5.5 - +1.5, exponent alignment gives 010110 minus 000110, both at exponent +3. Negate the aligned second mantissa: invert 000110 to 111001, add one to get 111010. Then 010110 + 111010 = 1 010000. Retain 010000, already normalised, with exponent 0011. Its value is 0.5 × 8 = 4.

For +1.5 - +5.5, use the same exponent +3 but reverse the aligned operands: 000110 + 101010 = 110000. This is -0.5 × 8 = -4. Normalise the negative mantissa to 100000 and decrease the exponent to 0010, still -4. Reversing the operands reverses the sign, as expected.

For +5.5 - (-1.5), the aligned subtrahend is 111010. Its negation is 000110, so the addition produces 011100 0011, or +7. For -5.5 - +1.5, the aligned calculation is 101010 + 111010, giving 100100 0011, or -7. These cover both positive and negative operands; “subtraction makes a value smaller” is unsafe when the subtrahend is negative.

Continue this lesson.

You’re previewing NovaLearn without an account. Create a free account to read the rest.

Create free account