See How Things Work

Fixed-point numbers

Unit
Representing Information
Part
2. Numbers in a Computer
Lesson
2.4

A fixed-point number is an integer kept in a fixed number of bits and read with an agreed number of those bits after a binary point. This lesson extends binary numerals past the point, defines the value, range and step of a fixed-point format and the Q notation for it, rounds numbers to the nearest value a format can hold, and adds and multiplies fixed-point numbers, with the rescaling that a product needs.

Binary digits after the point

The integers of lessons 2.1–2.3 have no fractional part. A decimal numeral extends to the right of its point with tenths, hundredths and thousandths, each place worth one tenth of the place to its left (Decimals: decimal place value). A binary numeral extends in the same way, with 2 in place of 10: the places right of the point are worth 12, 14, 18, …, each half of the place to its left.⁠[1] The point is then called the binary point.⁠[2]

Definition (Binary numeral with a fractional part).

A binary numeral b₍ₖ₎ … b₁b₀ . c₁c₂ … c₍f₎ has k + 1 digits before the binary point and f digits after it. Its value is

b₍ₖ₎ × 2ᵏ + ⋯ + b₁ × 2 + b₀ + c₁ × 12 + c₂ × 14 + ⋯ + c₍f₎ × 12ᶠ.

414200111½00¼1¼⅛1⅛.4 + 1 + ¼ + ⅛ = 5.375 = 43/8without the point, 101011 names 43
Figure. The binary numeral 101.011₂. The places left of the point are worth 4, 2 and 1, those right of it ½, ¼ and ⅛. The 1s are worth 4 + 1 + ¼ + ⅛ = 5.375. Without the point, the same digits 101011 name 43, and 43/8 = 5.375: Theorem 1.
Theorem 1 (the value is an integer over a power of two).

A binary numeral with f digits after the point has the value N2ᶠ, where N is the whole number named by the same digits with the point removed.

Proof.

The digit cⱼ in place j after the point is worth 12ʲ = 2ᶠ⁻ʲ2ᶠ (equivalent fractions), and in the numeral without the point the same digit stands in position f − j, worth 2ᶠ⁻ʲ. The digit bᵢ before the point is worth 2ⁱ = 2ⁱ⁺ᶠ2ᶠ, since 2ⁱ⁺ᶠ = 2ⁱ × 2ᶠ (Theorem 2 of lesson 1.5), and without the point it stands in position i + f, worth 2ⁱ⁺ᶠ. So every digit contributes its value in N, divided by 2ᶠ, and the sum of the contributions is N2ᶠ.

In particular, moving the binary point f places to the left divides a number by 2ᶠ, and moving it f places to the right multiplies the number by 2ᶠ. This fact carries the whole lesson, and the next one.

Example (Reading a binary fraction).

What is the value of 110.0101₂?

  1. With the places: 4 + 2 + 14 + 116 = 6 + 0.25 + 0.0625 = 6.3125.
  2. With Theorem 1: there are 4 digits after the point, and 1100101₂ = 64 + 32 + 4 + 1 = 101, so the value is 10116 = 6.3125.

Answer: 6.3125, by both routes.

Example (Writing a decimal in binary).

Write 6.625 as a binary numeral.

  1. 6.625 = 6 + 58, so three digits after the point suffice: 6.625 × 8 = 53.
  2. 53 = 32 + 16 + 4 + 1 = 110101₂ (Number Bases: binary numbers).
  3. By Theorem 1, moving the point three places left divides by 8: 6.625 = 110.101₂.

Answer: 110.101₂. Check: 4 + 2 + 12 + 18 = 6.625.

Not every fraction has such a numeral. The next theorem says exactly which ones do.

Theorem 2 (which numbers have finite binary numerals).

Let ab be a positive fraction in lowest terms. It has a binary numeral with finitely many digits after the point exactly when b is a power of 2: b = 1, 2, 4, 8, …

Proof.

If b = 2ᶠ, write a in binary and place the point f digits from the right, writing zeros on the left if a has fewer than f + 1 digits; by Theorem 1 the numeral names a2ᶠ. Conversely, if ab has a numeral with f digits after the point, then ab = N2ᶠ by Theorem 1, so a × 2ᶠ = N × b and b divides a × 2ᶠ. Since a and b have no common prime factor (lowest terms), every prime factor of b divides 2ᶠ, so it is 2 (prime factorization is unique). So b is a power of 2.

The decimal rule is similar: a fraction in lowest terms is a terminating decimal exactly when its denominator is a product of 2s and 5s (Decimals: terminating decimals). Every power of 2 is such a product, so every finite binary fraction is also a terminating decimal: 12ᶠ = 5ᶠ10ᶠ. The converse fails. 0.1 = 110 and 0.3 = 310 have the denominator 10 = 2 × 5, which is not a power of 2, so they have no finite binary numeral. As the Python documentation puts it, most decimal fractions cannot be represented exactly as binary fractions.⁠[3]

Example (Which of these have finite binary numerals?).

Decide for 0.375, 512, 0.35 and 0.75.

  1. 0.375 = 38, and 8 = 2³: yes, 0.011₂.
  2. 512 is in lowest terms and 12 = 4 × 3 has the factor 3: no.
  3. 0.35 = 720, and 20 = 4 × 5 has the factor 5: no, although 0.35 is a terminating decimal.
  4. 0.75 = 34: yes, 0.11₂.

Answer: 0.375 and 0.75 only. Checking the denominator in lowest terms settles it without any division.

Fixed-point formats

A computer stores n bits and no point. To hold fractions, a program agrees where the point is. Early computers fixed it in the design of the machine, with the decimal point or binary point in a fixed position.⁠[2] Today the agreement is part of the format: the position of the radix point is defined outside the representation, so it cannot be read off the bits.⁠[4]

Definition (Fixed-point format).

