Q15 Library

mikroPascal PRO for dsPIC30/33 and PIC24 includes a library for operating and working with Q15 fractional number format.

Library Routines

Q15_Abs

Prototype

function Q15_Abs(input : integer) : integer;

Description

Function calculates absolute value of the Q15 fractional format number.

Parameters
  • x: Q15 input number, ranging from -32768 to 32767.
Returns

Function returns number in Q15 fractional format, ranging from 0 to 32767.

Q15_Add

Prototype

function Q15_Add(x : integer; y : integer; var z : integer) : byte;

Description

Function sums two Q15 format numbers.

If the result exceeds [-32768, 32767] limits, result will be maximal(minimal) number in Q15 format.

Parameters
  • x: First Q15 number, ranging from -32768 to 32767.
  • y: Second Q15 number, ranging from -32768 to 32767.
  • z: Q15 output number.
Returns
  • 1 - if the result exceeds [-32768, 32767] limits.
  • 0 - otherwise.

Q15_Sub

Prototype

function Q15_Sub(x : integer; y : integer; var z : ^integer) : byte;

Description

Function subtracts two Q15 format numbers.

If the result exceeds [-32768, 32767] limits, result will be maximal(minimal) number in Q15 format.

Parameters
  • x: First Q15 number, ranging from -32768 to 32767.
  • y: Second Q15 number, ranging from -32768 to 32767.
  • z: Q15 output number.
Returns
  • 1 - if the result exceeds [-32768, 32767] limits.
  • 0 - otherwise.

Q15_Itof

Prototype

function Q15_Itof(x : integer) : real;

Description

Function converts number from the Q15 fractional number format to floating point number format.

Parameters
  • x: input Q15 number, ranging from -32768 to 32767.
Returns

Function returns number in single precision floating point format, ranging from -1 to 0.99997.

Q15_Ftoi

Prototype

function Q15_Ftoi(r : real) : integer;

Description

Function converts number from the single precision floating point number format to Q15 fractional number format.

Input number scope should be in the range of [-1.00000, 0.99997].

If the floating point number is not in this range, the resulting number will be 0x7FFF (0x8000).

Parameters
  • x: floating format number to be converted.
Returns

Converted Q15 fractional fromat number in the range of -32768 to 32767.

Q15_Itoa

Prototype

procedure Q15_Itoa(x : integer; var s : string);

Description

Function converts number from the Q15 fractional format to ASCII string.

Output ASCII string is in the following format : sn.ddddddddddddddd

  • s - sign; '-' negative, space - positive.
  • n - integer part; 0 or 1.
  • d - decimal part; 0..9 (ending zeros are not displayed).

Parameters
  • x: Q15 format number to be converted.
  • s: resulting ASCII string.
Returns

Nothing.

Q15_Atoi

Prototype

function Q15_Atoi(var s : string; var x : ^integer) : byte;

Description

Function converts the input ASCII string s into a number in Q15 fractional point format.

Input ASCII string should be in the following format: _s_n.ddddddddddddddd_

  • _ - sign; '_' spaces or tabs (it is allowed any count od spaces or tabs).
  • s - sign; '-' negative, space - positive.
  • n - integer part; 0 or 1.
  • d - decimal part; 0..9, if there is more than 15 digits, only first 15 will be considered in calculation (ending zeros are not displayed).

Parser of input characters has following features :

  1. Spaces: leading, trailing and after sign are allowed.
  2. Zeroes: leading and trailing zeroes are allowed.
  3. Sign: positive number can have '+' sign or can be without sign.
  4. Special: accept omited digits before/after decimal point (.123 or -1.).
Parameters
  • s: pointer to the input ASCII string.
  • out: pointer to the resulting number in Q15 format.
Returns
  • 2 - If input string is in not in the appropriate format.
  • 1 - If overflow has been detected.
  • 0 - If no errors occured.

Q15_Cos

Prototype

function Q15_Cos(x : integer) : integer;

Description

Function returns the cosine of x. The input number is in the Q15 format number and returns value in the Q15 format, ranging from 17705 to 32767.

Parameters
  • x: input number in the Q15 format number, ranging from -32768 to 32767
Returns

Function returns value in the Q15 format, ranging from 17705 to 32767.

Q15_Sin

Prototype

function Q15_Sin(x : integer) : integer;

Description

Function returns the sine of x. The input number is in the Q15 format number and returns value in the Q15 format, ranging from -27580 to 27578.

Parameters
  • x: input number in the Q15 format number, ranging from -32768 to 32767.
Returns

Function returns value in the Q15 format, ranging from -27573 to 27573.

Q15_Tan

Prototype

function Q15_Tan(x : integer) : integer;

Description

Function returns the tangent of x. The input number is in the Q15 format number and returns value in the Q15 format, ranging from -32768 to 32767.

Parameters
  • x: input number in the Q15 format number, ranging from -25763 to 25735.
Returns

Function returns value in the Q15 format, ranging from -32768 to 32767.

Q15_Asin

Prototype

function Q15_Asin(x : integer) : integer;

Description

Function returns the arc sine of x. The input number is in the Q15 format number and returns value in the Q15 format, ranging from -32768 to 32767.

Parameters
  • x: input number in the Q15 format number, ranging from -27573 to 27573.
Returns

Function returns value in the Q15 format, ranging from -32768 to 32767.

Q15_Atan

Prototype

function Q15_Atan(x : integer) : integer;

Description

Function returns the arc tangent of x. The input number is in the Q15 format number and returns value in the Q15 format, ranging from -25736 to 25735.

Parameters
  • x: input number in the Q15 format number, ranging from -32768 to 32767.
Returns

Function returns value in the Q15 format, ranging from -25736 to 25735.

Q15_Acos

Prototype

function Q15_Acos(x : integer) : integer;

Description

Function returns the arc cosine of x. The input number is in the Q15 format number and returns value in the Q15 format, ranging from 256 to 32767.

Parameters
  • x: input number in the Q15 format number, ranging from 17705 to 32767.
Returns

Function returns value in the Q15 format, ranging from 256 to 32767.

Q15_Log

Prototype

function Q15_Log(x : integer) : integer;

Description

Function returns the logarithm of x. The input number is in the Q15 format number and returns value in the Q15 format, ranging from -32767 to 0.

Parameters
  • x: input number in the Q15 format number, ranging from 3277 to 32767.
Returns

Function returns value in the Q15 format, ranging from -32767 to 0.

Copyright (c) 2002-2012 mikroElektronika. All rights reserved.
What do you think about this topic ? Send us feedback!
Want more examples and libraries? 
Find them on LibStock - A place for the code