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

FP16 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 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, qlibs::fp16, 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

Basic operator overloading also perform rounding and detect overflows. When overflow is detected, they return overflow as a marker value.

  • + Addition
  • - Subtraction
  • * Multiplication
  • / Division

Exponential and transcendental functions

Roots, exponents & similar.

  • sqrt() Square root. Performs rounding and is accurate to fp16 limits.
  • 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.
  • log() Natural logarithm using Newton approximation and exp(). Worst case error +-3 absolute, average error less than 1 unit.
  • log2() Logarithm base 2.
  • pow() Computes the power of a qlibs::fp16 number

Trigonometric functions and helpers

  • sin() Sine for angle in radians
  • cos() Cosine for angle in radians
  • tan() Tangent for angle in radians
  • asin() Inverse of sine, output in radians
  • acos() Inverse of cosine, output in radians
  • atan() Inverse of tangent, output in radians
  • atan2() Arc tangent in radians x,y
  • sinh() Hyperbolic sine
  • cosh() Hyperbolic cosine
  • tanh() Hyperbolic tangent
  • radToDeg() Converts angle units from radians to degrees.
  • degToRad() Converts angle units from degrees to radians
  • wrapToPi() Wrap the fixed-point angle in radians to [−pi pi]
  • wrapTo180() Wrap the fixed-point angle in degrees to [−180 180]

Fixed-Point literals

Fixed-point literal defines a compile-time constant whose value is specified in the source file. The suffix _fp indicates a type of qlibs::fp16.

A plus (+) or minus (-) symbol can precede a fixed-point literal. However, it is not part of the literal; it is interpreted as a unary operator.

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 <iostream>
#include <qlibs.h>
using namespace qlibs;
int main( int argc, char *argv[] )
{
fp16 a = 1.5_fp;
fp16 b = 5.2_fp;
fp16 c = 4.0_fp;
fp16 result;
result = ( -b + fp16::sqrt( ( b*b ) - ( 4_fp*a*c ) ) )/( 2_fp*a );
std::cout << " result = " << result << std::endl;
return EXIT_SUCCESS;
}
Fixed-point Q16.16 type with width of exactly 32 bits.
Definition fp16.hpp:31
The qLibs++ library namespace.
Definition fp16.cpp:4