An n-bit fixed-point format with f fraction bits (0 ≤ f ≤ n) reads an n-bit pattern as an integer k, either unsigned (lesson 2.1) or in two’s complement (lesson 2.2), and gives it the value

k2ᶠ.

By Theorem 1 this is the pattern read as a binary numeral with its point before the last f bits. The integer k is the stored integer; 2ᶠ is the scale factor.

Since the stored integers are ordinary integers, fixed-point hardware uses the same mechanisms and representations as integer hardware, and the C language extension for fixed-point types requires two’s complement for them.⁠[5]

Convention.

There are two common ways to name a format. The ISO extension of the C language writes an unsigned format with x integer bits and y fraction bits as x.y, and puts an s before a signed format for its sign bit, as in s4.15.⁠[6] The Q notation of signal-processing libraries names signed formats: Qm.n has m integer bits and n fraction bits, plus one sign bit, so it needs m + n + 1 bits.⁠[7] The 16-bit format Q0.15 is also called Q15.⁠[8] This lesson writes unsigned formats as unsigned x.y and signed ones as Qm.n.

Caution.

Documents do not agree on whether the sign bit counts as an integer bit. Texas Instruments’ C64x library manual calls its 16-bit format Q0.15, with no integer bits beside the sign bit,⁠[8] while the same company’s MSP library lists the same format, Q15, with 1 integer bit and 15 fraction bits.⁠[9] The OpenType font format writes its 16-bit signed type with 14 fraction bits as 2.14,⁠[10] which is Q1.14 in the notation of this lesson, and its 32-bit signed type with 16 fraction bits as 16.16,⁠[11] which is Q15.16. The number of fraction bits and the total width are what matter; check both before reading data.

Bits after the pointThe pattern read with its pointValue
00100 1100.76/1 = 76
40100.110076/16 = 4.75
70.1001 10076/128 = 0.59375
Table. The byte 01001100 read as an unsigned fixed-point number with 0, 4 and 7 bits after the point. The stored integer is 76 every time; only the agreed scale factor changes the value.
Example (Reading an unsigned 4.4 byte).

Which value does the byte 1011 0110 hold in the unsigned 4.4 format?

  1. As an unsigned integer, 10110110 = 128 + 32 + 16 + 4 + 2 = 182.
  2. With 4 fraction bits the value is 18216 = 11.375.

Answer: 11.375. Check: before the point, 1011 = 11; after it, .0110 = 14 + 18 = 0.375.

Example (A negative Q3.4 number).

Which value does the byte 1110 1100 hold in Q3.4, 8 bits in two’s complement with 4 fraction bits?

  1. As an unsigned integer the byte is 236, and its sign bit is 1, so the stored integer is 236 − 256 = −20 (lesson 2.2, Theorem 1).
  2. The value is -2016 = −1.25.

Answer: −1.25. Check with place values, the sign bit counting −8: −8 + 4 + 2 + 12 + 14 = −1.25.

Range and step

Theorem 3 (the values of a fixed-point format).
  1. An unsigned n-bit format with f fraction bits holds exactly the numbers k2ᶠ for k = 0, 1, …, 2ⁿ − 1: from 0 to 2ⁿ − 12ᶠ.
  2. A signed (two’s complement) n-bit format with f fraction bits holds exactly the numbers k2ᶠ for k = −2ⁿ⁻¹, …, 2ⁿ⁻¹ − 1: from −2ⁿ⁻¹2ᶠ to 2ⁿ⁻¹ − 12ᶠ.
  3. In both, neighboring values differ by exactly 12ᶠ, everywhere in the range.
Proof.

(1) and (2): the stored integers are exactly those of lesson 2.1, Proposition 1, and of lesson 2.2, Theorem 1, each with one pattern, and dividing by 2ᶠ keeps them in order. (3): neighboring stored integers k and k + 1 give values that differ by k + 12ᶠ − k2ᶠ = 12ᶠ.

Definition (Step).

The difference 12ᶠ between neighboring values of a fixed-point format is its step. Texas Instruments’ manuals call it the format’s finest fractional resolution.⁠[12]

The sources give the same numbers. Q15 holds −1 up to 32,76732,768 = 0.999969482421875 in steps of 132,768, about 3.05 × 10⁻⁵; Texas Instruments gives its approximate range as (−1, 1) and its finest resolution as 2⁻¹⁵ ≈ 3.05 × 10⁻⁵, and lists its maximum as 0.9999695.⁠[12][9] For Q3.12 the same manual gives the range (−8, 8) and the resolution 2⁻¹², about 2.441 × 10⁻⁴.⁠[13] Formats whose bits all lie after the point hold values from 0 to 1 when unsigned and from −1 to 1 when signed,⁠[14] never 1 itself, as Theorem 3 shows.

FormatFrom … toStep
unsigned 4.48 bits, 4 after the point0 … 15.93751/16
Q3.48 bits, 4 after the point, signed−8 … 7.93751/16
Q15 = Q0.1516 bits, 15 after the point, signed−1 … 1 − 1/32,7681/32,768
Q3.1216 bits, 12 after the point, signed−8 … 8 − 1/4,0961/4,096
Q1.1416 bits, 14 after the point, signedOpenType “2.14”−2 … 2 − 1/16,3841/16,384
Q15.1632 bits, 16 after the point, signedOpenType “16.16”−32,768 … 32,768 − 1/65,5361/65,536
Table. Some fixed-point formats, with the smallest and largest values and the step given by Theorem 3, computed exactly. Q15 and Q3.12 are formats of Texas Instruments’ libraries;⁠[8][13] 2.14 and 16.16 are OpenType’s names for the last two.⁠[10][11]

The width n is fixed, so moving one bit from before the point to after it halves the step and also halves the largest value. A format can be fine or wide, not both: the step is the same small amount near 0 and near the largest value.

