The funcionality is exported in the include file mathf8_24.h. THis file should be included in all XC and C files that use this library.
This type describes the fixed point numbers used in this library.
Values are stored in 32 bits comprising a sign bit (bit 31), 7 mantissa bits (bits 30..24), and 24 fractional bits (bits 23..0). The value of a fixed point number is defined as the the signed integer value multiplied by 2^-24. There are no representations for not-a-number or infinite.
This constant defines the smallest number that is defined in the fixed point range, which is -128.
This constant defines the biggest number that is defined in the fixed point range, which is 127.999999940395355224609375.
This constant is the fixed point representation of the number 0.5.
This constant is the fixed point representation of the number 1.0.
This constant is the closest fixed point representation of 2 PI.
This constant is the fixed point representation of PI/2.
The addition and subtraction operators + and - can be used on fixed point numbers as normal. Make sure that constants are shifted up 24 places, ie, for a fixed point number f, the statement f++ adds 0.000000059604644775390625 not one.
This function returns the product of two fixed point numbers.
If the product does not fit in a fixed point number, a modulo answer is returned.
Parameters: |
|
---|---|
Returns: | x*y |
This function returns the division of two fixed point numbers.
If the product does not fit in a fixed point number, higher bits will be lost.
Parameters: |
|
---|---|
Returns: | x / y |
This function is the fixed point equivalent of ldexp(): given a mantissa and an exponent it computes the fixed point number with the value mant * 2 ^ exp.
The reverse operation is implemented by frexp8_24().
Parameters: |
|
---|---|
Returns: | math * 2 ^ exp |
This functions splits a fixed point number into a mantissa and exponent.
The mantissa is either zero or in the range [0.5..1). The exponent is in the range [-32..32]. and an exponent of a fixed point number. The reverse operation is implemented by ldexp8_24().
Parameters: |
|
---|---|
Returns: | a mantissa as an fixed point number |
an exponent as an integer.
This function clips and rounds a 16.48 fixed point number into an 8.24 fixed point number.
It is used for finalising the answer out of a series of multiply accumulates. If an overflow has happened, the result is saturated to MINF8_24 or MAXF8_24. The number is rounded towards minus infinity; if rounding is required half should be added to the number, typically before the multiply accumulate
Parameters: |
|
---|---|
Returns: | saturated (h:l)[bits 8..40] |
This function converts a fixed point number to an integer, discarding the fractional part.
Parameters: |
|
---|---|
Returns: | integer equivalent |
This function converts an integer to a fixed point number.
The input to int2f8_24() has to be in the range [-128..127].
Parameters: |
|
---|---|
Returns: | fixed point equivalent |
This function returns the absolute value of a fixed point number.
The input value has to be in the range [-MAXF8_24..MAXF8_24]; the absolute value of MINF8_24 cannot be represented as a fixed point number.
Parameters: |
|
---|---|
Returns: |
This function rounds a fixed point number to its nearest integral number and returns a fixed point number.
The input to froundf8_24() should be less than 127.5.
Parameters: |
|
---|---|
Returns: | nearest integer. |
This function returns the sine of a fixed point number in radians.
The input number has to be in the range -MAXF8_24 + PI and MAXF8_24 - PI.
Parameters: |
|
---|---|
Returns: | sine(x) |
This function returns the cosine of a fixed point number in radians.
The input number has to be in the range -MAXF8_24 + PI and MAXF8_24 - PI.
Parameters: |
|
---|---|
Returns: | cosine(x) |
This function returns the hyperbolic sine (sinh) of a fixed point number.
The input number has to be in the range [-5.5..5.5] in order to avoid overflow, and for values outside the [-4..4] range there are relatively large errors in the result.
Parameters: |
|
---|---|
Returns: | sinh(x) |
This function returns the hyperbolic cosine (cosh) of a fixed point number.
The input number has to be in the range [-5.5..5.5] in order to avoid overflow, and for values outside the [-4..4] range there are relatively large errors in the result.
Parameters: |
|
---|---|
Returns: | sinh(x) |
This function returns the square root of a fixed point number.
The input number has to be positive.
Parameters: |
|
---|---|
Returns: | sqrt(x) |
This function returns the natural exponent of a fixed point number.
The input number has to be less than 4.8, otherwise the answer cannot be represented as a fixed point number. For input values larger than 3 there is a relatively large error in the last three bits of the answer.
Parameters: |
|
---|---|
Returns: | e^x |
Below is an example function that prints out the values of a sine wave:
#include "mathf8_24.h"
void printsine(void) {
for(f8_24 rad = 0; rad <= PI2; rad += PI2/20) {
printstr("sin ");
printf8_24(rad);
printstr(" = ");
printf8_24ln(cosf8_24(rad));
}
}