Sub topic 7
Fixed-point range, resolution and limitations
This chapter assumes you can encode 2's complement integers and convert binary fractions. The fixed-point encoding itself was covered in the previous chapter. Here you work out what a fixed-point format can and cannot represent, and why those limits led to floating point.
The format and its notation
A fixed-point number is an ordinary integer with an implied binary point. The hardware stores only the integer, and the programmer remembers where the point sits. If the integer is n and there are F fraction bits, the value is n / 2^F.
This chapter uses the notation Qm.n. The format has m integer bits, including the sign bit, and n fraction bits, so the word has m + n bits. Q4.4 is an 8-bit word with 4 integer bits and 4 fraction bits. Some textbooks leave the sign bit out of m, so check the convention in any exam question that uses Q notation.
Range
The scaling by 2^F applies to the whole integer range. Take a word of N bits with F fraction bits.
| Encoding | Smallest value | Largest value |
|---|---|---|
| Unsigned | 0 | (2^N - 1) / 2^F = 2^(N-F) - 2^-F |
| 2's complement | -2^(N-1) / 2^F = -2^(N-F-1) | (2^(N-1) - 1) / 2^F = 2^(N-F-1) - 2^-F |
For Q4.4 in 2's complement, the smallest value is -128/16 = -8. The largest is 127/16 = 7.9375. The largest value is never a round power of two. It sits one step below it.
Resolution
Resolution is the gap between two adjacent representable values. In fixed point it is always one unit in the last place:
- Resolution = 2^-F, the same everywhere in the range.
- The number of distinct values is 2^N, whatever the value of
F. - If you round a real number to the nearest representable value, the largest error is half a step, 2^-(F+1).
Q4.4 has a resolution of 1/16 = 0.0625 and a maximum rounding error of 0.03125. Q8.8 in a 16-bit word has a resolution of 1/256 and a maximum rounding error of 1/512.
The range and resolution trade-off
The word size N is fixed, so every fraction bit you add takes one integer bit away. Moving the binary point one place to the right halves the step and also halves the range.
This table shows the 16-bit, 2's complement case for three choices of F.
| Fraction bits F | Smallest | Largest | Resolution |
|---|---|---|---|
| 0 | -32768 | 32767 | 1 |
| 8 | -128 | 127.99609375 | 0.00390625 |
| 15 | -1 | 0.999969482421875 | 0.000030517578125 |
The following function computes these numbers for any word size.
def fixed_range(total, frac, signed=True):
if signed:
lo, hi = -(1 << (total - 1)), (1 << (total - 1)) - 1
else:
lo, hi = 0, (1 << total) - 1
scale = 1 << frac
return lo / scale, hi / scale, 1 / scale
for frac in (0, 8, 15):
print(frac, fixed_range(16, frac))Each line prints the range and resolution of one format. Powers of two divide exactly in binary floating point, so the printed values are exact.
Sizing a format
Suppose you need values from -100 to +100 with a resolution of at most 0.01. Work out the two halves separately:
- Range: a signed word with
Iinteger bits (excluding the sign bit) covers up to 2^I. Since 2^7 = 128 ≥ 100, you need I = 7. - Resolution: you need 2^-F ≤ 0.01. Since 2^-7 = 0.0078125 ≤ 0.01 but 2^-6 = 0.015625 is too coarse, you need F = 7.
- Add the sign bit. The total is 1 + 7 + 7 = 15 bits, which is Q8.7.
Limitations
Error is absolute, not relative
The error bound 2^-(F+1) is the same for every value. A value of 1000 and a value of 0.001 both get the same absolute error. For large values this is negligible, but for small values it can swamp the number.
In Q4.4, 0.1 becomes round(0.1 * 16) = 2, which reads back as 0.125. The error is 0.025, or 25% of the value. Any value below half a step, 0.03125, rounds to 0. Decimal fractions such as 0.1 are not exact in any binary fixed-point format, because 0.1 has an infinite binary expansion.
Overflow
Addition and subtraction of two numbers in the same format are plain integer operations, so the 2's complement overflow rules still apply. The result of a sum can fall outside the range. There are two common policies:
- Wrap-around keeps the low
Nbits. A large positive result becomes a negative one. - Saturation clamps the result to the largest or smallest representable value. Signal-processing hardware often does this, because a clamped error is smaller than a sign flip.
Multiplication needs a double-width result and a shift
Multiplying two fixed-point numbers multiplies their scale factors too. Q(a).(b) times Q(c).(d) gives a Q(a+c).(b+d) result, which has twice as many bits. To get back to the original format you shift right by F, and that shift discards low-order bits.
F = 4
TOTAL = 8
SCALE = 1 << F
def to_fixed(x):
return round(x * SCALE)
def saturate(n):
return max(-(1 << (TOTAL - 1)), min((1 << (TOTAL - 1)) - 1, n))
def wrap(n):
half = 1 << (TOTAL - 1)
return ((n + half) % (1 << TOTAL)) - half
a = to_fixed(2.5) # 40
b = to_fixed(3.25) # 52
raw = a * b # 2080, scaled by 2^(2F), needs 12 bits
prod = raw >> F # 130, back to scale 2^F
print(raw, prod, prod / SCALE) # 2080 130 8.125
print(saturate(prod) / SCALE) # 7.9375
print(wrap(prod) / SCALE) # -7.875
print((to_fixed(0.0625) ** 2) >> F) # 0The exact product 8.125 does not fit in Q4.4, whose largest value is 7.9375. Saturation gives 7.9375. Wrap-around gives -7.875, because 130 in 8 bits is the same bit pattern as -126. The last line shows underflow. The product 0.0625 × 0.0625 should be 0.00390625, which is smaller than a step, so the shift discards it and the result is 0.
The right shift of a negative number rounds toward minus infinity, not toward zero. This biases the error in one direction. To round to nearest, add half a step (1 << (F - 1)) before shifting.
Errors accumulate
Every multiplication and every conversion adds up to half a step of error. Over a long chain of operations, such as a filter that runs for millions of samples, these errors add up. Choosing F is a design decision made once, and it fixes the error budget for the whole program.
One format cannot fit values of very different sizes
A program that handles both 0.00001 and 1,000,000 needs both a tiny step and a huge range. With a fixed word, the number of bits needed is about log2(range / resolution). Here that is log2(10^6 / 10^-5) = log2(10^11) ≈ 37 bits, and that is only the minimum for a single format. Floating point avoids this cost by letting the step size change with the magnitude of the number.
Summary of decision criteria
| Situation | Better choice |
|---|---|
| Values stay in a known, narrow range, hardware is cheap | Fixed point |
| Absolute error is acceptable everywhere (money in cents, sensor counts) | Fixed point |
| Values span many orders of magnitude | Floating point |
| Relative error must stay roughly constant | Floating point |
Exercise
A 12-bit signed 2's complement fixed-point format has 5 fraction bits. (a) Give the smallest value, the largest value and the resolution. (b) What is the largest rounding error when a real number inside the range is stored? (c) Is 0.3 stored exactly?
Show answer
(a) The integer part has 12 - 5 = 7 bits including the sign, so the range is -2^6 = -64 to 2^6 - 2^-5 = 63.96875. The resolution is 2^-5 = 0.03125. (b) The largest rounding error is half a step, 2^-6 = 0.015625. (c) No. 0.3 × 32 = 9.6, which rounds to 10, and 10/32 = 0.3125. The error is 0.0125, which is within 0.015625.
Exercise
In Q4.4 (8-bit, 2's complement), compute 3.5 × 2.5 using integer arithmetic with a right shift by 4. What is the raw product, the shifted result and the value it represents? Does it overflow?
Show answer
3.5 is stored as 56 and 2.5 as 40. The raw product is 56 × 40 = 2240, which has scale 2^8. Shifting right by 4 gives 140. That is greater than 127, so it overflows Q4.4. The true value is 140/16 = 8.75, but the 8-bit pattern of 140 is the same as -116, which reads as -7.25 with wrap-around. With saturation the result is 127, which reads as 7.9375.
Exercise
You need a signed fixed-point format that represents values from -500 to +500 with a resolution of at most 0.005. How many bits are needed, and what is the Qm.n format?
Show answer
For the range, 2^9 = 512 ≥ 500, so 9 integer bits are needed, plus 1 sign bit, so m = 10. For the resolution, 2^-n ≤ 0.005 requires n = 8, because 2^-8 = 0.00390625 and 2^-7 = 0.0078125 is too coarse. The format is Q10.8, which needs 18 bits.