replace custom gcd function with std

Signed-off-by: Rosen Penev <rosenp@gmail.com>
main
Rosen Penev 3 years ago
parent 7a96867f31
commit 10a62b2350

@ -506,45 +506,6 @@ T stringTo(const std::string& s, bool& ok) {
template <>
bool stringTo<bool>(const std::string& s, bool& ok);
/*!
@brief Return the greatest common denominator of n and m.
(Implementation from Boost rational.hpp)
@note We use n and m as temporaries in this function, so there is no
value in using const IntType& as we would only need to make a copy
anyway...
*/
template <typename IntType>
IntType gcd(IntType n, IntType m) {
// Avoid repeated construction
IntType zero(0);
// This is abs() - given the existence of broken compilers with Koenig
// lookup issues and other problems, I code this explicitly. (Remember,
// IntType may be a user-defined type).
if (n < zero) {
if (n == std::numeric_limits<IntType>::min()) {
n = std::numeric_limits<IntType>::max();
} else {
n = -n;
}
}
if (m < zero)
m = -m;
// As n and m are now positive, we can be sure that %= returns a
// positive value (the standard guarantees this for built-in types,
// and we require it of user-defined types).
for (;;) {
if (m == zero)
return n;
n %= m;
if (n == zero)
return m;
m %= n;
}
}
} // namespace Exiv2
#endif // #ifndef TYPES_HPP_

@ -22,6 +22,7 @@
#include <algorithm>
#include <cmath>
#include <numeric>
// *****************************************************************************
// local declarations
@ -2807,7 +2808,7 @@ std::ostream& print0x9204(std::ostream& os, const Value& value, const ExifData*)
} else if (bias.second <= 0) {
os << "(" << bias.first << "/" << bias.second << ")";
} else {
int32_t d = gcd(bias.first, bias.second);
int32_t d = std::gcd(bias.first, bias.second);
int32_t num = std::abs(bias.first) / d;
int32_t den = bias.second / d;
os << (bias.first < 0 ? "-" : "+") << num;

@ -15,6 +15,7 @@
#include <cmath>
#include <cstring>
#include <iomanip>
#include <numeric>
#include <sstream>
#include <utility>
@ -629,7 +630,7 @@ Rational floatToRationalCast(float f) {
den = 10000;
}
const auto nom = static_cast<int32_t>(std::round(d * den));
const int32_t g = gcd(nom, den);
const int32_t g = std::gcd(nom, den);
return {nom / g, den / g};
}

Loading…
Cancel
Save