Skip to content
This repository has been archived by the owner on Jan 25, 2020. It is now read-only.

Commit

Permalink
rational: validate values when constructed with AVRational
Browse files Browse the repository at this point in the history
`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.
  • Loading branch information
itsmattkc committed Jan 19, 2020
1 parent 649ee04 commit cd06b2f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
17 changes: 17 additions & 0 deletions app/common/rational.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ rational::rational(const AVRational &r) :
numer(r.num),
denom(r.den)
{
validateConstructor();
}

rational rational::fromDouble(const double &flt)
Expand Down Expand Up @@ -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)
Expand Down
15 changes: 3 additions & 12 deletions app/common/rational.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,7 @@ class rational
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;
Expand Down Expand Up @@ -117,6 +106,8 @@ class rational
intType numer;
intType denom;

void validateConstructor();

//Function: ensures denom >= 0
void fixSigns();
//Function: ensures lowest form
Expand Down

0 comments on commit cd06b2f

Please sign in to comment.