unsigned 3.1: 0 to 7.5, step 1/2→unsigned 2.2: 0 to 3.75, step 1/4unsigned 1.3: 0 to 1.875, step 1/801234
Figure. Three unsigned 4-bit formats drawn on the same scale from 0 to 4. Each holds 16 values. With 1 bit after the point they reach 7.5 in steps of ½ (only the values up to 4 are drawn); with 2 bits, 3.75 in steps of ¼; with 3 bits, 1.875 in steps of ⅛.
Example (The edge cases: the ends of Q3.4).

Which bytes hold the largest and smallest values of Q3.4, and what are those values?

  1. The largest stored integer is 2⁷ − 1 = 127 = 0111 1111, so the largest value is 12716 = 7.9375.
  2. The smallest is −2⁷ = −128 = 1000 0000, so the smallest value is -12816 = −8.

Answer: 0111.1111 = 7.9375 and 1000.0000 = −8. The format reaches −8 but not +8, the asymmetry of two’s complement (lesson 2.2) in a new scale.

Rounding to the nearest value

Most numbers lie between two values of a format. Multiply x by 2ᶠ: if x × 2ᶠ is a whole number k, then x = k2ᶠ is stored exactly. Otherwise x × 2ᶠ lies between two neighboring integers k and k + 1, and x lies between the two values k2ᶠ and k + 12ᶠ, one step apart.

Definition (Rounding to the nearest value).

To round x to a fixed-point format is to replace it by a value of the format closest to x: compute x × 2ᶠ and round it to the nearest integer k (Decimals: rounding); the result is k2ᶠ. Simply dropping the bits after the f-th, called truncation, takes the value just below x (or x itself) instead.

Proposition 4 (the rounding error).

If x lies in the range of a format with f fraction bits, rounding to the nearest value changes x by at most half a step, 12ᶠ⁺¹. Truncation changes it by less than one step, 12ᶠ.

Proof.

x lies in an interval from k2ᶠ to k + 12ᶠ of length 12ᶠ. The nearer end is at most half the length away. Truncation always takes the lower end, which is less than the whole length away.

A value exactly halfway between two neighbors needs a rule; the Decimals unit rounds it up, and the next lessons meet the rule that floating point uses. Real systems also differ in whether they round to the nearest value at all. The C extension for fixed point requires only that a value that cannot be represented be rounded to the closest value above it or the closest value below it,⁠[15] leaving the choice to each implementation, so that the result is within one step, called there 1 unit in the last place.⁠[16]

Example (Rounding 2.7 into unsigned 4.4).

Store 2.7 in the unsigned 4.4 format.

  1. 2.7 × 16 = 43.2, which lies between 43 and 44; 43 is nearer.
  2. 43 = 0010 1011, so the byte is 0010.1011, with the value 4316 = 2.6875.

Answer: 0010 1011, which holds 2.6875. The error is 0.0125, within half a step, 132 = 0.03125 (Proposition 4). By Theorem 2, no number of fraction bits would store 2.7 exactly, since 2.7 = 2710.

Example (Rounding a negative number into Q3.4).

Store −0.3 in Q3.4.

  1. −0.3 × 16 = −4.8, which lies between −5 and −4; −5 is nearer.
  2. The 8-bit two’s-complement pattern of −5 is that of 256 − 5 = 251 = 1111 1011.

Answer: 1111.1011, which holds -516 = −0.3125, an error of 0.0125. Here truncation gives the same byte: in two’s complement, dropping bits always moves the stored integer down, from −4.8 to −5, never toward 0. Cutting the decimal −4.8 to −4 would be a different operation, and it would give −0.25, an error of 0.05.

Example (A common mistake: truncating instead of rounding).

Someone stores 1.99 in unsigned 4.4 by keeping only the first four bits after the point. What do they get, and what is the nearest value?

  1. 1.99 × 16 = 31.84. Truncation keeps 31 = 0001 1111: the value 1.9375, an error of 0.0525.
  2. Rounding takes 32, the nearer integer: 0010 0000, the value 2, an error of only 0.01.

Answer: truncation gives 1.9375; the nearest value is 2.0. Rounding up here carries all the way into the part before the point, as a decimal 1.99 rounds to 2.0, and the truncation error is more than five times the rounding error.

Example (The same error, very different importance).

Store 0.01 and 200.01 in the unsigned 8.8 format (16 bits, 8 after the point). Compare the errors.

  1. 0.01 × 256 = 2.56, rounded to 3: the value 3256 = 0.01171875, an error of 0.00171875.
  2. 200.01 × 256 = 51,202.56, rounded to 51,203: the value 200.01171875, the same error 0.00171875.
  3. Relative to the numbers, the first error is about 17% of 0.01, the second about 0.0009% of 200.01.

Answer: equal errors, but 0.01 keeps almost none of its precision. A fixed step suits numbers of similar size; a small number shares the scale of the largest one and loses significant digits. Campbell’s 1962 account of the Stretch computer names exactly this weakness: a common scale factor leaves a small number with leading zeros and fewer significant digits.⁠[17] The next lesson’s floating-point numbers move the point to fit each number.

A number outside the range cannot be rounded into it: the conversion overflows. The C extension names the two usual remedies, saturation and modular wraparound, and requires saturation, which replaces the number by the closest value of the format, its largest or smallest.⁠[18] Wraparound keeps the low bits, as in lesson 2.3. For fixed-point data, saturation is often preferred and often supported by the hardware itself.⁠[19]

Game

Nearest fixed-point value

Round
1
Score
0
Streak
0

Store x = −1.727 in the Q2.5 format: 8 bits, 5 of them after the binary point, read in two’s complement. Set the bits of the value nearest to x.

The format holds the values from −4 to 3.96875 in steps of 1/32. The numbers above the bits are their place values; the leftmost, the sign bit, counts negative.

−4
2
1
½
¼
⅛
1/16
1/32

Your bits store 0, so their value is 0/32 = 0, at a distance of 1.727 from −1.727.

Adding and multiplying

