Re: fixed-point

From: pout (pout_at_work.com)
Date: 04/10/04


Date: Sat, 10 Apr 2004 04:13:09 GMT

Malcolm <malcolm@55bank.freeserve.co.uk> wrote in message
news:c579ai$br7$1@news6.svr.pol.co.uk...
> "pout" <pout@work.com> wrote in message news:L4Edc.1914
> >
> > When you say "a certain range", do you mean the range that can be
> > represented by the format of fixed-point, like 16.16, its range is
> > 0000.0000 ~ FFFF.FFFF?
> >
> That's right. Fixed point can be signed or unsigned. If all your values
are
> in the range +- 30000 and you don't need greater precision than 0.0001 (1
in
> 10,000) then you can consider fixed point with 16:16 bits.
> > > Shall it be 8 bits instead of 16 in: #define Int2Fixed(x) (((long)
> > >(short)x) << 24)?
> > What exactly is accomplished by left-shifting 24 bits?
> >
> If we have the integer value 15 (0x0F) it becomes 0x000F000000 in the
fixed
> point representation.
> >
> > How to apply "Int2Fixed" and "Fixed2Int" in real calculation? For >
> example,
> >
> > int a, b;
> >
> > How to do addition, subtraction, multiplication and division with a
> > and b that involves "fixed-point" type?
> >
> To add or subtract two fixed point numbers, simply add or subtract them.
> Multipication is the tricky part. All values are scaled up by 16 bits, so
> you have to correct
>
> (a * b) >> 16;
>
> (or as you did it, calling the FixedToInt() macro).
> The problem is that on many compilers you will get overflow, since the
> product of two 32 bit numbers is a 64 bit number. This can be solved by a
> smidgeon of assembly, or sometimes by a judicious cast to long long.
> >
> > and in case of a/b,
> >
> > int d = Fixed2Int(Int2Fixed(a) / Int2Fixed(b));
> >
> There are two answers here.
> 1) You want the result as an integer. Simply divide two fixed point
numbers.
> 2) You wnat the result in fixed point format. You have to left shift the
> numerator before you do the divide
>
> x = (num << 16) / denom;
>

This is much clearer to me now.

According to what you said, whenever two numbers in division, to get a
result of fixed point, change either numerator or denominator to fixed point
format. Right?

After having the result in fixed point, will chaning the result back to int
make the number of fixed point lose its precision? Or make the conversion
to fixed point and back to int the same as a division of two ints?

Thanks!



Relevant Pages

  • [PATCH] new bitmap list format (for cpusets)
    ... A bitmap print and parse format that provides lists of ranges of ... The intention is to provide a format for the cpu and memory mask files ... extern int bitmap_parse(const char __user *ubuf, unsigned int ulen, ...
    (Linux-Kernel)
  • Re: [Lse-tech] [PATCH] new bitmap list format (for cpusets)
    ... came to the conclusion that the basic list format, ... So I redid the bitmap list format patch, ... A bitmap print and parse format that provides lists of ranges of ... extern int bitmap_parse(const char __user *ubuf, unsigned int ulen, ...
    (Linux-Kernel)
  • [PATCH] new bitmap list format (for cpusets)
    ... [This is a copy of the bitmap list format patch that I submitted to ... A bitmap print and parse format that provides lists of ranges of ... extern int bitmap_parse(const char __user *ubuf, unsigned int ulen, ...
    (Linux-Kernel)
  • [PATCH 02/11] tracing/events: nicer print format for parsing
    ... copy of the printk format used to output the data. ... MASK:= the bit mask to match ... char *name, int offset, int size, int is_signed); ... +#undef TP_FORMAT ...
    (Linux-Kernel)
  • Re: Using References to Formats, Examining Scalars With Devel::Peek
    ... the format to be able to refer to it by another name didn't occur to me. ... Seems most of the fields are the same as for the rest of the Perl ... from integer to double and how frequently the string portions are updated. ... Using an operator or a built-in with a double and an int usually converts ...
    (comp.lang.perl.misc)