# Boost :

From: Manuel Menezes de Sequeira (Manuel.Sequeira_at_[hidden])
Date: 2005-04-02 11:23:33

Andy Little wrote:
> [...]
> boost::rational uses a more complicated algorithm for addition, but
> nevertheless the above limit algoritm(which might be improved upon of
> course) holds I think.
> [...]

Two years ago I proposed an even more complicated algorithm for addition
which further reduces the number of cases where integer overflow occurs.
You can find it in
http://sourceforge.net/tracker/index.php?func=detail&aid=519635&group_id=7586&atid=107586

I really don't know whether the rational library is useful. I proposed
it as an exercise to my students, and it was for that reason that I came
across the addition algorithm I proposed in 2002.

In any case:
a) Using integer traits, it should be easy to distinguish between
limited and unlimited range integers. Hence, when rational is used with
unlimited range integers, simpler algorithms may be selected at compile
time.
b) If limited range integers are used, then either rounding is used or
not. It seems reasonable to leave the choice to the user of the class.
Using policies should be a simple solution.
c) For the cases of limited range integers without rounding:
i) It makes sense, in my opinion, to use algorithms which minimize the
number of cases where integer overflow occurs. I don't like the current
solution, since it only makes a half-hearted attempt at this, for of
efficiency reasons.
ii) It is certainly possible to provide the class with static functions
for checking whether overflow would occur for each operation. This
would make assertion based error checking possible. Whether this is
really appropriate I don't know.
d) A possibly useful change to the class would be to add representations
for plus and minus infinity, as well as "not a number": 1/0, -1/0 and
0/0. I think an algebra including these extra values is pretty easy to
define.