From 1301d4b9cf9900b6d2e68f0e1ab04b46b0eeb82e Mon Sep 17 00:00:00 2001 From: itsmattkc Date: Mon, 1 Jun 2020 17:44:42 +1000 Subject: [PATCH] rational: use true min/max values for extended range Will address issues caused by comparisons to our MIN/MAX constants when a numerator/denominator is > INT32_MAX/MIN. --- app/common/rational.cpp | 24 ++++++++++++++++++++++++ app/common/rational.h | 5 ++--- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/app/common/rational.cpp b/app/common/rational.cpp index a30b6f25d..6fa8206ef 100644 --- a/app/common/rational.cpp +++ b/app/common/rational.cpp @@ -229,6 +229,18 @@ bool rational::operator<(const rational &rhs) const return false; } + if (rhs == RATIONAL_MAX + || *this == RATIONAL_MIN) { + // We will always wither be LESS THAN (true) or EQUAL (false) + return (*this != rhs); + } + + if (*this == RATIONAL_MAX + || rhs == RATIONAL_MIN) { + // We will always be GREATER THAN (false) or EQUAL (false) + return false; + } + if (!isNull() && rhs.isNull()) { return (numer_ * denom_ < intType(0)); } @@ -246,6 +258,18 @@ bool rational::operator<=(const rational &rhs) const return true; } + if (rhs == RATIONAL_MAX + || *this == RATIONAL_MIN) { + // We will always wither be LESS THAN (true) or EQUAL (true) + return true; + } + + if (*this == RATIONAL_MAX + || rhs == RATIONAL_MIN) { + // We will always be GREATER THAN (false) or EQUAL (true) + return rhs == *this; + } + if (!isNull() && rhs.isNull()) { return (numer_ * denom_ < intType(0)); } diff --git a/app/common/rational.h b/app/common/rational.h index dd9ddcf2e..9cd3d861a 100644 --- a/app/common/rational.h +++ b/app/common/rational.h @@ -127,9 +127,8 @@ private: static intType gcd(const intType &x, const intType &y); }; -// We define these limits at 32-bit to try avoiding integer overflow -#define RATIONAL_MIN rational(INT32_MIN, 1) -#define RATIONAL_MAX rational(INT32_MAX, 1) +#define RATIONAL_MIN rational(INT64_MIN, 1) +#define RATIONAL_MAX rational(INT64_MAX, 1) uint qHash(const rational& r, uint seed);