From fb585413a5a9cf57157725f1ca23ed1bd2c2e891 Mon Sep 17 00:00:00 2001 From: itsmattkc Date: Fri, 11 Oct 2019 09:35:28 +1100 Subject: [PATCH] fixed bug in rational that caused integer overflows --- app/common/rational.cpp | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/app/common/rational.cpp b/app/common/rational.cpp index d6db8260c..1e5eec400 100644 --- a/app/common/rational.cpp +++ b/app/common/rational.cpp @@ -43,6 +43,11 @@ void rational::fixSigns() void rational::reduce() { + // Euclidean often fails if numbers are negative, we abs it and re-neg it later if necessary + bool neg = numer < 0; + + numer = qAbs(numer); + intType d = 1; if(denom != 0 && numer !=0) @@ -53,6 +58,10 @@ void rational::reduce() numer /= d; denom /= d; } + + if (neg) { + numer = -numer; + } } //Function: finds greatest common denominator @@ -424,5 +433,5 @@ istream& operator>>(istream &in, rational &value) QDebug operator<<(QDebug debug, const rational &r) { debug.nospace() << r.numerator() << "/" << r.denominator(); - return debug.maybeSpace(); + return debug.space(); }