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.
This commit is contained in:
itsmattkc
2020-06-01 17:44:42 +10:00
parent 644fb7cec8
commit 1301d4b9cf
2 changed files with 26 additions and 3 deletions
+24
View File
@@ -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));
}
+2 -3
View File
@@ -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);