Boost logo

Boost :

From: Moore, Paul (Paul.Moore_at_[hidden])
Date: 1999-12-14 10:51:25

From: Andrew D Jewell [mailto:ajewell_at_[hidden]]
> Int gcd1 = gcd(num, r.den);
> Int gcd2 = gcd(r.num, den);
> num = (num / gcd1) * (r.num / gcd2);
> den = (den / gcd2) * (r.den / gcd1);

I'm just putting this into practice, and I wanted to check:

Is it *required* for a compiler to implement

    (n/g) * x

by dividing n/g first, before multiplying by x? If compilers are allowed to
reorder calculations, and do (n*x)/g, then we can't be sure that this
formulation avoids overflow, and we *need* to introduce explicit
temporaries. I'm not 100% sure what the (C as well as C++, presumably)
standard says on this matter.

I'd rather avoid explicit temporaries, as they would obscure the code. But
do I need them?


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