Computing Library › Number Systems & Information
Number Systems & Information

Fixed-Point Representation

Fixed-point numbers store fractions by placing an implied radix point at a fixed bit position.

The concept

A fixed-point number is just an integer with an agreed, unchanging position for the binary point. If the low 8 bits are fractional, the stored integer is the real value times 2⁸. This is often written as a Q format, such as Q8.8.

Reading a value

Kronos motion — operating point

To recover the real value, divide the stored integer by the scale factor 2ᶠ, where f is the number of fractional bits. Bits above the point carry positive weights; bits below carry negative powers of two.

Arithmetic

Addition and subtraction of fixed-point values with the same format use ordinary integer operations. Multiplication doubles the fractional bits, so the product must be shifted right by f to restore the format.

Trade-offs versus floating point

Fixed point gives uniform precision across its whole range and uses only integer hardware, which is fast and deterministic. Its drawback is limited dynamic range: very large and very small magnitudes cannot both be represented well.

Where it is used

python
SCALE = 1 << 8   # Q8.8
def to_fixed(x): return int(round(x * SCALE))
def mul(a, b):   return (a * b) >> 8
print(mul(to_fixed(1.5), to_fixed(2.0)) / SCALE)  # 3.0