|
Boost : |
From: Stephen Silver (boost_at_[hidden])
Date: 2001-01-12 13:16:05
Nickolay Mladenov wrote:
> What is wrong with the negative reminders?
Good point, negative remainders are not really a problem.
> Shouldn't one just check the result(before return) for it's sign?
Yes, or just return abs(n). I've noticed that gcd(1,-1) currently
returns -1, so we can fix this bug at the same time:
template <typename IntType>
IntType gcd(IntType n, IntType m)
{
IntType r, zero(0);
while (m != zero) {
r = n % m;
n = m;
m = r;
}
using std::abs;
return abs(n);
}
r is best declared outside of the loop, otherwise it gets repeatedly
constructed and destructed.
Stephen
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk