C1218 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

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?

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, never x * (3300 / 4095);
  • round a division by adding half the divisor: (a + b/2) / b for positive values;
  • check the largest intermediate value fits: here 4095×3300=13.5×106, well under 231=2.1×109.

Binary fixed point (Q format)

A Q16.16 number stores x×216 in 32 bits: 16 bits of integer and 16 of fraction, resolution 2−16≈1.5×10−5. 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:

y=yi+(x−xi)yi+1−yixi+1−xi

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
  1. Find the table entries around it. 2561 lies between 2270 (30 °C) and 2676 (40 °C).
  2. Interpolate in tenths of a degree. The division is an integer one, so it drops the fraction: T=300+(2561−2270)×1002676−2270=300+29100406=300+71=371 so 37.1 °C.
  3. 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
  1. Scale each number by 216. 1.5×216=983042.25×216=147456
  2. Multiply. The product needs more than 32 bits: 98304×147456=1.4496×1010
  3. Shift right by 16. 221184=3.375×216

Answer 3.375, exactly.

Worked example

Store −1.75 in Q8.8 (16 bits, 8 fraction bits).

Show the solution
  1. Scale by 256. −1.75×256=−448
  2. Write it in 16-bit two's complement. 65536−448=65088=0xFE40
  3. Read it back. Shifting right by 8 with sign gives −2 (rounded toward −∞), and the low byte 0x40 is the fraction: −2+64256=−1.75

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?

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.