SBAA588 April 2024 LM73 , LM75B , LM95071 , TMP100 , TMP101 , TMP102 , TMP103 , TMP104 , TMP107 , TMP1075 , TMP108 , TMP112 , TMP114 , TMP116 , TMP117 , TMP121 , TMP122 , TMP123 , TMP124 , TMP126 , TMP144 , TMP175 , TMP1826 , TMP1827 , TMP275 , TMP400 , TMP401 , TMP411 , TMP421 , TMP422 , TMP423 , TMP431 , TMP432 , TMP435 , TMP451 , TMP461 , TMP464 , TMP468 , TMP4718 , TMP75 , TMP75B , TMP75C
Q format is a method of encoding rational numbers. Typically four or more bits are reserved to express fractional values between 1 and 0. Rational data stored in Q format can be manipulated and stored efficiently without the need for floating point operations that are sometimes prohibited in microcontroller code. In this document, the number following Q refers to the number of fractional bits. Other sources denote the number of integer bits in addition to the fractional bits, as shown in the table, where m is the number of integer bits, including the sign, and n is the number of fractional bits. All sources reviewed agree on a shorthand Qn notation that only includes the n fractional bits after the Q, which is consistent with this document.
Source | Q Format | Q Example | Example Detail |
---|---|---|---|
This document | Qn | Q4 | 12 bits total 8 integer bits including sign (7 integer bits without sign) 4 fractional bits |
Variant 1 | Qm.n | Q8.4 | |
Variant 2 (TI) | Q(m-1).n | Q7.4 | |
Variant 3 (ARM) | Qn.m | Q4.8 |
Q format can also be called a Fixed-point data format. Fixed-point data has a preconfigured resolution where floating point data has a variable resolution. The following table shows the relationship between resolution and bit weight for a chosen Q format. Higher Q formats are possible, but are not employed in temperature sensors today.
Q0 | Q1 | Q2 | Q3 | Q4 | Q5 | Q6 | Q7 |
---|---|---|---|---|---|---|---|
1 | 0.5 | 0.25 | 0.125 | 0.0625 | 0.03125 | 0.015625 | 0.0078125 |
1 | 1/2 | 1/4 | 1/8 | 1/16 | 1/32 | 1/64 | 1/128 |
20 | 2-1 | 2-2 | 2-3 | 2-4 | 2-5 | 2-6 | 2-7 |
As an example, integer data that is known to be in Q4 format can be converted to the rational Q value by multiplying the data with 0.0625, 1/16 or 2-4 because these values are all equal.