Q31 Library

mikroBasic PRO for PIC32 includes a library for operating and working with Q31 fractional number format.

Library Routines

Q31_Abs

Prototype

sub function Q31_Abs(dim input as longint, dim output as ^longint) as word

Description

Function calculates absolute value of the Q31 fractional format number.

Parameters
  • input: Q31 input number.
  • output: number pointer to Q31 output number.
Returns

Function always returns zero.

Q31_Add

Prototype

sub function Q31_Add(dim input1 as longint, dim input2 as longint, dim output as ^longint) as word

Description

Function sums two Q31 format numbers.

If the result exceeds [-2147483648, 2147483647] limits, result will be maximal(minimal) number in Q31 format.

Parameters
  • input1: First Q31 number.
  • input2: Second Q31 number.
  • output: number pointer to Q31 output number.
Returns
  • 1 - if the result exceeds [-2147483648, 2147483647] limits.
  • 0 - otherwise.

Q31_Sub

Prototype

sub function Q31_Sub(dim input1 as longint, dim input2 as longint, dim output as ^longint) as word

Description

Function subtracts two Q31 format numbers.

If the result exceeds [-2147483648, 2147483647] limits, result will be maximal(minimal) number in Q31 format.

Parameters
  • input1: First Q31 number.
  • input2: Second Q31 number.
  • output: number pointer to Q31 output number.
Returns
  • 1 - if the result exceeds [-2147483648, 2147483647] limits.
  • 0 - otherwise.

Q31_Itof

Prototype

sub function Q31_Itof(dim x as longint, dim f as ^real) as word

Description

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

Parameters
  • x: input Q31 number, ranging from -2147483648 to 2147483647.
  • f: pointer to the resulting floating point number.
Returns

Function always returns zero.

Q31_Ftoi

Prototype

sub function Q31_Ftoi(dim f as real, dim x as ^longint) as word

Description

Function converts number from the floating point number format to Q31 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 0x7FFFFFFF (0x80000000).

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

Q31_Itoa

Prototype

sub procedure Q31_Itoa(dim x as longint, dim byref s as string)

Description

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

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

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

Parameters
  • x: Q31 format number to be converted.
  • src: pointer to the resulting ASCII string.
Returns

Nothing.

Q31_Atoi

Prototype

sub function Q31_Atoi(dim byref s as string, dim x as ^longint) as word

Description

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

Input ASCII string should be in the following format: sn.ddddddddddddddddddddddddddddddd

  • 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 Q31 format.
Returns
  • 2 - If input string is in not in the appropriate format.
  • 1 - If overflow has been detected.
  • 0 - If no errors occured.

Q31_Cos

Prototype

sub function Q31_Cos(dim x as longint) as longint

Description

Function returns the cosine of x. The input number is in the Q31 format number and returns value in the Q31 format, ranging from 1160290361 to 2147483647.

Parameters
  • x: input number in the Q31 format number, ranging from -2147483648 to 2147483647
Returns

Function returns value in the Q31 format, ranging from 1160290361 to 2147483647.

Q31_Sin

Prototype

sub function Q31_Sin(dim x as longint) as longint

Description

Function returns the sine of x. The input number is in the Q31 format number and returns value in the Q31 format, ranging from -1807039907 to 1807039904.

Parameters
  • x: input number in the Q31 format number, ranging from -2147483648 to 2147483647.
Returns

Function returns value in the Q31 format, ranging from -1807039907 to 1807039904.

Q31_Tan

Prototype

sub function Q31_Tan(dim x as longint) as longint

Description

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

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

Function returns value in the Q31 format, ranging from -2147483648 to 2147483647.

Q31_Asin

Prototype

sub function Q31_Asin(dim x as longint) as longint

Description

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

Parameters
  • x: input number in the Q31 format number, ranging from -1807076313 to 1807076320.
Returns

Function returns value in the Q31 format, ranging from -2147483648 to 2147483647.

Q31_Atan

Prototype

sub function Q31_Atan(dim x as longint) as longint

Description

Function returns the arc tangent of x. The input number is in the Q31 format number and returns value in the Q31 format, ranging from -1673210000 do 1673209982.

Parameters
  • x: input number in the Q31 format number, ranging from -2147483648 to 2147483647.
Returns

Function returns value in the Q31 format, ranging from -1673210000 do 1673209982.

Q31_Acos

Prototype

sub function Q31_Acos(dim x as longint) as longint

Description

Function returns the arc cosine of x. The input number is in the Q31 format number and returns value in the Q31 format, ranging from 189407136 do 2147458881.

Parameters
  • x: input number in the Q31 format number, ranging from 1160311168 do 2147483647.
Returns

Function returns value in the Q31 format, ranging from 189407136 do 2147458881.

Q31_Log

Prototype

sub function Q31_Log(dim x as longint) as longint

Description

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

Parameters
  • x: input number in the Q31 format number, ranging from 214748368 do 2147483647.
Returns

Function returns value in the Q31 format, ranging from -2144585218 to 0.

Library Example

program Fixed_Point_Q31

dim Q31_1, Q31_2, l as longint
    r               as word
    f               as float
    stringQ31       as string[35]

