Fixed Point Arithmetic  0
Fixed-point math library.
Modules | Data Structures | Macros | Typedefs
Fixed point numbers

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
 

Detailed Description

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.


Data Structure Documentation

◆ mfrac

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

Definition at line 76 of file types.h.

Data Fields
mfrac_base v

◆ frac

struct frac

16 bit fractional number in Q1.15 format.

Values of this type can represent numbers between -1 and (1 - 2**-15) with a precision of 2**-15 (about 0.0000305 or 4.5 decimal places).

Definition at line 86 of file types.h.

Data Fields
frac_base v

◆ dfrac

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.

Definition at line 99 of file types.h.

Data Fields
dfrac_base v

◆ efrac

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.

Definition at line 112 of file types.h.

Data Fields
efrac_base v

Macro Definition Documentation

◆ DFRAC_BIT

#define DFRAC_BIT   (32)

Size in bits of a DFRAC.

Definition at line 145 of file types.h.

◆ EFRAC_BIT

#define EFRAC_BIT   (32)

Size in bits of an EFRAC.

Definition at line 146 of file types.h.

◆ FRAC_BIT

#define FRAC_BIT   (16)

Size in bits of a FRAC.

Definition at line 144 of file types.h.

◆ MFRAC_BIT

#define MFRAC_BIT   (16)

Size in bits of a FRAC.

Definition at line 143 of file types.h.

Typedef Documentation

◆ dfrac_base

typedef int32_t dfrac_base

Base arithmetic type for dfrac.

Definition at line 66 of file types.h.

◆ efrac_base

typedef int32_t efrac_base

Base arithmetic type for efrac.

Definition at line 67 of file types.h.

◆ frac_base

typedef int16_t frac_base

Base arithmetic type for frac.

Definition at line 65 of file types.h.

◆ mfrac_base

typedef int16_t mfrac_base

Base arithmetic type for mfrac.

Definition at line 64 of file types.h.