Boost logo

Boost :

From: Andrew D Jewell (ajewell_at_[hidden])
Date: 1999-12-11 11:59:29

More on the multiplication-overflow topic.

In case a example was needed for my addition rant, with a four-byte
int, something as simple as the following won't work with the latest

(rational(1, 100000) + rational(1, 100000)) == rational(1, 50000)

as there is a temporary whose value is (100000 * 100000).

I believe that *= and /= should change

         num *= r.num;
         den *= r.den;


             Int gcd1 = gcd(num, r.den);
             Int gcd2 = gcd(r.num, den);
             num = (num / gcd1) * (r.num / gcd2);
             den = (den / gcd2) * (r.den / gcd1);

for similar reasons, and an almost identical tricks helps op<

On a related topic, I keep feeling that throwing an overflow
exception might be necessary since, more so than integer and floating
point types, rationals can overflow to nonsensical values in
non-obvious situations. Then again, the cost might be prohibitive.



Boost list run by bdawes at, gregod at, cpdaniel at, john at