X-Git-Url: https://git.ucc.asn.au/?p=ipdf%2Fcode.git;a=blobdiff_plain;f=src%2Frational.h;h=9fe8f1f5f44b0e45b2b0111274165e8486644b85;hp=c2215a087ccc32a729ab958e4b112238bd13825a;hb=6ad7fc3c833837713405b64a50e62dd128cb7a30;hpb=e7e4a7ad5c977ae089bab7081e7931fd5e423cc1 diff --git a/src/rational.h b/src/rational.h index c2215a0..9fe8f1f 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"); @@ -71,10 +71,14 @@ struct Rational { if (Q < 0) { - P = (P < 0) ? -P : P; + 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());}