Q15 Library
mikroBasic PRO for PIC32 includes a library for operating and working with Q15 fractional number format.
Library Routines
- Q15_Abs
- Q15_Add
- Q15_Sub
- Q15_Itof
- Q15_Ftoi
- Q15_Itoa
- Q15_Atoi
- Q15_Sin
- Q15_Cos
- Q15_Tan
- Q15_Asin
- Q15_Acos
- Q15_Atan
- Q15_Log
Q15_Abs
Prototype |
sub function Q15_Abs(dim input as integer, dim output as ^integer) as word |
---|---|
Description |
Function calculates absolute value of the Q15 fractional format number. |
Parameters |
|
Returns |
Function always returns zero. |
Q15_Add
Prototype |
sub function Q15_Add(dim input1 as integer, dim input2 as integer, dim output as ^integer) as 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 |
|
Returns |
|
Q15_Sub
Prototype |
sub function Q15_Sub(dim input1 as integer, dim input2 as integer, dim output as ^integer) as 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 |
|
Returns |
|
Q15_Itof
Prototype |
sub function Q15_Itof(dim x as integer, dim f as ^real) as word |
---|---|
Description |
Function converts number from the Q15 fractional number format to floating point number format. |
Parameters |
|
Returns |
Function always returns zero. |
Q15_Ftoi
Prototype |
sub function Q15_Ftoi(dim f as real, dim x as ^integer) as 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 |
|
Returns |
|
Q15_Itoa
Prototype |
sub procedure Q15_Itoa(dim x as integer, dim byref s as string) |
---|---|
Description |
Function converts number from the Q15 fractional format to ASCII string. Output ASCII string is in the following format : sn.ddddddddddddddd
|
Parameters |
|
Returns |
Nothing. |
Q15_Atoi
Prototype |
sub function Q15_Atoi(dim byref s as string, dim x as ^integer) as 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
|
Parameters |
|
Returns |
|
Q15_Cos
Prototype |
sub function Q15_Cos(dim x as integer) as integer |
---|---|
Description |
Function returns the cosine of |
Parameters |
|
Returns |
Function returns value in the Q15 format, ranging from 17748 to 32767. |
Q15_Sin
Prototype |
sub function Q15_Sin(dim x as integer) as integer |
---|---|
Description |
Function returns the sine of |
Parameters |
|
Returns |
Function returns value in the Q15 format, ranging from -27580 to 27578. |
Q15_Tan
Prototype |
sub function Q15_Tan(dim x as integer) as integer |
---|---|
Description |
Function returns the tangent of |
Parameters |
|
Returns |
Function returns value in the Q15 format, ranging from -32768 to 32767. |
Q15_Asin
Prototype |
sub function Q15_Asin(dim x as integer) as integer |
---|---|
Description |
Function returns the arc sine of |
Parameters |
|
Returns |
Function returns value in the Q15 format, ranging from -32768 to 32767. |
Q15_Atan
Prototype |
sub function Q15_Atan(dim x as integer) as integer |
---|---|
Description |
Function returns the arc tangent of |
Parameters |
|
Returns |
Function returns value in the Q15 format, ranging from -25532 to 25509. |
Q15_Acos
Prototype |
sub function Q15_Acos(dim x as integer) as integer |
---|---|
Description |
Function returns the arc cosine of |
Parameters |
|
Returns |
Function returns value in the Q15 format, ranging from 2887 to 32767. |
Q15_Log
Prototype |
sub function Q15_Log(dim x as integer) as integer |
---|---|
Description |
Function returns the logarithm of |
Parameters |
|
Returns |
Function returns value in the Q15 format, ranging from -32748 to 0. |
Library Example
program Fixed_Point_Q15 dim Q15_1, Q15_2, i as integer r as word f as float stringQ15 as string[19] main: ' 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.87 Q15_Ftoi(1, @Q15_1) Q15_2 = Q15_Cos (Q15_1) Q15_Itof(Q15_2, @f) ' f = cos(1) = 0.54 ' Q15_Sin........................................................... Q15_Ftoi(0.5, @Q15_1) Q15_2 = Q15_Sin (Q15_1) Q15_Itof(Q15_2, @f) ' f = sin(0.5) = 0.47 Q15_Ftoi(1, @Q15_1) Q15_2 = Q15_Sin (Q15_1) Q15_Itof(Q15_2, @f) ' f = sin(1) = 0.84 ' Q15_Tan........................................................... Q15_Ftoi(0.5, @Q15_1) Q15_2 = Q15_Tan (Q15_1) Q15_Itof(Q15_2, @f) ' f = tan(0.5) = 0.54 Q15_Ftoi(0.78546, @Q15_1) Q15_2 = Q15_Tan (Q15_1) Q15_Itof(Q15_2, @f) ' f = tan(0.78546) = 0.99 ' Q15_Asin........................................................ Q15_Ftoi(0.5, @Q15_1) Q15_2 = Q15_Asin (Q15_1) Q15_Itof(Q15_2, @f) ' f = asin(0.5) = 0.52 Q15_Ftoi(0.8416137, @Q15_1) Q15_2 = Q15_Asin (Q15_1) Q15_Itof(Q15_2, @f) ' f = asin(0.8416137) = 0.99 ' Q15_Acos......................................................... Q15_Ftoi(0.9, @Q15_1) Q15_2 = Q15_Acos (Q15_1) Q15_Itof(Q15_2, @f) ' f = acos(0.9) = 0.45 Q15_Ftoi(0.8, @Q15_1) Q15_2 = Q15_Acos (Q15_1) Q15_Itof(Q15_2, @f) ' f = acos(0.8) = 0.64 ' Q15_Atan......................................................... Q15_Ftoi(0.5, @Q15_1) Q15_2 = Q15_Atan (Q15_1) Q15_Itof(Q15_2, @f) ' f = atan(0.5) = 0.46 Q15_Ftoi(1, @Q15_1) Q15_2 = Q15_Atan (Q15_1) Q15_Itof(Q15_2, @f) ' f = atan(1) = 0.77 ' Q15_Log......................................................... Q15_Ftoi(0.5, @Q15_1) Q15_2 = Q15_Log (Q15_1) Q15_Itof(Q15_2, @f) ' f = log(0.5) = -0.30 Q15_Ftoi(1, @Q15_1) Q15_2 = Q15_Log (Q15_1) Q15_Itof(Q15_2, @f) ' f = log(1) = 0 asm nop end asm end.
What do you think about this topic ? Send us feedback!