
Boost : 
From: Phil Endecott (spam_from_boost_dev_at_[hidden])
Date: 20070418 18:15:07
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 fixedpoint variable by specifying the number of
wholenumber bits and the number of fraction bits, e.g.
fixed<8,23> x;
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. 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
builtin integral types).
Implicit conversions are provided to and from the builtin integral and
floatingpoint 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.
Operations between fixedpoint values of different sizes are possible,
and return a fixedpoint 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 maximumvalue
case. There are no doubt more opportunities for refinement in this area.
You can see the code here:
http://svn.chezphil.org/libpbe/trunk/include/fixed.hh
Thanks for your attention.
Phil.
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk