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.

A Byte With a Binary Point
23
22
21
20
2-1
2-2
2-3
2-4

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

  1. 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.
  2. The smallest step. Flipping the LSB adds 1/16, or 0.0625. No value between two steps can be stored.
  3. 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.

FormatTotal bitsStepLargest unsigned valueThe byte 00101000 reads as
Q8.08125540
Q6.281/463.7510.0
Q4.481/1615.93752.5
Q2.681/643.9843750.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.

Which Bits of the Product to KeepArrows show data. The 16-bit product has its binary point between bit 8 and bit 7.
  • I/O
  • Datapath
  • Neutral
  • Data
Which bits of the product to keepArrows show data. The 16-bit product has its binary point between bit 8 and bit 7. io a = 2.5 Q4.4, stored 40 io b = 1.5 Q4.4, stored 24 datapath Multiplier 40 x 24 = 960 neutral bits 15 to 12 integer bits that donot fit neutral bits 11 to 4 Q4.4: 3.75 neutral bits 3 to 0 extra fraction bits operandoperandmust be 0the resultrounded away
  1. io a = 2.5Q4.4, stored 40
    • operand → Multiplier
  2. io b = 1.5Q4.4, stored 24
    • operand → Multiplier
  3. datapath Multiplier40 x 24 = 960
    • must be 0 → bits 15 to 12
    • the result → bits 11 to 4
    • rounded away → bits 3 to 0
  4. neutral bits 11 to 4Q4.4: 3.75
  5. neutral bits 15 to 12integer bits that do not fit
  6. neutral bits 3 to 0extra fraction bits
  1. 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.

  2. 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.

  3. 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.

  4. 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.

Quarters Rounded to Whole NumbersEach column is one input value, in steps of a quarter. The three rows below it show what each rule keeps.
0123456789101112131415value00.250.50.7511.251.51.7522.252.52.7533.253.53.75truncate0123half up01234half to even01234tietietietie
Scroll horizontally ↔
  1. 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.

  2. Cycle 3

    0.75 is not a tie. Both rounding rules give 1. Truncation still gives 0.

  3. Cycle 6

    1.5 is a tie between 1 and 2. Both rounding rules give 2: here the even neighbour is the upper one.

  4. 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.

RuleHow it is builtLargest error of one valueSum of the errors over these 16 values
Truncatedrop the bits0.75, always down-6
Round half upadd half a step, then drop the bits0.5+2
Round half to evenround half up, then clear the LSB at a tie0.50

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
  1. 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.

  2. 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.

  3. 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.

RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: a = 00101000 value 2.5000 b = 00011000 value 1.5000 product = 0000001111000000 value 3.7500 hex 03c0 kept = 00111100 value 3.7500 hex 3c

Check Yourself

Predict the outputThe program rounds 2.5 to a whole number in two ways and prints both. What exact line does it print?
module tb;
  logic [5:0] x;
  logic [3:0] up, even;
  initial begin
    x = 6'b0010_10;            // 2.5 in Q4.2
    up = (x + 6'd2) >> 2;
    even = (x[1:0] == 2'b10) ? (up & 4'b1110) : up;
    $display("%0d %0d", up, even);
  end
endmodule
Spot the bugEach option takes a Q4.4 result out of the 16-bit product of 2.5 and 1.5. The bench wants 3c, which is 3.75. Which option fails?
ConceptA Q4.4 number is multiplied by a Q6.2 number. How many fraction bits does the full product have?
ConceptThe byte 00101000 is 2.5 in Q4.4. What is the same byte in Q2.6?
ConceptA filter truncates after each of 1,000 multiplications. What does that do to its output?

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.

Before
kept = product[7:0];    // 12.0
After
kept = product[11:4];   // 3.75

Wrong 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.

Before
sum = a + b;
After
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.

Practice

Report a problem with this page