Q15 Library

mikroPascal PRO for PIC32 includes a library for operating and working with Q15 fractional number format.

Library Routines

Q15_Abs

Prototype

function Q15_Abs(input : integer; output : ^integer) : word;

Description

Function calculates absolute value of the Q15 fractional format number.

Parameters
  • in: Q15 input number, ranging from -32768 to 32767.
  • out: pointer to Q15 output number.
Returns

Function always returns zero.

Q15_Add

Prototype

function Q15_Add(input1 : integer; input2 : integer; output : ^integer) : word;

Description

Function sums two Q15 format numbers.

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

Parameters
  • in1: First Q15 number, ranging from -32768 to 32767.
  • in2: Second Q15 number, ranging from -32768 to 32767.
  • out: pointer to Q15 output number.
Returns
  • 1 - if the result exceeds [-32768, 32767] limits.
  • 0 - otherwise.

Q15_Sub

Prototype

function Q15_Sub(input1 : integer; input2 : integer; output : ^integer) : word;

Description

Function subtracts two Q15 format numbers.

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

Parameters
  • in1: First Q15 number, ranging from -32768 to 32767.
  • in2: Second Q15 number, ranging from -32768 to 32767.
  • out: pointer to Q15 output number.
Returns
  • 1 - if the result exceeds [-2147483648, 2147483647] limits.
  • 0 - otherwise.

Q15_Itof

Prototype

function Q15_Itof(x : integer; f : ^real) : word;

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.
  • f: pointer to the resulting floating point number.
Returns

Function always returns zero.

Q15_Ftoi

Prototype

function Q15_Ftoi(f : real; x : ^integer) : word;

Description

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

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

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

Parameters
  • f: floating format number to be converted.
  • x: pointer to the resulting Q15 fractional format number.
Returns
  • 1 - If overflow has been detected.
  • 0 - If no errors occured.

Q15_Itoa

Prototype

procedure Q15_Itoa(x : integer; s : ^char);

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: pointer to the resulting ASCII string.
Returns

Nothing.

Q15_Atoi

Prototype

function Q15_Atoi(s : ^char; x : ^integer) : word;

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: sn.ddddddddddddddd

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

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 17748 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 17748 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 -27580 to 27578.

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 -25733 to 25738.
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 -27568 to 27578.
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 -25532 to 25509.

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

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

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 2887 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 2887 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 -32748 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 -32748 to 0.

Library Example

program Q15_Test;

var Q15_1, Q15_2, i : integer;
    r               : word;
    f               : real;
    stringQ15       : string[19];
    
begin
    // Q15_Ftoi.........................................................
  f := 0.5;
  r := Q15_Ftoi(f, @Q15_1);                      // Q15_1 := 0x4000; r := 0;
  f := 1.5;
  r := Q15_Ftoi(f, @Q15_1);                      // Q15_1 := 0x7FFF; r := 1;
  f := -2.5;
  r := Q15_Ftoi(f, @Q15_1);                      // Q15_1 := 0x8000; r := 1;

  // Q15_Itof.........................................................
  Q15_1 := 0x1000;
  r := Q15_Itof(Q15_1, @f);                      // f := 0.125; r := 0;

  // Q15_Itoa.........................................................
  Q15_1 := integer(0xFFFF);
  Q15_Itoa(Q15_1, stringQ15);                   // stgingQ15 := "-0.000030517578125"

  // Q15_Atoi.........................................................
  r := Q15_Atoi('+0.25', @Q15_1);                // Q15_1 := 0x2000; r := 0;
  r := Q15_Atoi('        +0.7', @Q15_1);         // Q15_1 := 0x5999; r := 0;
  r := Q15_Atoi('mikroe.25', @Q15_1);            // r := 2;

  // Q15_Abs...........................................................
  Q15_Abs(integer(0x8000), @Q15_1);                      // Q15_1 := 0x7FFF;
  Q15_Abs(integer(0xE000), @Q15_1);                      // Q15_1 := 0X2000;

  // Q15_Add...........................................................
  r := Q15_Add(0x2000, 0x4000, @Q15_1);                            // Q15_1 := 0x6000; r := 0;
  r := Q15_Add(0x7000, 0x4000, @Q15_1);                            // Q15_1 := 0x7FFF; r := 1;

  // Q15_Sub...........................................................
  r := Q15_Sub(0x2000, 0x4000, @Q15_1);                            // Q15_1 := 0xE000; r := 0;
  r := Q15_Sub(0x7000, integer(0xC000), @Q15_1);                   // Q15_1 := 0x7FFF; r := 1;

  // Q15_Cos...........................................................
  Q15_Ftoi(0.5, @Q15_1);
  Q15_2 := Q15_Cos (Q15_1);
  Q15_Itof(Q15_2, @f);                          // f := cos(0.5) := 0.8775635

  Q15_Ftoi(1, @Q15_1);
  Q15_2 := Q15_Cos (Q15_1);
  Q15_Itof(Q15_2, @f);                          // f := cos(1) := 0.5403023

  // Q15_Sin...........................................................
  Q15_Ftoi(0.5, @Q15_1);
  Q15_2 := Q15_Sin (Q15_1);
  Q15_Itof(Q15_2, @f);                          // f := sin(0.5) := 0.4794006

  Q15_Ftoi(1, @Q15_1);
  Q15_2 := Q15_Sin (Q15_1);
  Q15_Itof(Q15_2, @f);                          // f := sin(1) := 0.84161138

  // Q15_Tan...........................................................
  Q15_Ftoi(0.5, @Q15_1);
  Q15_2 := Q15_Tan (Q15_1);
  Q15_Itof(Q15_2, @f);                          // f := tan(0.5) := 0.54623

  Q15_Ftoi(0.78546, @Q15_1);
  Q15_2 := Q15_Tan (Q15_1);
  Q15_Itof(Q15_2, @f);                          // f := tan(0.78546) := 0.999

  // Q15_Asin.........................................................
  Q15_Ftoi(0.5, @Q15_1);
  Q15_2 := Q15_Asin (Q15_1);
  Q15_Itof(Q15_2, @f);                          // f := asin(0.5) := 0.52349

  Q15_Ftoi(0.8416137, @Q15_1);
  Q15_2 := Q15_Asin (Q15_1);
  Q15_Itof(Q15_2, @f);                          // f := asin(0.8416137) := 0.999

  // Q15_Acos.........................................................
  Q15_Ftoi(0.9, @Q15_1);
  Q15_2 := Q15_Acos (Q15_1);
  Q15_Itof(Q15_2, @f);                          // f := acos(0.9) := 0.451026

  Q15_Ftoi(0.8, @Q15_1);
  Q15_2 := Q15_Acos (Q15_1);
  Q15_Itof(Q15_2, @f);                          // f := acos(0.8) := 0.6430969

  // Q15_Atan.........................................................
  Q15_Ftoi(0.5, @Q15_1);
  Q15_2 := Q15_Atan (Q15_1);
  Q15_Itof(Q15_2, @f);                          // f := atan(0.5) := 0.463531

  Q15_Ftoi(1, @Q15_1);
  Q15_2 := Q15_Atan (Q15_1);
  Q15_Itof(Q15_2, @f);                          // f := atan(1) := 0.778472

  // Q15_Log.........................................................
  Q15_Ftoi(0.5, @Q15_1);
  Q15_2 := Q15_Log (Q15_1);
  Q15_Itof(Q15_2, @f);                          // f := log(0.5) := -0.301239

  Q15_Ftoi(1, @Q15_1);
  Q15_2 := Q15_Log (Q15_1);
  Q15_Itof(Q15_2, @f);                          // f := log(1) := 0

end.
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