Theorem 5 (addition).

Two numbers k2ᶠ and j2ᶠ of the same format add to k + j2ᶠ. So the integer adder of lesson 2.1 adds fixed-point numbers unchanged: add the stored integers. The sum is exact whenever it is in range, and it overflows exactly as integers do (lesson 2.3).

Proof.

Fractions with the same denominator add by adding their numerators; the denominator 2ᶠ stays.

Numbers with different numbers of fraction bits must first be brought to the same scale. Adding g − f zeros at the end of a pattern with f fraction bits gives the same value with g fraction bits, since k2ᶠ = k × 2ᵍ⁻ᶠ2ᵍ; going the other way drops bits and may need rounding.

Example (Adding in unsigned 4.4).

Add 2.6875 = 0010.1011 and 1.5 = 0001.1000 in unsigned 4.4.

  1. The stored integers are 43 and 24, and 43 + 24 = 67.
  2. In columns: 00101011 + 00011000 = 01000011, with no carry out.

Answer: 0100.0011 = 6716 = 4.1875, and indeed 2.6875 + 1.5 = 4.1875, exactly.

Example (Adding numbers of two formats).

Add x = 1.5 in Q3.4 and y = 0.40625 in Q1.6.

  1. x has the stored integer 24 with 4 fraction bits; y has 26 with 6 fraction bits.
  2. Bring x to 6 fraction bits by writing two zeros after it: 24 × 4 = 96, and 9664 = 1.5.
  3. Add: 96 + 26 = 122, so the sum is 12264 = 1.90625, which Q1.6 holds (its largest value is 1.984375).

Answer: 1.90625, exactly. Bringing y down to 4 fraction bits instead would have needed 264 = 6.5, not a whole number, and so a rounding.

Theorem 6 (multiplication).

(1) k2ᶠ × j2ᶠ = kj2²ᶠ: the integer product kj, read with 2f fraction bits, is the exact product. (2) To return it to f fraction bits, divide kj by 2ᶠ, that is, drop its last f bits, rounding as in Proposition 4. (3) The product of two n-bit unsigned integers always fits in 2n bits.

Proof.

(1) Fractions multiply numerator by numerator and denominator by denominator (multiplying fractions), and 2ᶠ × 2ᶠ = 2²ᶠ. (2) kj2²ᶠ = kj ÷ 2ᶠ2ᶠ, so the stored integer with f fraction bits is kj ÷ 2ᶠ, rounded when it is not whole; by Theorem 3 of lesson 2.1, dividing by 2ᶠ keeps the bits left of the last f. (3) k, j ≤ 2ⁿ − 1, so kj ≤ (2ⁿ − 1)² = 2²ⁿ − 2 × 2ⁿ + 1 < 2²ⁿ.

So a fixed-point multiplication is an integer multiplication into double width followed by a shift. The C extension describes exactly this for its 32-bit format with 31 fraction bits: a 32 × 32 → 64-bit integer multiplication followed by a shift right by 31 bits.⁠[20]

a = 0010.1011 = 2.6875, stored integer 43b = 0001.0101 = 1.3125, stored integer 2143 × 21 = 903, with 8 bits after the point:0000001110000111.kept: 0011.1000droppedexact product 903/256 = 3.527343750111 is less than 1000, half a step: round down to 3.5
Figure. 2.6875 × 1.3125 in unsigned 4.4. The stored integers 43 and 21 multiply to 903, a 16-bit integer with 8 bits after the point: the exact product 3.52734375. Keeping 4 bits after the point keeps the green bits; the dropped bits 0111 are less than half a step (1000), so the result rounds down to 0011.1000 = 3.5.
Example (A product that is exact).

Multiply 2.5 by 1.75 in unsigned 4.4.

  1. The stored integers are 40 and 28, and 40 × 28 = 1,120. With 8 fraction bits the product is 1,120256 = 4.375.
  2. Divide by 16 to return to 4 fraction bits: 1,120 ÷ 16 = 70, a whole number, so nothing is lost.

Answer: 7016 = 4.375 = 0100.0110, exactly 2.5 × 1.75.

Example (A common mistake: forgetting to rescale).

In the figure’s product, someone keeps the last 8 bits of 903 = 0000 0011 1000 0111 and reads them in unsigned 4.4. What do they get?

  1. The last 8 bits are 1000 0111 = 135, read as 13516 = 8.4375.
  2. But those bits are the product’s places from 12 down to 1256: read with their real scale they are only 0.52734375, the fractional part.

Answer: 8.4375, far from the true 3.52734375. The bits of a product must be taken at the right scale: after a multiplication the point has moved f places, and it must be moved back.

Example (The edge case: −1 × −1 in Q15).

Multiply −1 by −1 in Q15.

  1. −1 is the stored integer −32,768 = −2¹⁵, the smallest of Q15.
  2. The integer product is (−2¹⁵) × (−2¹⁵) = 2³⁰, with 30 fraction bits: exactly 1.
  3. Returning to 15 fraction bits gives the stored integer 2³⁰ ÷ 2¹⁵ = 2¹⁵ = 32,768, one more than the largest, 32,767.

Answer: the product 1 overflows Q15, whose values stop at 0.999969482421875. It is the only product of two Q15 numbers that does not fit, the same exception as negating the smallest integer in lesson 2.2; saturation stores the largest value instead.

Game

Multiply and rescale

Round
1
Score
0
Streak
0

Multiply 2.15625 by 1.125 in the unsigned 3.5 format. The integer product is shown with its binary point. Set the 8 bits of the product in unsigned 3.5, rounded to the nearest value.

a = 2.15625, stored integer 69

b = 1.125, stored integer 36

integer product 69 × 36 = 2,484: 16 bits, 10 after the point

00 0010.0110 1101 00

The product in unsigned 3.5:

4
2
1
½
¼
⅛
1/16
1/32

Your bits store 0, the value 0.