main:
  ' Q31_Ftoi.........................................................
  f = 0.5
  r = Q31_Ftoi(f, @Q31_1)                       ' Q31_1 = 0x40000000  r = 0
  f = 1.5
  r = Q31_Ftoi(f, @Q31_1)                       ' Q31_1 = 0x7FFFFFF  r = 1
  f = -2.5
  r = Q31_Ftoi(f, @Q31_1)                       ' Q31_1 = 0x80000000  r = 1

  ' Q31_Itof.........................................................
  Q31_1 = 0x08000000
  r = Q31_Itof(Q31_1, @f)                       ' f = 0.0625  r = 0

  ' Q31_Itoa.........................................................
  Q31_1 = 0x00000001
  Q31_Itoa(Q31_1, stringQ31)                    ' stringQ31 = " 0.0000000004656612873077392578125"

  ' Q31_Atoi.........................................................
  r = Q31_Atoi("0.00001525878906", @Q31_1)      ' Q31_1 = 0x00007FFF  r = 0
  r = Q31_Atoi("+1.25", @Q31_1)                 ' r = 1

  ' Q31_Abs...........................................................
  Q31_Abs(0x12345678, @Q31_1)                            ' Q31_1 = 0x12345678
  Q31_Abs(longint(0xFEDCBA98), @Q31_1)                   ' Q31_1 = 0x01234568

  ' Q31_Add...........................................................
  r = Q31_Add(longint(0xE0000000), longint(0xC0000000), @Q31_1)   ' Q31_1 = 0xA0000000  r = 0
  r = Q31_Add(longint(0xA0000000), longint(0xC0000000), @Q31_1)   ' Q31_1 = 0x80000000  r = 1

  ' Q31_Sub...........................................................
  r = Q31_Sub(longint(0xE0000000), longint(0xC0000000), @Q31_1)   ' Q31_1 = 0x20000000  r = 0
  r = Q31_Sub(longint(0xA0000000), 0x40000000, @Q31_1)            ' Q31_1 = 0x80000000  r = 1

  ' Q31_Cos...........................................................
  Q31_Ftoi(0, @Q31_1)
  Q31_2 = Q31_Cos (Q31_1)
  Q31_Itof(Q31_2, @f)                           ' f = cos(0) = 1

  Q31_Ftoi(-1, @Q31_1)
  Q31_2 = Q31_Cos (Q31_1)
  Q31_Itof(Q31_2, @f)                           ' f = cos(-1) = 0.54

  ' Q31_Sin...........................................................
  Q31_Ftoi(0, @Q31_1)
  Q31_2 = Q31_Sin (Q31_1)
  Q31_Itof(Q31_2, @f)                           ' f = sin(0) = 0

  Q31_Ftoi(-1, @Q31_1)
  Q31_2 = Q31_Sin (Q31_1)
  Q31_Itof(Q31_2, @f)                           ' f = sin(-1) = -0.84

  ' Q31_Tan...........................................................
  Q31_Ftoi(0, @Q31_1)
  Q31_2 = Q31_Tan (Q31_1)
  Q31_Itof(Q31_2, @f)                           ' f = tan(0) = 0

  Q31_Ftoi(-0.78530, @Q31_1)
  Q31_2 = Q31_Tan (Q31_1)
  Q31_Itof(Q31_2, @f)                           ' f = tan(-0.78546) = -0.99

  ' Q31_Asin.........................................................
  Q31_Ftoi(0, @Q31_1)
  Q31_2 = Q31_Asin (Q31_1)
  Q31_Itof(Q31_2, @f)                           ' f = asin(0) = 0

  Q31_Ftoi(-0.841308, @Q31_1)
  Q31_2 = Q31_Asin (Q31_1)
  Q31_Itof(Q31_2, @f)                           ' f = asin(-0.8416137) = -0.99

  ' Q31_Acos.........................................................
  Q31_Ftoi(0.6, @Q31_1)
  Q31_2 = Q31_Acos (Q31_1)
  Q31_Itof(Q31_2, @f)                           ' f = acos(0.6) = 0.92

  Q31_Ftoi(0.5403137, @Q31_1)
  Q31_2 = Q31_Acos (Q31_1)
  Q31_Itof(Q31_2, @f)                           ' f = acos(0.5403137) = 0.99

  ' Q31_Atan.........................................................
  Q31_Ftoi(0, @Q31_1)
  Q31_2 = Q31_Atan (Q31_1)
  Q31_Itof(Q31_2, @f)                           ' f = atan(0) = 0

  Q31_Ftoi(-1, @Q31_1)
  Q31_2 = Q31_Atan (Q31_1)
  Q31_Itof(Q31_2, @f)                           ' f = atan(-1) = -0.77

  ' Q31_Log.........................................................
  Q31_Ftoi(0.3, @Q31_1)
  Q31_2 = Q31_Log (Q31_1)
  Q31_Itof(Q31_2, @f)                           ' f = log(0.3) = -0.52

  Q31_Ftoi(0.1, @Q31_1)
  Q31_2 = Q31_Log (Q31_1)
  Q31_Itof(Q31_2, @f)                           ' f = log(0.1) = -0.99
  asm nop  end asm
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