Re: fixed-point

From: kal (k_amir7_at_yahoo.com)
Date: 04/10/04


Date: 10 Apr 2004 08:11:07 -0700


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



Relevant Pages

  • Re: Floating Point and C casts? (was: Re: typecast internals)
    ... You are mixing a discussion of integer and floating point storage ... What particular data conversion problem are you really ... formats), but these formats are not typically implemented off the VAX ... IEEE floating point conversions are detailed in the compiler manuals ...
    (comp.os.vms)
  • Re: converting float to double
    ... but this is also true of the integer math ... back conversion resulted in the original. ... of approximate inverse functions and what you have to do ... Doing it in floating point would be much more problematical. ...
    (comp.lang.c)
  • Re: converting float to double
    ... > Dik T. Winter wrote: ... floating point it is irrelevent. ... So it will not work for conversion of pre-euro valuta to euro valuta ... easier in the floating point case. ...
    (comp.lang.c)
  • Re: converting float to double
    ... but this is also true of the integer math ... > easier in the floating point case. ... >> when converting gulden to euro and the reverse did work, ... >> back conversion resulted in the original. ...
    (comp.lang.c)
  • Re: Translating binary files written on 64 bit machine on a 32 bit machine
    ... representation and supported value range of the floating point ... Write a program for the Sun/Linux platform which converts the ... Write a program for the Windows machine which reads this ... You may or may not still need to do a character set conversion ...
    (alt.comp.lang.learn.c-cpp)