Looking ahead.

Division works the same way in reverse: k2ᶠ ÷ j2ᶠ = kj, so to keep f fraction bits the stored integer is k × 2ᶠ ÷ j, rounded (Exercise 7). How processors multiply and divide integers in circuits is part of the unit Digital Logic & Computer Hardware.

Where fixed point is used

Fixed point needs nothing but integer arithmetic, and that decides where it is used.

  • Signal processing. Embedded processors often analyze signals by applying filtering algorithms to the data they receive; the usual data type of those filters is fixed point, and such processors are often equipped with special hardware for it.⁠[21] Texas Instruments’ signal-processing library for its C64x processors, for example, uses Q15 unless a function says otherwise.⁠[8]
  • Processors without floating point. Fixed-point types are often used where floating-point types would otherwise be, typically because the hardware does not support floating-point operations directly.⁠[22]
  • File formats. OpenType fonts store numbers in the 32-bit 16.16 and 16-bit 2.14 formats;⁠[11][10] a 2.14 number is a signed two’s-complement integer part and an unsigned fraction, added together.⁠[23]
Example (Reading an OpenType 2.14 number).

An OpenType file holds the 16-bit 2.14 number 0xE000. What is its value?

  1. 0xE000 = 1110 0000 0000 0000. In 2.14 the point comes after the first two bits: 11.10 0000 0000 0000.
  2. The integer part 11 in two’s complement is −2 + 1 = −1; the fraction .1000… is 12.
  3. Added: −1 + 12 = −0.5. Check with Theorem 1: 0xE000 = 57,344, the stored integer is 57,344 − 65,536 = −8,192, and −8,19216,384 = −0.5.

Answer: −0.5.

Historical note.

Most early computers designed for scientific computation used fixed-point arithmetic.⁠[24] The programmer had to keep track of the scale factors, which could exist only in the programmer’s mind, taken into account by hand.⁠[25] To avoid the difficulties of limited range and of scaling, G. R. Stibitz proposed in the early 1940s an automatic scaling procedure, called floating-point arithmetic:⁠[26] the subject of the next lesson.

Looking ahead.

Storing the numbers of a neural network in a few bits with a shared scale, to make it smaller and faster, is taken up in the lesson “Quantization” of the unit Efficient Inference.

Exercises

Exercise .

Find the values of (a) 0.1101₂ and (b) 11.0011₂, both with the places and with Theorem 1.

▸ Show solution

(a) 0.1101₂ = 12 + 14 + 116 = 0.8125; by Theorem 1, 1101₂ = 13 and 1316 = 0.8125.

(b) 11.0011₂ = 3 + 18 + 116 = 3.1875; by Theorem 1, 110011₂ = 51 and 5116 = 3.1875.

Exercise .

Write 9.8125 in unsigned 4.4. Which of 0.3, 0.375 and 0.6 can a fixed-point format hold exactly?

▸ Show solution

9.8125 × 16 = 157 = 1001 1101, so 9.8125 = 1001.1101 exactly.

0.375 = 38 is held exactly (0000.0110). 0.3 = 310 and 0.6 = 35 have denominators that are not powers of 2, so by Theorem 2 no format holds them exactly.

Exercise .

Give the range and the step of Q7.8 and of unsigned 6.10.

▸ Show solution

Q7.8 has 16 bits and 8 fraction bits: from −32,768256 = −128 to 32,767256 = 127.99609375, in steps of 1256 = 0.00390625.

Unsigned 6.10: from 0 to 65,5351,024 = 63.9990234375, in steps of 11,024.

Exercise .

Round 3.14159 into Q3.12. Give the stored integer, the value, the error, and the pattern in hexadecimal.

▸ Show solution

3.14159 × 4,096 = 12,867.95264, which rounds to 12,868. The value is 12,8684,096 = 3.1416015625, with an error of 0.0000115625, well within half a step, 18,192 ≈ 0.000122. The stored integer 12,868 = 0011 0010 0100 0100, so the pattern is 0x3244.

Exercise .

Multiply −1.5 by 2.25 in Q3.4. Give the integer product, the result and its byte.

▸ Show solution

The stored integers are −24 and 36. The integer product is −864, with 8 fraction bits: -864256 = −3.375. Dividing by 16 gives −54 exactly, so the result is -5416 = −3.375, exactly −1.5 × 2.25.

The 8-bit two’s-complement pattern of −54 is that of 256 − 54 = 202 = 1100 1010.

Exercise .

Which numbers x round to 2.6875 in unsigned 4.4?

▸ Show solution

The nearest value is 4316 = 2.6875 exactly when x × 16 rounds to 43, that is, when 42.5 ≤ x × 16 ≤ 43.5 (with the halfway points resolved by the chosen rule). Dividing by 16: 2.65625 ≤ x ≤ 2.71875.

The interval has length 116, one step, centered on 2.6875: every value of the format is the rounding of one such interval.

Exercise .

Divide 1.5 by 0.625 in unsigned 4.4, keeping 4 fraction bits, as in the Looking-ahead note on division.

▸ Show solution

1.50.625 = 2.4. The stored integers are 24 and 10, and to keep 4 fraction bits the result is 24 × 16 ÷ 10 = 38.4, rounded to 38: 3816 = 2.375, an error of 0.025, within half a step.

Dividing the stored integers alone, 24 ÷ 10 = 2.4, gives a value without the scale factor; multiplying the dividend by 2ᶠ first keeps f fraction bits.

Exercise .

Prove that the product of the stored integers of two signed n-bit fixed-point numbers always fits in 2n bits of two’s complement.

▸ Show solution

Let k and j be stored integers of a signed n-bit format, −2ⁿ⁻¹ ≤ k, j ≤ 2ⁿ⁻¹ − 1. The largest product is (−2ⁿ⁻¹)² = 2²ⁿ⁻², from two smallest values, and 2²ⁿ⁻² ≤ 2²ⁿ⁻¹ − 1. The most negative product is −2ⁿ⁻¹ × (2ⁿ⁻¹ − 1) = −2²ⁿ⁻² + 2ⁿ⁻¹, which is at least −2²ⁿ⁻¹.

