Q (number format)

Q (number format)

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.

Characteristics

Because 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 Q"m"."n", where:
*Q designates that the number is in Q format notation — the Texas Instruments representation for signed fixed-point numbers.
*"m" (optional; default=0) is the number of bits used to designate the two's complement integer portion of the number, exclusive of the sign bit.
*"n" is the number of bits used to designate the two's complement fractional portion of the number, i.e. the number of bits to the right of the binary point.

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 Q"m"."n" format, using an "m"+"n"+1 bit signed integer container with "n" fractional bits:
* its range is [-2"m", 2"m" - 2-"n"]
* its resolution is 2-"n"

For example, a Q14.1 format number:
* requires 14+1+1 = 16 bits
* its range is [-214, 214 - 2-1] = [-16384.0, +16383.5] = [0x8000, 0x8001 … 0xFFFF, 0x0000, 0x0001 … 0x7FFE, 0x7FFF]
* its resolution is 2-1 = 0.5

Unlike floating point, the resolution will remain constant over the entire range.

Conversion

Float to Q

To convert a number from floating point to Q"m"."n" format:
# Multiply the floating point number by 2"n"
# Round to the nearest integer

Q to Float

To convert a number from Q"m"."n" format to floating point:
# Convert the number directly to floating point
# Divide by 2"n"

Math operations

Q numbers are a ratio of two integers, the numerator is kept in storage, the denominator is equal to 2"n".

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.

: extstylefrac{num1}{d} + frac{num2}{d} = frac{num1+num2}{d}: extstylefrac{num1}{d} - frac{num2}{d} = frac{num1-num2}{d}:( extstylefrac{num1}{d} imes frac{num2}{d}) imes d = frac{num1 imes num2}{d}:( extstylefrac{num1}{d} / frac{num2}{d})/d = frac{num1/num2}{d}

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) :

Addition

signed int a,b,result; result = a+b;

ubtraction

signed int a,b,result; result = a-b;

Multiplication

signed 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;

Division

signed int a,b,result; signed long int temp; // pre-multiply by the base temp = a<ee also

*Binary scaling
*fixed-point arithmetic
*floating point

External links

* [http://www.superkits.net/whitepapers/Fixed%20Point%20Representation%20&%20Fractional%20Math.pdf Fixed Point Representation And Fractional Math] (Note: the accuracy of the article is in dispute; see discussion.)


Wikimedia Foundation. 2010.

Игры ⚽ Поможем сделать НИР

Look at other dictionaries:

  • Computer number format — A computer number format is the internal representation of numeric values in digital computer and calculator hardware and software.[1] Contents 1 Bits, bytes, nibbles, and unsigned integers 1.1 Bits 1.2 …   Wikipedia

  • Format string attack — Format string attacks are a class of software vulnerability discovered around 1999. Previously thought harmless, Format string attacks can be used to crash a program or to execute harmful code. The problem stems from the use of unfiltered user… …   Wikipedia

  • Number 96 (TV series) — Number 96 Title card from a 1975 episode of Number 96. Where the cliff hanger resolution that followed this shot at the start of the episode took place in one of the building s flats, the shot of the building would zoom in on that flat as the… …   Wikipedia

  • Format — For mat (f[ o]r m[.a] or f[ o]r m[aum]t ), n. [F. or G. Cf. {Formation}.] 1. (Print.) The shape and size of a book or other printed publication; hence, its external form. [WordNet sense 2] [Webster 1913 Suppl.] The older manuscripts had been… …   The Collaborative International Dictionary of English

  • Number One Enemy — Single by Daisy Dares You featuring Chipmunk from the album Rush Released 1 March 2010 …   Wikipedia

  • Number one (film, 1973) — Number one Titre original Number one Réalisation Gianni Buffardi Acteurs principaux Luigi Pistilli Renzo Montagnani Claude Jade Chris Avram Scénario Sandro Continenza Musique Giancarlo Chiaramello Photographi …   Wikipédia en Français

  • Number Six (The Prisoner) — Number Six is the central fictional character in the 1960s television series The Prisoner, played by Patrick McGoohan. In the AMC remake, the character is played by Jim Caviezel, renamed Six . In several episodes, his attempts to escape his… …   Wikipedia

  • Number 1 (Goldfrapp song) — Number 1 Single by Goldfrapp from the album Supernature B side …   Wikipedia

  • Number 1 (Tinchy Stryder song) — Number 1 Single by Tinchy Stryder featuring N Dubz from the album Catch 22 Against All Odds B side Stuck on …   Wikipedia

  • Number Six (song) — Number Six Single by Alice Nine from the album Alpha Released October 4, 2006 …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”