1.4 Math Libraries Help

Introduction

This section provides descriptions of the Math libraries that are available in MPLAB-X Harmony 3.

Description

The fixed point math libraries use fractional value represented in "Q" format as described below

Integer Representation of Fractions

Fractional representation of a real number is given by:

Qn.m where:

n is the number of data bits to the left of the radix point

m is the number of data bits to the right of the radix point

• a signed bit is implied, and takes one bit of resolution

• Shorthand may eliminate the leading 0, such as in Q0.15, which may be shortened to Q15, and similarly Q0.31, which is shortened to Q31

Qn.m numerical values are used by the library processing data as integers. In this format the n represents the number of integer bits, and the m represents the number of fractional bits. All values assume a sign bit in the most significant bit. Therefore, the range of the numerical value is:

-2(n-1) to [2(n-1)) - 2(-m)]; with a resolution of 2(-m).

A Q16 format number (Q15.16) would range from -32768.0 (0x8000 0000) to 32767.99998474 with a precision of 0.000015259 (or 2(-16)). For example, a numerical representation of the number 3.14159 in Q2.13 notation would be:

3.14159 * 213(13) = 25735.9 => 0x6488

And converting from the Q7.8 format with the value 0x1D89 would be:

0x1D89 / 2(8) = 7561 / 256 => 29.5316, accurate to 0.00391

The majority of the math libraries uses functions with variables in Q15 or Q31 format. This limits the equivalent numerical range to roughly -1.0 to 0.999999999. It is possible to represent other number ranges, but scaling before and after the function call are necessary. All library functions will saturate the output if the value exceeds the maximum or is lower than the minimum allowable value for that resolution. Some prescaling may be necessary to prevent unwanted saturation in functions that may otherwise create calculation errors.

Usually, a Q16, Q31 and version of each function is available. Floating point versions are also available for CMSIS-DSP.