C12 · Lesson 18 · Fixed-point arithmetic
Fractions on a processor with no floating point
This board's processor, like most small microcontrollers, has no floating-point hardware. A C compiler can still do float arithmetic in software, hundreds of instructions per operation, and many embedded compilers and code reviews forbid it for that reason (this course's compiler doesn't offer it at all). The alternative is fixed point: keep every quantity as an integer in small units, millivolts or tenths of a degree, or as an integer scaled by a power of two, and do everything with integer multiplies, divides and shifts. It's fast and exact to a known resolution, as long as you watch two things: rounding, and overflow.
- The program reads the thermistor and prints three integers: the raw code, the voltage in millivolts, and the temperature in tenths of a degree, all without floating point.
- The temperature comes from a table of codes at 0, 10, ..., 80 °C, with straight-line interpolation between entries. Move the temperature slider and compare the printed tenths with it.
- At 5 °C the table gives 48 (4.8 °C): straight lines between points 10 °C apart miss the curve slightly. A table every 5 °C would halve that error, or better.
Predict
In C, int mv = code * 3300 / 4095; for code = 2048. Does it give the exact answer, 1650.4, rounded or truncated?
Integer division truncates toward zero: becomes 1650, and 1650.9 would also become 1650. Adding half the divisor first, (code * 3300 + 2047) / 4095, rounds to the nearest instead. The order matters too: code * (3300 / 4095) would compute first.
Name it
Scaled integers
Pick a unit small enough for the resolution you need and store whole numbers of it: millivolts, tenths of a degree, microseconds. Then
- multiply before dividing, to keep precision:
x * 3300 / 4095, neverx * (3300 / 4095); - round a division by adding half the divisor:
(a + b/2) / bfor positive values; - check the largest intermediate value fits: here , well under .
Binary fixed point (Q format)
A Q16.16 number stores in 32 bits: 16 bits of integer and 16 of fraction, resolution . Adding and subtracting work directly. Multiplying two Q16.16 values gives a Q32.32 product, which is shifted back right by 16 (and needs 64 bits in between unless the values are small). Shifts are cheap, so binary scaling is common in signal processing; decimal scaling is common for quantities people read.
Tables and interpolation
Functions like logarithms and the thermistor's curve are expensive to compute, so firmware stores a table of values and interpolates linearly between neighbors:
The error depends on the table's spacing and the curve's bend; halving the spacing cuts it about fourfold.
Worked example
The ADC reads 2561. What temperature does the play's table give?
Show the solution
- Find the table entries around it. 2561 lies between 2270 (30 °C) and 2676 (40 °C).
- Interpolate in tenths of a degree. The division is an integer one, so it drops the fraction: so 37.1 °C.
- Compare with the truth. The true temperature for that code is 37.0 °C.
Answer the table gives 37.1 °C.
Worked example
Multiply 1.5 by 2.25 in Q16.16.
Show the solution
- Scale each number by .
- Multiply. The product needs more than 32 bits:
- Shift right by 16.
Answer 3.375, exactly.
Worked example
Store −1.75 in Q8.8 (16 bits, 8 fraction bits).
Show the solution
- Scale by 256.
- Write it in 16-bit two's complement.
- Read it back. Shifting right by 8 with sign gives −2 (rounded toward −∞), and the low byte 0x40 is the fraction:
Answer −1.75 is stored as 0xFE40.
Check
What integer represents 1.25 in a Q format with 8 fraction bits?
Check
In Q format with 16 fraction bits, 0.75 and 2 are stored as integers. What's their product, shifted back right by 16, as an integer?
Check
The ADC reads 1484. What does (c * 3300 + 2047) / 4095 give, in integer arithmetic?
Write units the way you normally would: 1.5 mV, 1500 µV and 0.0015 V are the same answer.
Check
Interpolate in integers: the table says 1357 → 100 and 2676 → 400. What does 2000 give, truncated?
Check
A computation multiplies a 12-bit code (up to 4095) by 100000 before dividing. What's the largest intermediate value? (A signed 32-bit int holds up to about 2.1 × 10⁹.)
Check
Which line computes millivolts from a 12-bit code most accurately in integer C?
Check
Read the potentiometer on ADC0. Print the raw code, then the voltage in millivolts rounded to the nearest whole millivolt, using only integer arithmetic.
C on the course's RISC-V board (10 MHz, no floating point). #include "board.h" for the registers; print(x) shows a number. Tab indents; press Esc and then Tab to move on.
Check
Write int tenths(int code) using the table from the play (codes at 0, 10, ... 80 °C), interpolating in integers. Print tenths(1500), then tenths(X) for X = 2450.
C on the course's RISC-V board (10 MHz, no floating point). #include "board.h" for the registers; print(x) shows a number. Tab indents; press Esc and then Tab to move on.
Check
−0.75 and 2.25 are multiplied in Q16.16.
(a)
What integer stores −0.75?
(b)
What integer does the product, shifted right by 16, give?
Check
A Q-format number has 8 fraction bits, in 16 bits.
(a)
What unsigned 16-bit pattern stores −0.5?
(b)
What is the largest magnitude it can hold, about?
Explain why
Why multiply before dividing in fixed-point arithmetic, and what's the risk?
Integer division discards the fraction, so dividing first throws away precision that later steps can never recover: code / 4095 * 3300 is 0 for any code below 4095, and even a better ordering loses up to one unit at each division. Multiplying first keeps all the significant digits until the single division at the end, so only one rounding error happens. The risk is overflow: the intermediate product is much larger than the result, so it must fit the integer type, which means checking the largest possible inputs (4095 × 3300 is fine in 32 bits; 4095 × 10⁶ is not) and, if needed, using a wider type or scaling down first.
Practice
Keep practicing
12 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.