A levels Computer Science · Data representation
Floating point
1Why floating point?
With a fixed number of bits, fixed point has to choose between a large range (big numbers) and good precision (small fractions). Floating point lets the binary point "float", so it can store very large and very small numbers.
A floating-point number has two parts, like standard form:
$$\text{value} = \text{mantissa} \times 2^{\text{exponent}}$$
- The mantissa holds the significant digits (and sign).
- The exponent says how far to move the binary point.
In A-level questions, both are usually stored in two's complement, with the binary point just after the first bit of the mantissa.
Check yourself
What does the exponent in a floating-point number do?
Show the answer
Says how far to move the binary point
The power of 2.
2Mantissa and exponent
A floating-point number with an 8-bit mantissa and a 4-bit exponent, both in two's complement. The binary point sits after the mantissa's first bit.
A positive number
- Mantissa 0.1101000, exponent 0011.
- Exponent = 3, so move the point 3 places right: 0110.1000.
- Value = 4 + 2 + 0.5 = 6.5. (Check: 0.8125 × 2³ = 6.5.)
A negative mantissa
- Mantissa 1.0110000, exponent 0010.
- Mantissa value = −1 + 0.25 + 0.125 = −0.625.
- Value = −0.625 × 2² = −2.5.
A negative exponent
- Mantissa 0.1100000, exponent 1110 (= −2).
- Value = 0.75 × 2⁻² = 0.1875.
Check yourself
What is the value of mantissa 0.1010000 with exponent 0010?
Show the answer
2.5
0.625 × 4.
3Normalisation
A floating-point number is normalised when the first two bits of the mantissa are different:
- positive numbers start 0.1;
- negative numbers start 1.0.
Normalising gives the maximum precision for the mantissa bits, and gives each value one unique representation.
Normalising
- Mantissa 0.0011010, exponent 0101 (5).
- Shift the mantissa 2 places left to get 0.1101000.
- Each left shift needs the exponent reduced by 1: 5 − 2 = 3.
- Normalised: 0.1101000, exponent 0011.
Check yourself
Is the mantissa 1.1010000 normalised?
Show the answer
No, a negative normalised mantissa starts 1.0
First two bits must differ.
4Converting denary to floating point
12.25 (8-bit mantissa, 4-bit exponent)
- 12.25 = 1100.01 in binary.
- Move the point to give 0.110001: this is 4 places left, so the exponent is 4.
- Mantissa 0.1100010, exponent 0100.
−3.75
- 3.75 = 011.11. Two's complement of this: 100.01 (check: −4 + 0.25 = −3.75).
- Normalise to 1.0001: the point moves 2 places left, so the exponent is 2.
- Mantissa 1.0001000, exponent 0010.
Check yourself
What is the exponent when 5.5 (101.1) is normalised?
Show the answer
3 (0011)
0.1011 × 2³.
5Range, precision and errors
With a fixed total number of bits:
- more mantissa bits → better precision;
- more exponent bits → greater range.
| Error | Meaning |
|---|---|
| rounding error | the number cannot be stored exactly |
| absolute error | the size of the difference between the true and stored values |
| relative error | absolute error ÷ true value |
| overflow | the number is too large for the exponent |
| underflow | the number is too small (too close to 0) to represent |
Storing 6.7
- With a 7-bit fraction in the mantissa and exponent 3, the closest value is 0.1101011 × 2³ = 6.6875.
- Absolute error = 6.7 − 6.6875 = 0.0125.
- Relative error = 0.0125 ÷ 6.7 = 0.19%.
Check yourself
Which part of a floating-point number should get more bits for better precision?
Show the answer
The mantissa
Significant digits.
6Floating point versus fixed point
| Fixed point | Floating point |
|---|---|
| simpler and faster to process | slower; needs more complex hardware |
| smaller range | much larger range |
| constant absolute precision | constant relative precision |
| good for money (pence) | good for science and graphics |
Real computers use the IEEE 754 standard: a 32-bit float has 1 sign bit, 8 exponent bits and 23 mantissa bits.
Because of rounding errors, two floating-point numbers that "should" be equal may not be: in Python, 0.1 + 0.2 == 0.3 is False. Compare with a small tolerance instead.
Check yourself
Why should money usually be stored as an integer number of pence rather than a float?
Show the answer
Floats can introduce rounding errors with decimal fractions
0.1 is inexact.
Now test yourself
This lesson has a 8-question quiz and 5 exam-style questions with mark schemes in Impera. Free, with your progress saved.
Practise in Impera