Fixed Point
Saturday, November 14th, 2009Fixed point is used to represent a floating point number.
It could be used to speed up the multiplication and division operations on systems without a floating point processor.
Floating point is represented by scaling an integer.
In C/C++ one would shift the number by a scale…
const int SHIFT_AMT = 16; const int INTEGER_MASK = 0xFF; const int DECIMAL_MASK = 0xFF;
It is easiest for me to think of fixed point as a number multiplied by a scale.
I find it easier because it makes thinking about operations on the fixed point easier.
MATH
C/C++
typedef signed int fixedpoint; int i = 10; fixedpoint fp = i << SHIFT_AMT;
Addition and Subtraction are trivial with fixed point, Multiplication and Division are not...
Well... they are just not as trivial, there is one more step involved, scaling the result.
For Multiplication, you need to divide by the scale.
For Division, you need to multiply by the scale.
Multiplication
MATH
C/C++
fixedpoint number1 = 10 << SHIFT_AMT; fixedpoint number2 = 10 << SHIFT_AMT; fixedpoint result = (number1 * number2) >> SHIFT_AMT;
Division
MATH
C/C++
fixedpoint number1 = 10 << SHIFT_AMT; fixedpoint number2 = 10 << SHIFT_AMT; fixedpoint result = (number1 / number2) << SHIFT_AMT;
Other useful operations on fixed point could be:
Modulus.
Square-root.
Sine.
Cosine.