// Set this to the number of desired binary digits to the right of the radix.
#define NUM_FRACT_BIN_DIGITS 8
// The Fixed Point data type, which must be a fundamental type (e.g., int, short, long, etc.).
typedef int FIXEDPOINT;
// Convert float value to fixed point.
FIXEDPOINT FloatToFixed( float val )
{
return (FIXEDPOINT)( val * ( 1 << NUM_FRACT_BIN_DIGITS ) );
}
// Convert fixed point value to float.
float FixedToFloat( FIXEDPOINT val )
{
return (float)val / ( 1 << NUM_FRACT_BIN_DIGITS );
}
// Extract the whole part of a fixed point value.
int IntegerPart( FIXEDPOINT val )
{
return val >> NUM_FRACT_BIN_DIGITS;
}
// Extract the fractional part of a fixed point value.
int FractionalPart( FIXEDPOINT val )
{
return val & ( ( 1 << NUM_FRACT_BIN_DIGITS ) - 1 );
}