Fixed-Point Numbers and Rounding
Store fractions as scaled integers. Keep the binary point in line through an add and a multiply, and choose how to drop bits.
You Will Be Able To
- Read a pattern in Q notation and state its range and step.
- Line up two binary points before an addition.
- Pick the right bits of a product to return to the input format.
- Compare truncation, round half up and round half to even.
- Lesson
- G1.4
- Time
- 25 min
- Runs on
- Your browser
- Check
- 5 questions
A controller multiplies a reading of 2.5 by a gain of 1.5. Its display shows 12.0. The multiplier is correct. The product has its binary point in a new place, and the design kept the wrong eight bits.
Weights show bit magnitudes in the fixed-point view. For signed values, the leftmost bit has a negative weight.
- Unsigned
- 40
- Signed fixed point (4 fraction bits)
- 2.5
- 2.5 in Q4.4. The weights under the bits run from 8 down to 1/16. The stored integer is 40, and 40/16 is 2.5.
- The smallest step. Flipping the LSB adds 1/16, or 0.0625. No value between two steps can be stored.
- 1.5 in Q4.4. The gain 1.5 is 1 + 1/2. The stored integer is 24.
The fixed view reads the top bit as a sign, so keep it at 0 for the unsigned values of this lesson.
A Binary Point That Only You Can See
Fixed point stores a fraction as an integer with an agreed scale. The format Qm.n has m integer bits and n fraction bits. The stored integer is the value times 2^n.
| Format | Total bits | Step | Largest unsigned value | The byte 00101000 reads as |
|---|---|---|---|---|
| Q8.0 | 8 | 1 | 255 | 40 |
| Q6.2 | 8 | 1/4 | 63.75 | 10.0 |
| Q4.4 | 8 | 1/16 | 15.9375 | 2.5 |
| Q2.6 | 8 | 1/64 | 3.984375 | 0.625 |
One byte in four formats. More fraction bits give a finer step and a smaller range.
Nothing in the hardware marks the point. An adder or a register sees an integer. The format is a promise that the designer keeps at every operation.
A signed format works the same way with a two's-complement integer. Signed Q4.4 runs from -8 to 7.9375.
Line up the Points to Add
Two numbers can be added only when their binary points line up. If the formats differ, shift one operand first.
Points out of line
logic [7:0] a; // Q4.4, holds 2.5
logic [7:0] b; // Q6.2, holds 1.5
logic [7:0] sum; // Q4.4
assign sum = a + b;
Measured: the sum reads 2.875. The 1.5 was added as 6/16.
Points in line
logic [7:0] a; // Q4.4, holds 2.5
logic [7:0] b; // Q6.2, holds 1.5
logic [7:0] sum; // Q4.4
assign sum = a + (b << 2);
Measured: the sum reads 4.0. The shift gives b two more fraction bits.
A sum of two N-bit numbers can need N + 1 bits. Fixed-point sums overflow and saturate exactly like the integers of lesson G1.3.
A Product Doubles the Fraction Bits
Multiply two stored integers and the scales multiply too. Q4.4 times Q4.4 has a scale of 16 times 16, which is 256. So the 16-bit product is Q8.8.
- I/O
- Datapath
- Neutral
- Data
- Multiply the integers
The multiplier knows nothing about the point. It gives 960, the pattern
03c0. As Q8.8 that is 960/256, or 3.75. - Keep the middle
Bits 11 to 4 have four integer bits and four fraction bits. They hold
3c, which is 3.75 in Q4.4. - The top bits
Bits 15 to 12 are integer bits above the Q4.4 range. If any is 1, the result overflowed and must saturate.
- The bottom bits
Bits 3 to 0 are finer than the Q4.4 step. Dropping them is a rounding decision.
Read the diagram as text
- a = 2.5: Q4.4, stored 40
- b = 1.5: Q4.4, stored 24
- Multiplier: 40 x 24 = 960
- bits 15 to 12: integer bits that do not fit
- bits 11 to 4: Q4.4: 3.75
- bits 3 to 0: extra fraction bits
- a = 2.5 → Multiplier: operand
- b = 1.5 → Multiplier: operand
- Multiplier → bits 15 to 12: must be 0
- Multiplier → bits 11 to 4: the result
- Multiplier → bits 3 to 0: rounded away
The low byte of this product is c0. Read as Q4.4 it is 12.0, the wrong value of the hook.
Three Ways to Drop Bits
Dropping fraction bits changes the value. This change is the quantization error. There are three common rules, and they differ at a tie: a value exactly half way between two results.
- Cycle 2
0.5 is a tie. Truncation gives 0, round half up gives 1, and round half to even gives 0, the even neighbour.
- Cycle 3
0.75 is not a tie. Both rounding rules give 1. Truncation still gives 0.
- Cycle 6
1.5 is a tie between 1 and 2. Both rounding rules give 2: here the even neighbour is the upper one.
- Cycle 10
2.5 is a tie between 2 and 3. Round half up gives 3. Round half to even gives 2.
Measured on the simulator. The two rounding rules differ only at the ties 0.5 and 2.5.
| Rule | How it is built | Largest error of one value | Sum of the errors over these 16 values |
|---|---|---|---|
| Truncate | drop the bits | 0.75, always down | -6 |
| Round half up | add half a step, then drop the bits | 0.5 | +2 |
| Round half to even | round half up, then clear the LSB at a tie | 0.5 | 0 |
Computed from the measured rows above, in whole steps. Truncation can lose almost one step; rounding never errs by more than half a step. Only round half to even has errors that cancel.
Truncation costs no logic, and its error always points down. On a two's-complement value "down" means toward minus infinity: -0.25 truncates to -1. Round half up costs one adder. Round half to even also checks for a tie, and it removes the last small bias.
Asked in interviews
What is Q4.4, and what is its resolution?
A fixed-point format with four integer bits and four fraction bits. The stored integer is the value times 16, so the step is 1/16.
Two Q4.4 numbers are multiplied. Where is the point in the product?
The product has 16 bits with eight fraction bits: Q8.8. To return to Q4.4, keep bits 11 to 4 and check bits 15 to 12 for overflow. Round away bits 3 to 0.
Why use round half to even?
Round half up sends every tie upward, which adds a small positive bias. Round half to even sends half of the ties up and half down, so the errors cancel over many values.
Try It
The stored integers are 40 and 24. Their product is 960, the pattern 03c0. Read with eight fraction bits, that is 3.75.
Bits 11 to 4 of the product are 3c. Read as Q4.4, that is 3.75 again.
Change the kept bits to product[7:0] and run. The last line shows 12.0, the wrong value of the hook. Then set a to 8'b1111_0000, which is 15.0, and b to 2.0. The true product 30 does not fit Q4.4, and kept shows 14.0.
a = 00101000 value 2.5000 b = 00011000 value 1.5000 product = 0000001111000000 value 3.7500 hex 03c0 kept = 00111100 value 3.7500 hex 3cCheck Yourself
Common Mistakes
Wrong Keep the low eight bits of a Q4.4 product.
The product is Q8.8, so its low byte holds only fraction bits and the integer part is lost.
Fix Keep the bits around the binary point: product[11:4] is Q4.4 again.
kept = product[7:0]; // 12.0kept = product[11:4]; // 3.75Wrong Add a Q4.4 number to a Q6.2 number as they are.
The adder adds bit 0 to bit 0, but those bits have different weights.
Fix Shift one operand until both have the same number of fraction bits.
sum = a + b;sum = a + (b << 2);Wrong Expect the hardware to know where the binary point is.
The wires carry an integer; the point exists only in the design notes and the signal names.
Fix Write the format beside every signal, as in // Q4.4, and check it at each operation.
Wrong Truncate after every step of a long calculation.
Each truncation errs downward, so the errors add up in one direction.
Fix Round where the error matters, and use round half to even where ties are common.
Remember
- Qm.n stores the value times 2^n as an integer; the binary point lives in the design notes only.
- Shift operands until their fraction bits match before you add them.
- A product has the fraction bits of both operands added; keep the bits around the point.
- Truncation always errs down. Round half up adds half a step first. Round half to even also sends each tie to the even value.
- Check the bits above the kept field: if they are not empty, the result overflowed.