Edexcel International Advanced Level Computer Science, point 11.3.1
Definition
Floating-point representation stores a real number in scientific notation in binary: a sign, a significand (mantissa) holding the digits, and an exponent saying where the binary point goes. The point "floats", so the same number of bits can store very large and very small numbers. IEEE-754 is the standard that defines the formats.
You must use the 16-bit half-precision format.
Function and why needed
Fixed-point (2.1.4) has a fixed range and precision. Floating point can cover a huge range (from tiny to enormous) using few bits, which is needed for science, engineering and graphics. It trades exactness for range.
Characteristics: the 16-bit format
| Part | Bits | Meaning |
|---|---|---|
| Sign (S) | 1 | 0 = positive, 1 = negative (sign-and-magnitude: the significand isn't two's complement) |
| Exponent (E) | 5 | Biased: stored value true exponent |
| Significand (M) | 10 | Fraction bits after an implied leading 1 |
Value of a normal number:
- Bias . A biased exponent lets negative exponents be stored as positive numbers, so numbers can be compared as unsigned patterns.
- Normalised: the number is written as , so the leading 1 is always there and isn't stored (the implied bit). This gives 11 bits of precision from 10 stored bits.
- Stored exponent range for normal numbers: 1 to 30 (true exponent to ).
- Special values: exponent 0 is for zero and tiny "subnormal" numbers, and exponent 31 () is for infinity and NaN ("not a number").
Operation: converting a decimal number to 16-bit floating point
Example: 5.75
- Sign: positive, so S = 0.
- Convert to binary: .
- Normalise: (the point moved 2 places left).
- Biased exponent: .
- Significand: the bits after the point:
0111, padded with zeros to 10 bits:0111000000. - Result:
0 10001 0111000000.
Example: -0.15625
- S = 1 (negative).
- (since ).
- Normalise: .
- Biased exponent: .
- Significand:
01padded:0100000000. - Result:
1 01100 0100000000.
Operation: converting 16-bit floating point to decimal
Example: 0 10010 1010000000
- Sign: 0, so positive.
- Exponent: , so the true exponent .
- Significand: add the implied 1: .
- Shift the point 3 places right: .
- Result: 13.
Range and precision
| Quantity | Value |
|---|---|
| Largest | |
| Smallest normal positive | |
| Precision | About 3 decimal digits (11 significant bits) |
| Number of bits | 16 |
- More exponent bits give a larger range.
- More significand bits give greater precision.
- A fixed total number of bits means a trade-off between range and precision (as with fixed point).
- Larger formats (single 32-bit: 8-bit exponent, 23-bit significand; double 64-bit: 11 and 52) give more of both.
Rounding and accuracy
Most decimal fractions can't be stored exactly, because they need an infinite number of binary places:
In 16-bit: is stored as 0 01011 1001100110, which is 0.0999755859375, not exactly 0.1.
Consequences:
- Rounding errors appear, and can build up in long calculations.
- Never compare floating-point numbers for exact equality:
0.1 + 0.2 == 0.3isFalsein Python. Compare with a small tolerance instead. - Subtracting nearly equal numbers loses accuracy.
- Very large and very small numbers added together: the small one can disappear.
- Overflow and underflow (2.2.3) occur outside the range.
Benefits and drawbacks
- Benefits: huge range from few bits, handles tiny and enormous values, and standardised (the same on every computer, supported in hardware).
- Drawbacks: approximate (rounding errors), less precise than fixed point for the same bits near zero, slower than integer arithmetic, and more complex. Money should not use floating point (use integers in pence).
Worked example
Task: convert -6.5 to 16-bit IEEE-754 format, and check by converting back.
- S = 1.
- .
- Normalise: .
- Biased exponent: .
- Significand:
101padded:1010000000. - Result:
1 10001 1010000000.
Check: exponent , significand , shifted 2 places gives , and the sign 1 makes it -6.5. ✓
Exam tip: write the structure first (1, 5, 10 bits), and work in steps: sign, binary, normalise, add the bias, fill the significand. Remember the implied leading 1 isn't stored, and the bias is 15. When converting back, subtract the bias and put the 1 back. Fixed point (2.1.4) and floating point differ: floating point has a larger range but is approximate.
Check yourself
What are the three parts of a 16-bit IEEE-754 number, and how many bits does each have?
Sign (1 bit), biased exponent (5 bits) and significand (10 bits).
What is the bias, and why is it used?
- It is added to the true exponent so that negative exponents can be stored as positive numbers.
What is the implied bit?
The leading 1 before the binary point in a normalised significand, which isn't stored, giving an extra bit of precision.
Why can 0.1 not be stored exactly?
Its binary form is an infinitely repeating fraction, so it must be rounded to fit the available bits.
What is the stored value of the exponent for 2³?
3 + 15 = 18, which is 10010 in binary.