X-Git-Url: https://git.ucc.asn.au/?p=ipdf%2Fcode.git;a=blobdiff_plain;f=src%2Frational.h;fp=src%2Frational.h;h=41cce09078b4e96eb17a6bb6e28478ea64637b57;hp=c2215a087ccc32a729ab958e4b112238bd13825a;hb=00ef152dc3a065e37fe45c1cd8023d739f518b8e;hpb=e7e4a7ad5c977ae089bab7081e7931fd5e423cc1 diff --git a/src/rational.h b/src/rational.h index c2215a0..41cce09 100644 --- a/src/rational.h +++ b/src/rational.h @@ -51,7 +51,7 @@ template struct Rational { /** Construct from a double.**/ - Rational(double d = 0) : P(d*1e6), Q(1e6) // Possibly the worst thing ever... + Rational(double d=0) : P(d*1e6), Q(1e6) // Possibly the worst thing ever... { Simplify(); CheckAccuracy(d, "Construct from double"); @@ -74,7 +74,11 @@ struct Rational P = (P < 0) ? -P : P; Q = -Q; } - + if (P == 0) + { + Q = 1; + return; + } T g = gcd(llabs(P),llabs(Q)); P /= g; Q /= g; @@ -93,9 +97,6 @@ struct Rational bool operator>=(const Rational & r) const {return *this == r || *this > r;} bool operator!=(const Rational & r) const {return !(*this == r);} - - - /* Rational operator+(const Rational & r) const { Rational result = (r.P == 0) ? Rational(P,Q) : Rational(P*r.Q + r.P*Q, Q*r.Q); @@ -108,7 +109,6 @@ struct Rational result.CheckAccuracy(ToDouble() - r.ToDouble(),"-"); return result; } - */ Rational operator*(const Rational & r) const { Rational result(P * r.P, Q * r.Q); @@ -128,8 +128,9 @@ struct Rational return result; } - Rational operator+(const Rational & r) const {return Rational(ToDouble()+r.ToDouble());} - Rational operator-(const Rational & r) const {return Rational(ToDouble()-r.ToDouble());} + /** To cheat, use these **/ + //Rational operator+(const Rational & r) const {return Rational(ToDouble()+r.ToDouble());} + //Rational operator-(const Rational & r) const {return Rational(ToDouble()-r.ToDouble());} //Rational operator*(const Rational & r) const {return Rational(ToDouble()*r.ToDouble());} //Rational operator/(const Rational & r) const {return Rational(ToDouble()/r.ToDouble());}