It compiles... and runs with FPS of zero
[ipdf/code.git] / src / rational.h
index c2215a0..ce1a767 100644 (file)
@@ -37,9 +37,9 @@ T gcd(const T & p, const T & q)
                big = q;
                small = p;
        }
-       if (small == 0)
+       if (small == T(0))
                return g;
-       while ((g = big % small) > 0)
+       while ((g = big % small) > T(0))
        {
                big = small;
                small = g;
@@ -51,7 +51,7 @@ template <class T = int64_t>
 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");
@@ -69,13 +69,17 @@ struct Rational
 
        void Simplify()
        {
-               if (Q < 0
+               if (Q < T(0)
                {
-                       P = (P < 0) ? -P : P;
+                       P = -P;
                        Q = -Q;
                }
-
-               T g = gcd(llabs(P),llabs(Q));
+               if (P == T(0))
+               {
+                       Q = T(1);
+                       return;
+               }
+               T g = gcd(T(llabs(P)),T(llabs(Q)));
                P /= g;
                Q /= g;
        }
@@ -93,22 +97,18 @@ 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);
+               Rational result = (r.P == T(0)) ? Rational(P,Q) : Rational(P*r.Q + r.P*Q, Q*r.Q);
                result.CheckAccuracy(ToDouble() + r.ToDouble(),"+");
                return result;
        }
        Rational operator-(const Rational & r) const 
        {
-               Rational result = (r.P == 0) ? Rational(P,Q) : Rational(P*r.Q - r.P*Q, Q*r.Q);
+               Rational result = (r.P == T(0)) ? Rational(P,Q) : Rational(P*r.Q - r.P*Q, Q*r.Q);
                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());}
 
@@ -170,6 +171,7 @@ inline Rational<int64_t> pow(const Rational<int64_t> & a, const Rational<int64_t
 }
 
 
+
 }
 
 #endif //_RATIONAL_H

UCC git Repository :: git.ucc.asn.au