OCR H446, section 1.4.1 (h)
Floating point numbers can't simply be added bit by bit, because each has its own exponent. They must first be brought to the same scale. This page covers addition and subtraction, for positive and negative numbers.
The format is the same as in 1.4.1 (g): an 8-bit two's complement mantissa (with the point after the sign bit) and a 4-bit two's complement exponent.
The method
- Compare the exponents. The number with the smaller exponent must be adjusted.
- Align: shift the mantissa of the smaller number right (once for each step) and increase its exponent by 1 each time, until both exponents are equal.
- Add or subtract the mantissas (as two's complement fixed point numbers).
- The exponent of the answer is the common exponent.
- Normalise the result if needed (shift the mantissa left and reduce the exponent, or shift right and increase it if the mantissa overflowed).
- Check for overflow (an exponent too large) or underflow (too small).
Example 1: addition
Add and .
- : mantissa (), exponent (3). Value .
- : mantissa (), exponent (1). Value .
Step 1 and 2: align. has the smaller exponent (1, against 3), so shift its mantissa right 2 places and raise its exponent by 2:
Check: , and . ✓
Step 3: add the mantissas.
0.1000000 (A)
+ 0.0011000 (B, aligned)
-----------
0.1011000
Result: mantissa (), exponent (3).
Value: . ✓ (.) It starts , so it is already normalised.
Example 2: subtraction
Subtract from using the same numbers: .
0.1000000 (A)
- 0.0011000 (B, aligned)
-----------
0.0101000
The result mantissa is with exponent 3. It starts , so it isn't normalised.
Normalise: shift left 1 place: , and reduce the exponent by 1 to 2 ().
Value: . ✓ (.)
(In practice, subtraction is done by adding the two's complement of the second mantissa, as with integers.)
Example 3: a result that needs a big normalising shift
Subtract , with both at exponent .
- : mantissa (). : mantissa ().
0.1100000
- 0.1011000
-----------
0.0001000
The result is , exponent 3. Shift left 3 places to get , and reduce the exponent by 3 to 0 ().
Value: . ✓ (.)
Information may be lost. Each shift left brings in zeros on the right, so the result has fewer reliable bits than the format allows.
Negative numbers
Handle them through their two's complement mantissas, and add as usual. For example, :
- : mantissa , exponent .
- : mantissa (), exponent (2).
- Align: has the smaller exponent, so shift its mantissa right 1 place and raise the exponent to 3. When shifting a negative mantissa right, copy the sign bit into the new place: . Check: , and . ✓
- Add:
0.1010000 (+0.625)
+ 1.1010000 (-0.375)
-----------
10.0100000 -> ignore the carry out: 0.0100000
- Result: (), exponent 3: value . ✓ (.)
- Normalise: shift left 1 to get , exponent (2): . ✓
Another method: via fixed point
OCR's mark schemes often use a different route, which gives the same answer. Turn each number into fixed point binary, do the sum, then normalise the result.
- Convert both exponents to denary.
- Apply each exponent: move each mantissa's binary point that many places (right for a positive exponent), giving two fixed point numbers.
- Line up the binary points (pad with zeros) and add or subtract in binary.
- Normalise the answer back into the format asked for.
Example: subtract .
- Exponents: and .
- Apply them: (point 3 places right, which is 5.5) and (point 1 place right, which is 1.5).
- Line up the points and subtract:
0101.10 (5.5)
- 0001.10 (1.5)
---------
0100.00 (4)
- Normalise: , so the mantissa is and the exponent is . Check: . ✓
To subtract, you can also add the two's complement of the second fixed point number, as with integers. The working must be in binary: converting to denary, calculating and converting back doesn't earn the marks. OCR questions often use a 6-bit mantissa; the method is the same whatever the sizes.
Problems to watch for
| Problem | What happens |
|---|---|
| Loss of precision | Shifting the smaller mantissa right pushes bits off the end, which are lost. The result is rounded, so there are rounding errors |
| Mantissa overflow | Adding two mantissas can give a result of 1 or more, which doesn't fit. Shift right one place and increase the exponent by 1 |
| Exponent overflow | The result needs an exponent larger than the largest that can be stored |
| Exponent underflow | The result needs an exponent smaller than the smallest, so it is too close to zero to represent |
| Adding very different sizes | Adding a tiny number to a huge one: after aligning, the small one's bits are all shifted away, so it has no effect |
Worked example
Task: add and .
- . .
- Align to exponent 4: shift right 2 places: .
- Add: ().
- Result: mantissa , exponent : . ✓ (.) It is normalised.
Exam tip: write each step: align the exponents (shift the smaller number's mantissa right, and increase its exponent), add or subtract, then normalise (and adjust the exponent). Check by converting to denary. Say what happens to precision when bits are shifted out.
Check yourself
Why must the exponents be made equal before adding floating point numbers?
The mantissas are only on the same scale when the exponents are the same, so the digits line up and can be added directly.
Which number's mantissa is shifted when aligning, and in which direction?
The number with the smaller exponent has its mantissa shifted right, and its exponent is increased by 1 for each shift.
A result mantissa is 0.0010100 with exponent 0101. Normalise it
Shift left 2 places to get 0.1010000, and reduce the exponent by 2 to 0011.
Why can floating point addition lose precision?
When aligning, bits of the smaller mantissa are shifted off the end and lost, so the result is rounded.