So every product lies in the 2n-bit two’s-complement range −2²ⁿ⁻¹ to 2²ⁿ⁻¹ − 1. With f fraction bits each, the product has 2f fraction bits, as Theorem 6 says.

Summary

  1. Binary numerals continue after a binary point with the places ½, ¼, ⅛, …. A numeral with f digits after the point is the integer of its digits divided by 2ᶠ. A fraction in lowest terms has a finite binary numeral exactly when its denominator is a power of 2, so 0.1 and 0.3 have none.
  2. A fixed-point format reads n bits as an integer k, unsigned or in two’s complement, with the value k/2ᶠ. The point is part of the agreement, not of the bits. Formats are written x.y and s x.y, or Qm.n for signed ones; documents differ on whether the sign bit is counted.
  3. Its values run from 0 to (2ⁿ − 1)/2ᶠ unsigned, or from −2ⁿ⁻¹/2ᶠ to (2ⁿ⁻¹ − 1)/2ᶠ signed, all one step 1/2ᶠ apart. More fraction bits give a finer step and a shorter range; small numbers keep few significant bits.
  4. Rounding to the nearest value (x × 2ᶠ to the nearest integer) errs by at most half a step; truncation by up to a whole step. Numbers outside the range overflow and are saturated or wrapped.
  5. Fixed-point numbers of one format add as integers. Their product is the integer product with 2f fraction bits, which must be shifted back by f bits and rounded. Fixed point serves signal processing, processors without floating point, and file formats.

