|
Q is a fixed point number format where the number of fractional bits (and optionally the number of integer bits) is specified. For example, a Q15 number has 15 fractional bits; a Q1.14 number has 1 integer bit and 14 fractional bits. Q format is often used in hardware that does not have a floating-point unit and in applications that require constant resolution.
CharacteristicsBecause Q format numbers are fixed point, they can be stored and operated on as integers. The number of fractional bits and the underlying integer size are chosen on an application-specific basis, depending on the range and resolution needed. The notation used is Qm.n, where:
Note that the most significant bit is always designated as the sign bit (the number is stored as a two's complement number). Representing a signed fixed-point data type in Q format therefore always requires m+n+1 bits to account for the sign. For a given Qm.n format, using an m+n+1 bit signed integer container with n fractional bits:
For example, a Q14.1 format number:
Unlike floating point, the resolution will remain constant over the entire range. ConversionFloat to QTo convert a number from floating point to Qm.n format:
Q to FloatTo convert a number from Qm.n format to floating point:
Math operationsQ numbers are a ratio of two integers, the numerator is kept in storage, the denominator is equal to 2n. Consider the following example: The Q8 denominator equals 28 = 256 1.5 equals 384/256 384 is stored, 256 is inferred because it is a Q8 number. If the Q numbers base is to be maintained (n remains constant) the Q number math operations must keep the denominator constant. Because the denominator is a power of two the multiplication can be implemented as an arithmetic shift to the left and the division as an arithmetic shift to the right; on many processors shifts are faster than multiplication and division. To maintain accuracy the intermediate multiplication and division results must be double precision and care must be taken in rounding the intermediate result before converting back to the desired Q number. Using C the operations are (note that here, Q refers to the fractional part's number of bits) : Additionsigned int a,b,result; result = a+b; Subtractionsigned int a,b,result; result = a-b; Multiplicationsigned int a,b,result,K; signed long int temp; temp = (long int) a * (long int) b; // result type is operand's type // Rounding; mid values are rounded up K = 1 << (Q-1); // 2**(Q-1) temp = temp + K; // Correct by dividing by base result= temp >> Q; Divisionsigned int a,b,result; signed long int temp; // pre-multiply by the base temp = a<<Q; // So the result will be rounded ; mid values are rounded up. temp = temp+b/2; result = temp/b; See alsoExternal links
|
This article is from Wikipedia. All text is available under the terms of the GNU Free Documentation License.
Mercedes Car
This site monitored by SitePinger.net