Re: rules of fixed-point arithmetic
- From: glen herrmannsfeldt <gah@xxxxxxxxxxxxxxxx>
- Date: Mon, 22 Sep 2008 20:24:01 -0800
Andrew Reilly wrote:
Well, it's true that I'd forgotten about the bizzaro-land of PL/I and ADA "fixed point", where the scale factors not only aren't constant for the arithmetic, but which don't even have to be powers of two.
Well, they are either powers of 2 or 10, as PL/I allows for
binary or decimal base. (Independent of the underlying representation.
I have used versions that do decimal scaling on binary numbers.)
Scale factor and base are known at compile time.
I'm certain that every so often that becomes important, but glancing at the web, it's mostly so that people can write papers about neat ways to make it perform a little better.
In particular, it makes a HELL of a difference whether you are assuming
two things, either or both of which may be false:
1) All fixed-point numbers have the same precision.
2) Division of two fixed-point numbers is not something that you
have to worry about - either you don't need it, or its exact result
isn't critical.
I assumed those things because both are true in the vast bulk of the places where fixed point is actually *used*, as opposed to being worried about.
For PL/I the first is probably true for many variables
involved in a calculation, but constants have the precision
and scale factor as written, good or bad.
(snip)
Yes, division of fixed point numbers is a pain, since, as you point out, you have to deal with results that may not fit neatly into the range of your data. Whenever possible, the answer is to just not divide. When division is essential, you have to deal with it. One of the more convenient ways to do that is to define a division operator as x/(y*M), where M is a power-of-two scale factor that you select to ensure that your result is a fraction in the appropriate range: trading precision and range. I think that the only fixed point arithmetic where division isn't much of a pain is one where the number of bits devoted to integer values is the same as the number devoted to fractions, such as the 16.16 format that Knuth used in TeX.
PL/I provides the DIVIDE built-in function to specify the
desired quotient precision and scale. Some say that should
be the only way to divide.
-- glen
.
- References:
- rules of fixed-point arithmetic
- From: Sam Green
- Re: rules of fixed-point arithmetic
- From: Andrew Reilly
- Re: rules of fixed-point arithmetic
- From: Nick Maclaren
- Re: rules of fixed-point arithmetic
- From: Andrew Reilly
- Re: rules of fixed-point arithmetic
- From: Nick Maclaren
- Re: rules of fixed-point arithmetic
- From: Andrew Reilly
- rules of fixed-point arithmetic
- Prev by Date: Re: rules of fixed-point arithmetic
- Next by Date: Re: rules of fixed-point arithmetic
- Previous by thread: Re: rules of fixed-point arithmetic
- Next by thread: Re: rules of fixed-point arithmetic
- Index(es):
Relevant Pages
|