rational: validate values when constructed with AVRational
`rational` is supposed to "fix signs" and "reduce" whenever its values are set, but I neglected to do this when its values were set by an AVRational. Now it will do so on both.
This commit is contained in:
@@ -7,6 +7,7 @@ rational::rational(const AVRational &r) :
|
||||
numer(r.num),
|
||||
denom(r.den)
|
||||
{
|
||||
validateConstructor();
|
||||
}
|
||||
|
||||
rational rational::fromDouble(const double &flt)
|
||||
@@ -136,6 +137,22 @@ QString rational::toString() const
|
||||
return QStringLiteral("%1/%2").arg(QString::number(numer), QString::number(denom));
|
||||
}
|
||||
|
||||
void rational::validateConstructor()
|
||||
{
|
||||
if(denom != intType(0))
|
||||
{
|
||||
if(numer != intType(0))
|
||||
{
|
||||
fixSigns();
|
||||
reduce();
|
||||
}
|
||||
else
|
||||
denom = intType(0);
|
||||
}
|
||||
else
|
||||
numer = intType(0);
|
||||
}
|
||||
|
||||
//Assignment Operators
|
||||
|
||||
const rational& rational::operator=(const rational &rhs)
|
||||
|
||||
+3
-12
@@ -37,18 +37,7 @@ public:
|
||||
rational(const intType &numerator, const intType &denominator)
|
||||
:numer(numerator), denom(denominator)
|
||||
{
|
||||
if(denom != intType(0))
|
||||
{
|
||||
if(numer != intType(0))
|
||||
{
|
||||
fixSigns();
|
||||
reduce();
|
||||
}
|
||||
else
|
||||
denom = intType(0);
|
||||
}
|
||||
else
|
||||
numer = intType(0);
|
||||
validateConstructor();
|
||||
}
|
||||
|
||||
rational(const rational &rhs) = default;
|
||||
@@ -117,6 +106,8 @@ private:
|
||||
intType numer;
|
||||
intType denom;
|
||||
|
||||
void validateConstructor();
|
||||
|
||||
//Function: ensures denom >= 0
|
||||
void fixSigns();
|
||||
//Function: ensures lowest form
|
||||
|
||||
Reference in New Issue
Block a user