Merge branch 'master' of git.ucc.asn.au:/ipdf/code
[ipdf/code.git] / src / rational.h
index 41cce09..b019505 100644 (file)
@@ -8,17 +8,34 @@
 #include "common.h"
 #include <cmath>
 #include <cassert>
+#include "arbint.h"
+#include "gmpint.h"
+#include <climits>
+#include <values.h>
 
 namespace IPDF
 {
+       
+template <class T> T Tabs(const T & a)
+{
+       return abs(a);
+}
+template <> Arbint Tabs(const Arbint & a);
+template <> Gmpint Tabs(const Gmpint & a);
 
 /* Recursive version  of GCD
 template <class T>
 T gcd(const T & a, const T & b)
 {
-       if (a == 1 || a == 0) return 1;
-       if (b == 0) return a;
-       if (b == a) return a;
+       Debug("Called on %li/%li", int64_t(a), int64_t(b));
+       if (a == T(1) || a == T(0)) return T(1);
+       if (b == T(0)) return a;
+       if (b == a) 
+       {
+               Debug("Equal!");
+               return a;
+       }
+       Debug("Not equal!");
        
        if (a > b) return gcd(a-b,b);
        return gcd(a, b-a);
@@ -29,6 +46,8 @@ T gcd(const T & a, const T & b)
 template <class T>
 T gcd(const T & p, const T & q)
 {
+
+
        T g(1);
        T big(p);
        T small(q);
@@ -37,15 +56,18 @@ 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))
        {
+               //Debug("big = %li, small = %li", int64_t(big), int64_t(small));
                big = small;
                small = g;
+               //Debug("Loop %u", ++count);
        }
        return small;
-}       
+}      
+
 
 template <class T = int64_t>
 struct Rational
@@ -54,7 +76,6 @@ struct Rational
        Rational(double d=0) : P(d*1e6), Q(1e6) // Possibly the worst thing ever...
        {
                Simplify();
-               CheckAccuracy(d, "Construct from double");
        }
 
        Rational(const T & _P, const T & _Q) : P(_P), Q(_Q)
@@ -69,17 +90,23 @@ struct Rational
 
        void Simplify()
        {
-               if (Q < 0
+               if (Q < T(0)
                {
-                       P = (P < 0) ? -P : P;
+                       P = -P;
                        Q = -Q;
                }
-               if (P == 0)
+               if (P == T(0))
+               {
+                       Q = T(1);
+                       return;
+               }
+               if (P == Q)
                {
-                       Q = 1;
+                       P = Q = T(1);
                        return;
                }
-               T g = gcd(llabs(P),llabs(Q));
+               T g = gcd(Tabs(P), Tabs(Q));
+               //Debug("Got gcd!");
                P /= g;
                Q /= g;
        }
@@ -99,32 +126,35 @@ struct Rational
 
        Rational operator+(const Rational & r) const 
        {
-               Rational result = (r.P == 0) ? Rational(P,Q) : Rational(P*r.Q + r.P*Q, Q*r.Q);
-               result.CheckAccuracy(ToDouble() + r.ToDouble(),"+");
+               Rational result = (r.P == T(0)) ? Rational(P,Q) : Rational(P*r.Q + r.P*Q, Q*r.Q);
+               //if (!result.CheckAccuracy(ToDouble() * r.ToDouble(),"+"))
+               //{
+               //      Debug("This is %s (%f) and r is %s (%f)", Str().c_str(), ToDouble(), r.Str().c_str(), 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);
-               result.CheckAccuracy(ToDouble() - r.ToDouble(),"-");
+               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);
-               if (!result.CheckAccuracy(ToDouble() * r.ToDouble(),"*"))
-               {
-                       Debug("This is %s (%f) and r is %s (%f)", Str().c_str(), ToDouble(), r.Str().c_str(), r.ToDouble());
-               }
+               //if (!result.CheckAccuracy(ToDouble() * r.ToDouble(),"*"))
+               //{
+               //      Debug("This is %s (%f) and r is %s (%f)", Str().c_str(), ToDouble(), r.Str().c_str(), r.ToDouble());
+               //}
                return result;
        }
        Rational operator/(const Rational & r) const 
        {
                Rational result(P * r.Q, Q*r.P);
-               if (!result.CheckAccuracy(ToDouble() / r.ToDouble(),"/"))
-               {
-                       Debug("This is %s (%f) and r is %s (%f)", Str().c_str(), ToDouble(), r.Str().c_str(), r.ToDouble());
-               }
+               //if (!result.CheckAccuracy(ToDouble() / r.ToDouble(),"/"))
+               //{
+               //      Debug("This is %s (%f) and r is %s (%f)", Str().c_str(), ToDouble(), r.Str().c_str(), r.ToDouble());
+               //}
                return result;
        }       
 
@@ -134,19 +164,31 @@ struct Rational
        //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) {P = r.P; Q = r.Q; return *this;}
+       Rational operator-() const {Rational r(*this); r.P = -r.P; return r;}
+       Rational & operator=(const Rational & r) {P = r.P; Q = r.Q; Simplify(); return *this;}
        Rational & operator+=(const Rational & r) {this->operator=(*this+r); return *this;}
        Rational & operator-=(const Rational & r) {this->operator=(*this-r); return *this;}
        Rational & operator*=(const Rational & r) {this->operator=(*this*r); return *this;}
        Rational & operator/=(const Rational & r) {this->operator=(*this/r); return *this;}
 
-       double ToDouble() const {return (double)(P) / (double)(Q);}
+       double ToDouble() const 
+       {
+               T num = P, denom = Q;
+               while (Tabs(num) > T(DBL_MAX))
+               {
+                       num /= T(16);
+                       denom /= T(16);
+               }
+               return ((double)(num))/((double)(denom));
+       }
        bool CheckAccuracy(double d, const char * msg, double threshold = 1e-3) const
        {
-               double result = fabs(ToDouble() - d) / d;
+               double result = fabs(ToDouble() - d);
+               if (d != 0e0) result /= d;
                if (result > threshold)
                {
                        Warn("(%s) : Rational %s (%f) is not close enough at representing %f (%f vs %f)", msg, Str().c_str(), ToDouble(), d, result, threshold);
+                       Backtrace();
                        return false;
                }
                return true;
@@ -154,7 +196,7 @@ struct Rational
        std::string Str() const
        {
                std::stringstream s;
-               s << (int64_t)P << "/" << (int64_t)Q;
+               s << int64_t(P) << "/" << int64_t(Q);
                return s.str();
        }
        
@@ -162,13 +204,8 @@ struct Rational
        T Q;
 };
 
-inline Rational<int64_t> pow(const Rational<int64_t> & a, const Rational<int64_t> & b)
-{
-       //TODO:Implement properly
-       int64_t P = std::pow((double)a.P, b.ToDouble());
-       int64_t Q = std::pow((double)a.Q, b.ToDouble());
-       return Rational<int64_t>(P, Q);
-}
+
+
 
 
 }

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