Fixed Point Arithmetic
0
Fixed-point math library.
|
This header depends only upon the definition of. More...
Modules | |
Comparison functions | |
Fixed point arithmetic | |
Limits | |
Fxp_width | |
Width based type names. | |
Data Structures | |
struct | mfrac |
16 bit fractional number in Q8.8 format. More... | |
struct | frac |
16 bit fractional number in Q1.15 format. More... | |
struct | dfrac |
32 bit fractional number in Q2.30 format. More... | |
struct | efrac |
32 bit fractional number in Q17.15 format. More... | |
Macros | |
#define | MFRAC_BIT (16) |
#define | FRAC_BIT (16) |
#define | DFRAC_BIT (32) |
#define | EFRAC_BIT (32) |
Typedefs | |
typedef int16_t | mfrac_base |
typedef int16_t | frac_base |
typedef int32_t | dfrac_base |
typedef int32_t | efrac_base |
This header depends only upon the definition of.
Unfortunately in C there is no way to forbid casting between integer types. While this would be inocuous in the case of integers, fractionals are different. For type casting to preserve the numerical value, the decimal point sould be aligned when converting between types. To prevent accidental implicit casts between fractional types we enclose them in structs. The downside of this approach is that it is no longer possible to use the '+' and '-' operators between fractionals, but in most cases you should not do that and use a double-length accumulator instead.
struct mfrac |
16 bit fractional number in Q8.8 format.
Values of this type can represent numbers between -128 and (2**7 - 2**-8), or approximately 127.996 with a precision of 2**-8 (about 0.0039 or 2.4 decimal places).
Data Fields | ||
---|---|---|
mfrac_base | v |
struct frac |
struct dfrac |
32 bit fractional number in Q2.30 format.
Values of this type can represent numbers between -2 and (2 - 2**-30) with a precision of 2**-30 (9.31E-10 or 9 decimal places)
A typical use of double fracs is for accumulators that store a multiply-sum of fracs.
Data Fields | ||
---|---|---|
dfrac_base | v |
struct efrac |
32 bit fractional number in Q17.15 format.
Values of this type can represent numbers between -65536 and (65536 - 2**-15) with a precision of 2**-15 (about 0.0000305 or 4.5 decimal places).
The main reason for defining efracs as Q17.15 is that casting fracs to (int32_t) and adding them naturally results in a 17.15 value.
Data Fields | ||
---|---|---|
efrac_base | v |
typedef int32_t dfrac_base |
typedef int32_t efrac_base |
typedef int16_t mfrac_base |