Boost logo

Boost :

From: Michael Marcin (mmarcin_at_[hidden])
Date: 2007-04-18 18:39:22


Phil Endecott wrote:
> Dear All,
>
> I have recently written some code for fixed point arithmetic which I
> would be happy to contribute to Boost, if it is considered worthy.
> Please let me know if this is something that you would use, especially
> if you would be able to help with testing on platforms other than
> Linux
> and other tasks necessary for Boostification. Here's a very quick
> summary:
>
> You declare a fixed-point variable by specifying the number of
> whole-number bits and the number of fraction bits, e.g.
>
> fixed<8,23> x;
>

I have a very similar library that I've been developing/using for work for
some months.
I believe number of whole-number bits is commonly called magnitude bits.

> This has a range of about -256 to +256 with a precision of 1/(2^23),
> and occupies 32 bits. boost::integer is used to choose an
> implementation type with sufficient bits.

Interesting. How do you decide what type to promote to for fixed-point
multiply and divides. AFAIK Boost.Integer doesn't support 64-bit types
which forced me to roll my own solution.

> The usual arithmetic
> operators are implemented with some help from boost::operators, and
> should have no overhead; note that overflow is not detected (like the
> built-in integral types).

Platforms where binary fixed-point math is useful unfortunately tend to use
compilers that are a little dated. In particular EBO is missed by RVCT 2.2
in some cases which makes using Boost.Operators a no go for this type of
project IMO.

> Implicit conversions are provided to and from the built-in integral
> and floating-point types (well, actually I have only added the ones
> that I
> have needed; the others should only need copy&paste). Implicit
> conversion between fixed types of different sizes is also possible.
> I'm starting to think that perhaps I should make some of these
> conversion explicit: I worry that code using this might be doing an
> "invisible" conversion via a floating point type that I didn't want.
> Any thoughts about this would be appreciated.

These are way too dangerous. An explicit syntax is necessary.

>
> Operations between fixed-point values of different sizes are possible,
> and return a fixed-point value with sufficient bits to store the
> result. For example:
>
> fixed<10,4> a;
> fixed<15,2> b;
> fixed<15,4> c;
> c = a + b;
>
> ..except that actually one more bit is needed in the maximum-value
> case. There are no doubt more opportunities for refinement in this
> area.
>

Interesting. The only mixed-type arithmetic I support currently is
multiplication through a very cheesy expression template that delays the
calculation until it is assigned to a result type.

> You can see the code here:
>
> http://svn.chezphil.org/libpbe/trunk/include/fixed.hh
>

I'll check it out.

I think a Boost worthy implementation (which mine certainly isn't in its
present form) would require at least:
- optional overflow/underflow detection
- fixed-point math routines for the common standard library math functions.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk