
"Peter Dimov" <pdimov@mmltd.net> wrote
Andy Little wrote:
For a 16bit int this leaves you with a safe range of +-127 in this case.Of course after 'normalisation' referred to in the boost::rational docs, the result of the addition itself may or may not be out of range.
True, but if your computations do not consist of a single addition, you'll see you "conservative" range shrinking down rapidly to [1, 1] for the denominator. ;-)
The result may be out of range, but nevertheless this can be checked. To automate this one could create a rational<int_type, tri_state range_checked> where range_checked = false on a temporary. (Then += etc must be implemented in terms of *this = check(*this+in)). Only allowing operations on types where both operands have range_checked== true. Users could find their way round the inconvenience by using rational<int_type, dont_care>.
The real answer is that rational addition is rational-overflow-safe (as opposed to ordinary integer-overflow-safe) if Di == Dj (or the equivalent where all D's divide some Di). That is, when I could have used integer arithmetic instead of rational.
However these special case properties of the denominators need to be checked, and integer arithmetic may also overflow (bigger values than int common), so one is back to the problem of 'seemingly-random' combinations of values that will work. regards Andy Little