What are the purposes of fixed-point? When should it be used?
First ask yourself "what is the purpose of a floating point number?"
A floating point number enables one to store a wider range of
values in the same storage space compared to integeral numbers,
albeit with loss of accuracy.
The problem with using floating point numbers in computer
calculations is that, whatever your processor, it always takes
longer to operate on floting point numbers vis-a-vis integers.
This has nothing to do with the hardware but with the algorithm used.
e.g. Assuming decimal digits. To add 0.0100 E+2 and 0.0001 E+20 steps
smiliar to the following are executed in the processor.
1. Remove insignificant digits. Those are the zeroes next to the "."
The two numbers now become 0.1000 E+1 and 0.1000 E+17
2. Adjust the numbers so that both numbers have the same exponent
values, which should be that of the higher exponent value.
The two numbers now become 0.0000 E+17 and 0.1000 E+17
3. Add the mantissas, it is the mantissa of the result, use the
exponent of one of the numbers as the exponent of the result.
There are more stuff like overflow handling. But I hope you get some
idea of the complexity involved.
Fixed point number is like the values of items in your grocery receipt.
There are always fixed number of positions after the decimal point.
That is to say, the exponent value is always the same.
One can of course store these values as floating point numbers and
operate on them. But things can be speeded up a bit if one can
make use of the fact that the nunber of digits after decimal point is
always the same.
e.g. Instead of considering dollars just store all values in cents.
So, 1.23 become 123, 56.75 becomes 5675 etc. Then all the operations
are in integers which is much faster.
I read:
#define Int2Fixed(x) (((long)(short)x) << 16)
Looks to be nice code. If the rest of the program is like this then
the program code might be worth studying.
and the fixed-point in 16.16 format. Does the 16 in the MACRO refer to
integer or decimal part?
The 16 in the macro refers to the multiplication factor. Note that
shifting left by 16 bits is the same as multiplying by 2**16.
For example, if in 8.24, should the macro be:
#define Int2Fixed(x) (((long)(short)x) << 24)?
Yes, almost. It should be (((long)(byte)x) << 24) the raeson being
that the maximum value you can store in the integer part is only
8 bits, i.e. a byte. See below.
But note that the concept of the fixedpoint is only in the mind
of the programmer. The numbers are all integers. On input to the
program, the numbres are multiplied by such a constant value (2**24)
that the result is always integral for all input values.
Another question is about the casting here. What is actually happening when
doing casting like : (long)(short)x? Could someone elaborate this?
There are two type conversions, first x is convrted to (short) and
then it is converted to (long). The question is why the conversion to
(short) first? Why not just ((long)x) ?
The reason is that programmars use macros like functions. That is when
a programmer codes as follows:
y = Int2Fixed(x)
he is treating the macro like the following function:
long Int2Fixed (short x);
So, he is liable to pass a (long) or even (float) or(double) value for x
thinking that the function call will convert the values to the appropriate
type. As you know this is a macro invocation and not a function call.
So, the (short) in the "(long)(short)x" makes the macro act like a
function call in so far as the paramter conversion is concerned.
THIS IS GOOD CODING!
Hope I haven't confused you more than warranted.