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]
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ᶠ.
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.
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.
What is the value of 110.0101₂?
- With the places: 4 + 2 + 14 + 116 = 6 + 0.25 + 0.0625 = 6.3125.
- 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.
Write 6.625 as a binary numeral.
- 6.625 = 6 + 58, so three digits after the point suffice: 6.625 × 8 = 53.
- 53 = 32 + 16 + 4 + 1 = 110101₂ (Number Bases: binary numbers).
- 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.
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, …
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]
Decide for 0.375, 512, 0.35 and 0.75.
- 0.375 = 38, and 8 = 2³: yes, 0.011₂.
- 512 is in lowest terms and 12 = 4 × 3 has the factor 3: no.
- 0.35 = 720, and 20 = 4 × 5 has the factor 5: no, although 0.35 is a terminating decimal.
- 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]
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]
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.
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 point | The pattern read with its point | Value |
|---|---|---|
| 0 | 0100 1100. | 76/1 = 76 |
| 4 | 0100.1100 | 76/16 = 4.75 |
| 7 | 0.1001 100 | 76/128 = 0.59375 |
Which value does the byte 1011 0110 hold in the unsigned 4.4 format?
- As an unsigned integer, 10110110 = 128 + 32 + 16 + 4 + 2 = 182.
- 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.
Which value does the byte 1110 1100 hold in Q3.4, 8 bits in two’s complement with 4 fraction bits?
- 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).
- 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
- 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ᶠ.
- 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ᶠ.
- In both, neighboring values differ by exactly 12ᶠ, everywhere in the range.
(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ᶠ.
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]
| Format | From … to | Step |
|---|---|---|
| unsigned 4.48 bits, 4 after the point | 0 … 15.9375 | 1/16 |
| Q3.48 bits, 4 after the point, signed | −8 … 7.9375 | 1/16 |
| Q15 = Q0.1516 bits, 15 after the point, signed | −1 … 1 − 1/32,768 | 1/32,768 |
| Q3.1216 bits, 12 after the point, signed | −8 … 8 − 1/4,096 | 1/4,096 |
| Q1.1416 bits, 14 after the point, signedOpenType “2.14” | −2 … 2 − 1/16,384 | 1/16,384 |
| Q15.1632 bits, 16 after the point, signedOpenType “16.16” | −32,768 … 32,768 − 1/65,536 | 1/65,536 |
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.
Which bytes hold the largest and smallest values of Q3.4, and what are those values?
- The largest stored integer is 2⁷ − 1 = 127 = 0111 1111, so the largest value is 12716 = 7.9375.
- 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.
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.
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ᶠ.
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]
Store 2.7 in the unsigned 4.4 format.
- 2.7 × 16 = 43.2, which lies between 43 and 44; 43 is nearer.
- 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.
Store −0.3 in Q3.4.
- −0.3 × 16 = −4.8, which lies between −5 and −4; −5 is nearer.
- 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.
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.99 × 16 = 31.84. Truncation keeps 31 = 0001 1111: the value 1.9375, an error of 0.0525.
- 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.
Store 0.01 and 200.01 in the unsigned 8.8 format (16 bits, 8 after the point). Compare the errors.
- 0.01 × 256 = 2.56, rounded to 3: the value 3256 = 0.01171875, an error of 0.00171875.
- 200.01 × 256 = 51,202.56, rounded to 51,203: the value 200.01171875, the same error 0.00171875.
- 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]
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.
Your bits store 0, so their value is 0/32 = 0, at a distance of 1.727 from −1.727.
Adding and multiplying
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).
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.
Add 2.6875 = 0010.1011 and 1.5 = 0001.1000 in unsigned 4.4.
- The stored integers are 43 and 24, and 43 + 24 = 67.
- In columns: 00101011 + 00011000 = 01000011, with no carry out.
Answer: 0100.0011 = 6716 = 4.1875, and indeed 2.6875 + 1.5 = 4.1875, exactly.
Add x = 1.5 in Q3.4 and y = 0.40625 in Q1.6.
- x has the stored integer 24 with 4 fraction bits; y has 26 with 6 fraction bits.
- Bring x to 6 fraction bits by writing two zeros after it: 24 × 4 = 96, and 9664 = 1.5.
- 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.
(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.
(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]
Multiply 2.5 by 1.75 in unsigned 4.4.
- The stored integers are 40 and 28, and 40 × 28 = 1,120. With 8 fraction bits the product is 1,120256 = 4.375.
- 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.
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?
- The last 8 bits are 1000 0111 = 135, read as 13516 = 8.4375.
- 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.
Multiply −1 by −1 in Q15.
- −1 is the stored integer −32,768 = −2¹⁵, the smallest of Q15.
- The integer product is (−2¹⁵) × (−2¹⁵) = 2³⁰, with 30 fraction bits: exactly 1.
- 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.
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:
Your bits store 0, the value 0.
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]
An OpenType file holds the 16-bit 2.14 number 0xE000. What is its value?
- 0xE000 = 1110 0000 0000 0000. In 2.14 the point comes after the first two bits: 11.10 0000 0000 0000.
- The integer part 11 in two’s complement is −2 + 1 = −1; the fraction .1000… is 12.
- 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.
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.
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
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.
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.
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.
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.
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.
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.
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.
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
- 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.
- 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.
- 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.
- 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.
- 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.“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.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.“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.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.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.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.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.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.Texas Instruments, “MSP DSP Library: Fixed-Point Data Types”, Texas Instruments, MSP DSP Library users guide (2015).Section “Fixed-Point Data Types (the table of the formats’ properties)”. software-dl.ti.com/msp430/msp430_public_sw/mcu/msp430/DSPLib/latest/exports/html/usersguide_fixed.html · open at the quoted passage.
“_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.Microsoft, “The OpenType Font File (OpenType specification, version 1.9.1)”, Microsoft Learn, Typography documentation.Section “Data types”. learn.microsoft.com/en-us/typography/opentype/spec/otff · open at the quoted passage.
“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.Microsoft, “The OpenType Font File (OpenType specification, version 1.9.1)”, Microsoft Learn, Typography documentation.Section “Data types”. learn.microsoft.com/en-us/typography/opentype/spec/otff · open at the quoted passage.
“32-bit signed fixed-point number (16.16)”
Used for: OpenType’s 32-bit signed type Fixed is written 16.16. - 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.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.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.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.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.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.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.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.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.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 “Introduction, p. vii”. open-std.org/jtc1/sc22/wg14/www/docs/n1169.pdf · open at the quoted passage.
“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.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.Microsoft, “The OpenType Font File (OpenType specification, version 1.9.1)”, Microsoft Learn, Typography documentation.Section “Data types”. learn.microsoft.com/en-us/typography/opentype/spec/otff · open at the quoted passage.
“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.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.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.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.