Documentation
Tools for embedded systems
Loading...
Searching...
No Matches
Fixed-Point Q16.16 library

qFP16 is a compact fixed-point number library intended for use in embedded systems. It includes a variety of transcendental functions and essential operators, carefully chosen for optimal performance.

The format is a signed Q16.16, which is good enough for most purposes.

Datatype limits

The maximum representable value is 32767.999985. The minimum value is -32768.0

The minimum value is also used to represent fp16.overflow for overflow detection, so for some operations it cannot be determined whether it overflowed or the result was the smallest possible value. In practice, this does not matter much.

The smallest unit (machine precision) of the datatype is 1/65536=0.000015259.

Fixed-point functions

All the provided functions operate on 32-bit numbers, qFP16_t, which have 16-bit integer part and 16-bit fractional part.

Conversion functions

Conversion from integers and floating-point values. These conversions retain the numeric value and perform rounding where necessary.

Basic arithmetic

These functions perform rounding and detect overflows. When overflow is detected, they return qfp16.overflow as a marker value.

Exponential and transcendental functions

Roots, exponents & similar.

  • qFP16_Sqrt() Square root. Performs rounding and is accurate to qFP16 limits.
  • qFP16_Exp() Exponential function using power series approximation. Accuracy depends on range, worst case +-40 absolute for negative inputs and +-0.003% for positive inputs. Average error is +-1 for neg and +-0.0003% for pos.
  • qFP16_Log() Natural logarithm using Newton approximation and qFP16_Exp(). Worst case error +-3 absolute, average error less than 1 unit.
  • qFP16_Log2() Logarithm base 2.
  • qFP16_IPow() Computes the integer-power of a qFP16_t number
  • qFP16_Pow() Modulo

Trigonometric functions and helpers

Example: Solution of the quadratic equation

This draft example computes one solution of the quadratic equation by using the fixed point format. Equation is given by:

\( x = \frac{ -b + \sqrt{ b^{2} - 4ac} }{ 2a } \)

#include <stdio.h>
#include <stdlib.h>
#include "qfp16.h"
int main( int argc, char *argv[] )
{
qFP16_t a = qFP16_Constant( 1.5f );
qFP16_t b = qFP16_Constant( 5.2f );
qFP16_t c = qFP16_Constant( 4.0f );
qFP16_t tmp;
char ans[ 10 ];
tmp = qFP16_Mul( qFP16_IntToFP( 4 ), qFP16_Mul( a, c ) );
tmp = qFP16_Add( -b, qFP16_Sqrt( qFP16_Sub( qFP16_Mul( b, b ), tmp ) ) );
tmp = qFP16_Div( tmp, qFP16_Mul( qFP16_IntToFP( 2 ), a ) );
printf( " result = %s \r\n" , qFP16_FPToA( tmp, ans, 4 ) );
return EXIT_SUCCESS;
}
qFP16_t qFP16_Sqrt(qFP16_t x)
Returns the fixed-point square root of x.
Definition qfp16.c:370
#define qFP16_Constant(x)
A macro for defining a fixed-point constant value.
Definition qfp16.h:69
char * qFP16_FPToA(const qFP16_t num, char *str, int decimals)
Converts the fixed-point value to a formatted string.
Definition qfp16.c:806
int32_t qFP16_t
Fixed-point Q16.16 type with width of exactly 32 bits.
Definition qfp16.h:25
qFP16_t qFP16_Div(const qFP16_t x, const qFP16_t y)
Returns the fixed-point division operation x / y.
Definition qfp16.c:298
qFP16_t qFP16_Add(const qFP16_t X, const qFP16_t Y)
Returns the fixed-point addition x + y.
Definition qfp16.c:219
qFP16_t qFP16_IntToFP(const int x)
Returns the int value x converted to fixed-point q16.16.
Definition qfp16.c:141
qFP16_t qFP16_Sub(const qFP16_t X, const qFP16_t Y)
Returns the fixed-point subtraction x - y.
Definition qfp16.c:233
qFP16_t qFP16_Mul(const qFP16_t x, const qFP16_t y)
Returns the fixed-point product operation x * y.
Definition qfp16.c:247