From: Scott McMurray (me22.ca+boost_at_[hidden])
Date: 2008-08-01 11:06:56
On Fri, Aug 1, 2008 at 09:57, Neal Becker <ndbecker2_at_[hidden]> wrote:
> Here is an improved version of fixed-pt, incorporating some of the suggestions given here:
Have you considered using Boost.Integer to get the base types?
base_type = boost::int_t<int_bits+frac_bits>::least;
It would solve some of the multiplication overflow issues, too, if you
did the multiplication in a
boost::int_t<(int_bits+frac_bits)*2-1>::fast. Although of course
you'd still need some way of handling the other case when
boost::integer can't give you a type that big.
(Also, it looked like int_bits includes the sign, but that should be
made explicit somewhere.)
As for mod, I think that c=a%b, where all three have s frac_bits,
should be c*(1<<s)=((a*(1<<s))%(b*(1<<s))), which works out to
c.val=a.val%b.val, I think. On x86, I'd expect -2 % 1.5 to give me
-0.5, so with 2 frac bits, that's (-8 % 6)/4 => -2/4 => -0.5. Of
course, with the customizable rounding policy, it might have to give 1
instead, depending on how you do casts to int.