Sources

  1. 1.
    “15. Floating-Point Arithmetic: Issues and Limitations”, The Python Tutorial, Python 3 documentation, Python Software Foundation.
    “the decimal fraction 0.625 has value 6/10 + 2/100 + 5/1000, and in the same way the binary fraction 0.101 has value 1/2 + 0/4 + 1/8.”
    Used for: the places right of the point in a binary numeral are worth 1/2, 1/4, 1/8, …, as decimal places are worth tenths, hundredths and thousandths.
  2. 2.
    Werner Buchholz (ed.), Planning a Computer System: Project Stretch (McGraw-Hill Book Company, New York, 1962).
    Chapter 8, “Floating-point Operation” (by S. G. Campbell), section 8.1, “Problems of Fixed-point Arithmetic”, p. 92 (the whole topic: pp. 92–94).
    “the machine was designed with the decimal point (or binary point) in a fixed position, as in a mechanical desk calculator.”
    Used for: the point of a binary numeral is called the binary point; early computers fixed the decimal or binary point in the design of the machine.
  3. 3.
    “15. Floating-Point Arithmetic: Issues and Limitations”, The Python Tutorial, Python 3 documentation, Python Software Foundation.
    “Unfortunately, most decimal fractions cannot be represented exactly as binary fractions.”
    Used for: most decimal fractions cannot be represented exactly as binary fractions.
  4. 4.
    ISO/IEC JTC 1/SC 22/WG 14, “N1169: ISO/IEC TR 18037, Programming languages — C — Extensions to support embedded processors (approval draft of the 2006 Technical Report)”, open-std.org, the site of the ISO C standards committee (4 April 2006).
    Section “Annex A.1.1.1, “The fixed-point data types”, p. 65”. open-std.org/jtc1/sc22/wg14/www/docs/n1169.pdf · open at the quoted passage.
    “while the position of the radix point is defined implicitly (i.e., outside the format representation)”
    Used for: in a fixed-point format the position of the point is defined outside the representation, so it cannot be read off the bits.
  5. 5.
    ISO/IEC JTC 1/SC 22/WG 14, “N1169: ISO/IEC TR 18037, Programming languages — C — Extensions to support embedded processors (approval draft of the 2006 Technical Report)”, open-std.org, the site of the ISO C standards committee (4 April 2006).
    Section “Annex A.1.1.1, “The fixed-point data types”, p. 66”. open-std.org/jtc1/sc22/wg14/www/docs/n1169.pdf · open at the quoted passage.
    “The hardware implementation of fixed-point arithmetic uses the same mechanisms and representations as the integer implementation on that same hardware. Hence, although the C standard allows other types of implementations for integers, this Technical Report requires exclusively two's complement behaviour for fixed-point arithmetic.”
    Used for: fixed-point hardware uses the same mechanisms and representations as integer hardware; the C extension requires two’s complement for fixed-point arithmetic.
  6. 6.
    ISO/IEC JTC 1/SC 22/WG 14, “N1169: ISO/IEC TR 18037, Programming languages — C — Extensions to support embedded processors (approval draft of the 2006 Technical Report)”, open-std.org, the site of the ISO C standards committee (4 April 2006).
    Section “4.1.1, “The data types”, p. 3”. open-std.org/jtc1/sc22/wg14/www/docs/n1169.pdf · open at the quoted passage.
    “(For the unsigned formats, the notation "x.y" means x integral bits and y fractional bits, for a total of x + y non-padding bits. The added "s" in the signed formats denotes the sign bit.)”
    Used for: the C extension writes an unsigned format with x integer bits and y fraction bits as x.y, and puts s before a signed format for its sign bit (its formats include s4.15).
  7. 7.
    Texas Instruments, “TMS320C64x DSP Library Programmer’s Reference (SPRU565B)”, Texas Instruments (October 2003).
    Section “Appendix A.2, “Fractional Q Formats”, p. A-3”. ti.com/lit/ug/spru565b/spru565b.pdf · open at the quoted passage.
    “In a Qm.n format, there are m bits used to represent the two’s complement integer portion of the number, and n bits used to represent the two’s complement fractional portion. m+n+1 bits are needed to store a general Qm.n number.”
    Used for: Q notation: Qm.n has m integer bits and n fraction bits and needs m + n + 1 bits, the extra one for the sign.
  8. 8.
    Texas Instruments, “TMS320C64x DSP Library Programmer’s Reference (SPRU565B)”, Texas Instruments (October 2003).
    Section “Appendix A.2, “Fractional Q Formats”, p. A-3”. ti.com/lit/ug/spru565b/spru565b.pdf · open at the quoted passage.
    “Unless specifically noted, DSPLIB functions use Q15 format, or to be more exact, Q0.15.”
    Used for: the 16-bit format Q0.15 is also called Q15; Texas Instruments’ C64x library calls it Q0.15 and uses it unless a function says otherwise.
  9. 9.
    Texas Instruments, “MSP DSP Library: Fixed-Point Data Types”, Texas Instruments, MSP DSP Library users guide (2015).
    “_q15 1 15 -1.0 0.9999695 0.00003051758”
    Used for: the MSP library lists Q15 with 1 integer bit and 15 fraction bits, the minimum −1.0, the maximum 0.9999695 and the resolution 0.00003051758 (the table’s columns are Type, Integer bits, Fractional bits, Minimum, Maximum, Resolution).
  10. 10.
    Microsoft, “The OpenType Font File (OpenType specification, version 1.9.1)”, Microsoft Learn, Typography documentation.
    “16-bit signed fixed number with the low 14 bits of fraction (2.14).”
    Used for: OpenType’s 16-bit signed type F2DOT14 has 14 fraction bits and is written 2.14.
  11. 11.
    Microsoft, “The OpenType Font File (OpenType specification, version 1.9.1)”, Microsoft Learn, Typography documentation.
    “32-bit signed fixed-point number (16.16)”
    Used for: OpenType’s 32-bit signed type Fixed is written 16.16.
  12. 12.
    Texas Instruments, “TMS320C64x DSP Library Programmer’s Reference (SPRU565B)”, Texas Instruments (October 2003).
    Section “Appendix A.2.2, “Q.15 Format”, p. A-3”. ti.com/lit/ug/spru565b/spru565b.pdf · open at the quoted passage.
    “The approximate allowable range of numbers in Q.15 representation is (−1,1) and the finest fractional resolution is 2−15 = 3.05 × 10−5.”
    Used for: the step is called the finest fractional resolution; Q15’s range is (−1, 1) and its resolution 2⁻¹⁵ ≈ 3.05 × 10⁻⁵.
  13. 13.
    Texas Instruments, “TMS320C64x DSP Library Programmer’s Reference (SPRU565B)”, Texas Instruments (October 2003).
    Section “Appendix A.2.1, “Q3.12 Format”, p. A-3”. ti.com/lit/ug/spru565b/spru565b.pdf · open at the quoted passage.
    “The approximate allowable range of numbers in Q.3.12 representation is (−8,8) and the finest fractional resolution is 2−12 = 2.441 × 10−4.”
    Used for: Q3.12 has the range (−8, 8) and the resolution 2⁻¹² ≈ 2.441 × 10⁻⁴.
  14. 14.
    ISO/IEC JTC 1/SC 22/WG 14, “N1169: ISO/IEC TR 18037, Programming languages — C — Extensions to support embedded processors (approval draft of the 2006 Technical Report)”, open-std.org, the site of the ISO C standards committee (4 April 2006).
    Section “4.1.1, “The data types”, p. 3”. open-std.org/jtc1/sc22/wg14/www/docs/n1169.pdf · open at the quoted passage.
    “The fract fixed-point types have no integral bits; consequently, values of unsigned fract types are in the range of 0 to 1, and values of signed fract types are in the range of -1 to 1.”
    Used for: formats with all their bits after the point hold values from 0 to 1 when unsigned and from −1 to 1 when signed.
  15. 15.
    ISO/IEC JTC 1/SC 22/WG 14, “N1169: ISO/IEC TR 18037, Programming languages — C — Extensions to support embedded processors (approval draft of the 2006 Technical Report)”, open-std.org, the site of the ISO C standards committee (4 April 2006).
    Section “4.1.3, “Rounding and Overflow”, p. 4”. open-std.org/jtc1/sc22/wg14/www/docs/n1169.pdf · open at the quoted passage.
    “If the source value cannot be represented exactly by the fixed-point type, the source value is rounded to either the closest fixed-point value greater than the source value (rounded up) or to the closest fixed-point value less than the source value (rounded down).”
    Used for: the C extension requires only that a value that cannot be represented be rounded to the closest value above it or below it.
  16. 16.
    ISO/IEC JTC 1/SC 22/WG 14, “N1169: ISO/IEC TR 18037, Programming languages — C — Extensions to support embedded processors (approval draft of the 2006 Technical Report)”, open-std.org, the site of the ISO C standards committee (4 April 2006).
    Section “Annex A.4, “Rounding and Overflow”, p. 70”. open-std.org/jtc1/sc22/wg14/www/docs/n1169.pdf · open at the quoted passage.
    “It was chosen not to specify this further as there is no common path chosen for this in hardware implementations, so it was decided to leave this implementation defined. The above requirement to precision means that the result should be within 1 unit in last place (ulp).”
    Used for: the choice is left to each implementation, and the result is within one step, called 1 unit in the last place.
  17. 17.
    Werner Buchholz (ed.), Planning a Computer System: Project Stretch (McGraw-Hill Book Company, New York, 1962).
    Chapter 8, “Floating-point Operation” (by S. G. Campbell), section 8.1, “Problems of Fixed-point Arithmetic”, p. 94 (the whole topic: pp. 92–94).
    “causes k leading zeros to occur in the fixed-point number, leaving a maximum of n - k , instead of n, significant digits.”
    Used for: Campbell’s 1962 account names the weakness of a common scale factor: a small number gets leading zeros and keeps fewer significant digits.
  18. 18.
    ISO/IEC JTC 1/SC 22/WG 14, “N1169: ISO/IEC TR 18037, Programming languages — C — Extensions to support embedded processors (approval draft of the 2006 Technical Report)”, open-std.org, the site of the ISO C standards committee (4 April 2006).
    Section “4.1.3, “Rounding and Overflow”, p. 4”. open-std.org/jtc1/sc22/wg14/www/docs/n1169.pdf · open at the quoted passage.
    “Of the two common approaches for fixed-point overflow handling (saturation and modular wrap-around) only saturation is required by this Technical Report; for a description of modular wrap-around, see Annex E.4. When calculating the saturated result on fixed-point overflow, the source value is replaced by the closest available fixed-point value.”
    Used for: a number outside the range overflows; the two usual remedies are saturation and modular wraparound; the C extension requires saturation, which replaces the number by the closest value of the format.
  19. 19.
    ISO/IEC JTC 1/SC 22/WG 14, “N1169: ISO/IEC TR 18037, Programming languages — C — Extensions to support embedded processors (approval draft of the 2006 Technical Report)”, open-std.org, the site of the ISO C standards committee (4 April 2006).
    Section “Annex A.4, “Rounding and Overflow”, p. 70”. open-std.org/jtc1/sc22/wg14/www/docs/n1169.pdf · open at the quoted passage.
    “Saturation on overflow is often preferred. Furthermore saturation on overflow is often supported in hardware by processors that naturally operate on fixed-point data types.”
    Used for: for fixed-point data saturation is often preferred and often supported by the hardware.
  20. 20.
    ISO/IEC JTC 1/SC 22/WG 14, “N1169: ISO/IEC TR 18037, Programming languages — C — Extensions to support embedded processors (approval draft of the 2006 Technical Report)”, open-std.org, the site of the ISO C standards committee (4 April 2006).
    Section “Annex A.4, “Rounding and Overflow”, p. 70”. open-std.org/jtc1/sc22/wg14/www/docs/n1169.pdf · open at the quoted passage.
    “would typically be compiled as a 32 x 32 -> 64-bit integer multiplication followed by a shift right by 31 bits”
    Used for: a fixed-point multiplication of the C extension’s 32-bit format with 31 fraction bits is a 32 × 32 → 64-bit integer multiplication followed by a shift right by 31 bits (the sentence begins “A multiplication of two 32-bit long fracts to a long fract result”).
  21. 21.
    ISO/IEC JTC 1/SC 22/WG 14, “N1169: ISO/IEC TR 18037, Programming languages — C — Extensions to support embedded processors (approval draft of the 2006 Technical Report)”, open-std.org, the site of the ISO C standards committee (4 April 2006).
    “Embedded processors are often used to analyze analogue signals and process these signals by applying filtering algorithms to the data received. Typical applications can be found in all wireless devices. The common data type used in filtering algorithms is the fixed-point data type”
    Used for: embedded processors often analyze signals with filtering algorithms, whose usual data type is fixed point (the sentence goes on: such processors are often equipped with special hardware for fixed-point data).
  22. 22.
    ISO/IEC JTC 1/SC 22/WG 14, “N1169: ISO/IEC TR 18037, Programming languages — C — Extensions to support embedded processors (approval draft of the 2006 Technical Report)”, open-std.org, the site of the ISO C standards committee (4 April 2006).
    Section “Annex A.4, “Rounding and Overflow”, p. 70”. open-std.org/jtc1/sc22/wg14/www/docs/n1169.pdf · open at the quoted passage.
    “Fixed-point data types are often used in situations where floating-point data types otherwise would have been used. Typically because the underlying hardware does not support floating-point operations directly for various reasons.”
    Used for: fixed-point types are often used where floating-point types would otherwise be, typically because the hardware does not support floating point directly.
  23. 23.
    Microsoft, “The OpenType Font File (OpenType specification, version 1.9.1)”, Microsoft Learn, Typography documentation.
    “The F2DOT14 format consists of a signed, 2’s complement integer and an unsigned fraction. To compute the actual value, take the integer and add the fraction.”
    Used for: a 2.14 number is a signed two’s-complement integer part and an unsigned fraction, added together.
  24. 24.
    Werner Buchholz (ed.), Planning a Computer System: Project Stretch (McGraw-Hill Book Company, New York, 1962).
    Chapter 8, “Floating-point Operation” (by S. G. Campbell), section 8.1, “Problems of Fixed-point Arithmetic”, p. 92 (the whole topic: pp. 92–94).
    “Most of the early computers designed for scientific computation used fixed-point arithmetic.”
    Used for: most early computers designed for scientific computation used fixed-point arithmetic.
  25. 25.
    Werner Buchholz (ed.), Planning a Computer System: Project Stretch (McGraw-Hill Book Company, New York, 1962).
    Chapter 8, “Floating-point Operation” (by S. G. Campbell), section 8.1, “Problems of Fixed-point Arithmetic”, p. 93 (the whole topic: pp. 92–94).
    “it may exist only in the mind of the programmer, who takes it into account in his calculation, or it may appear explicitly in the computer memory.”
    Used for: the scale factor could exist only in the programmer’s mind, taken into account by hand.
  26. 26.
    Werner Buchholz (ed.), Planning a Computer System: Project Stretch (McGraw-Hill Book Company, New York, 1962).
    Chapter 8, “Floating-point Operation” (by S. G. Campbell), section 8.2, “Floating-point Arithmetic”, p. 94 (the whole topic: pp. 94–95).
    “To avoid difficulties of limited range and scaling in fixed-point arithmetic, G. R. Stibitz in the early 1940’s proposed an automatic scaling procedure, called floating-point arithmetic,”
    Used for: to avoid the difficulties of limited range and scaling, Stibitz proposed an automatic scaling procedure, floating-point arithmetic, in the early 1940s.