Fix operator logic.

This commit is contained in:
Chris--A 2015-03-05 17:42:23 +10:00
parent 35310f0596
commit 675ddb0f84
2 changed files with 32 additions and 28 deletions

View File

@ -128,32 +128,32 @@ size_t Fraction::printTo(Print& p) const
};
// EQUALITIES
bool Fraction::operator == (Fraction c)
bool Fraction::operator == (const Fraction &c)
{
return (n * c.d) == (d * c.n);
}
bool Fraction::operator != (Fraction c)
bool Fraction::operator != (const Fraction &c)
{
return (n * c.d) != (d * c.n);
}
bool Fraction::operator > (Fraction c)
bool Fraction::operator > (const Fraction &c)
{
return (n * c.d) > (d * c.n);
}
bool Fraction::operator >= (Fraction c)
bool Fraction::operator >= (const Fraction &c)
{
return (n * c.d) >= (d * c.n);
}
bool Fraction::operator < (Fraction c)
bool Fraction::operator < (const Fraction &c)
{
return (n * c.d) < (d * c.n);
}
bool Fraction::operator <= (Fraction c)
bool Fraction::operator <= (const Fraction &c)
{
return (n * c.d) <= (d * c.n);
}
@ -165,7 +165,7 @@ Fraction Fraction::operator - ()
}
// BASIC MATH
Fraction Fraction::operator + (Fraction c)
Fraction Fraction::operator + (const Fraction &c)
{
if (d == c.d)
{
@ -174,7 +174,7 @@ Fraction Fraction::operator + (Fraction c)
return Fraction(n*c.d + c.n*d, d * c.d);
}
Fraction Fraction::operator - (Fraction c)
Fraction Fraction::operator - (const Fraction &c)
{
if (d == c.d)
{
@ -183,18 +183,18 @@ Fraction Fraction::operator - (Fraction c)
return Fraction(n*c.d - c.n*d, d * c.d);
}
Fraction Fraction::operator * (Fraction c)
Fraction Fraction::operator * (const Fraction &c)
{
return Fraction(n * c.n, d * c.d);
}
Fraction Fraction::operator / (Fraction c)
Fraction Fraction::operator / (const Fraction &c)
{
// division by zero returns 0
return Fraction(n * c.d, d * c.n);
}
void Fraction::operator += (Fraction c)
Fraction& Fraction::operator += (const Fraction &c)
{
if (d == c.d)
{
@ -206,9 +206,10 @@ void Fraction::operator += (Fraction c)
d *= c.d;
}
simplify();
return *this;
}
void Fraction::operator -= (Fraction c)
Fraction& Fraction::operator -= (const Fraction &c)
{
if (d == c.d)
{
@ -220,21 +221,24 @@ void Fraction::operator -= (Fraction c)
d *= c.d;
}
simplify();
return *this;
}
void Fraction::operator *= (Fraction c)
Fraction& Fraction::operator *= (const Fraction &c)
{
n *= c.n;
d *= c.d;
simplify();
return *this;
}
void Fraction::operator /= (Fraction c)
Fraction& Fraction::operator /= (const Fraction &c)
{
// division by zero returns 0
n *= c.d;
d *= c.n;
simplify();
return *this;
}
double Fraction::toDouble()

View File

@ -31,26 +31,26 @@ public:
size_t printTo(Print& p) const;
// equalities
bool operator == (Fraction);
bool operator != (Fraction);
bool operator > (Fraction);
bool operator >= (Fraction);
bool operator < (Fraction);
bool operator <= (Fraction);
bool operator == (const Fraction&);
bool operator != (const Fraction&);
bool operator > (const Fraction&);
bool operator >= (const Fraction&);
bool operator < (const Fraction&);
bool operator <= (const Fraction&);
// negation
Fraction operator - ();
// basic math
Fraction operator + (Fraction);
Fraction operator - (Fraction);
Fraction operator * (Fraction);
Fraction operator / (Fraction);
Fraction operator + (const Fraction&);
Fraction operator - (const Fraction&);
Fraction operator * (const Fraction&);
Fraction operator / (const Fraction&);
void operator += (Fraction);
void operator -= (Fraction);
void operator *= (Fraction);
void operator /= (Fraction);
Fraction& operator += (const Fraction&);
Fraction& operator -= (const Fraction&);
Fraction& operator *= (const Fraction&);
Fraction& operator /= (const Fraction&);
double toDouble();
bool isProper(); // abs(f) < 1