Merge branch 'master' of git.ucc.asn.au:/ipdf/code
[ipdf/code.git] / src / rational.h
1 #ifndef _RATIONAL_H
2 #define _RATIONAL_H
3
4 /**
5  * A really shoddy implementation of Rational numbers
6  */
7
8 #include "common.h"
9 #include <cmath>
10 #include <cassert>
11 #include "arbint.h"
12 #include "gmpint.h"
13 #include <climits>
14 #include <values.h>
15
16 namespace IPDF
17 {
18         
19 template <class T> T Tabs(const T & a)
20 {
21         return abs(a);
22 }
23 template <> Arbint Tabs(const Arbint & a);
24 template <> Gmpint Tabs(const Gmpint & a);
25
26 /* Recursive version  of GCD
27 template <class T>
28 T gcd(const T & a, const T & b)
29 {
30         Debug("Called on %li/%li", int64_t(a), int64_t(b));
31         if (a == T(1) || a == T(0)) return T(1);
32         if (b == T(0)) return a;
33         if (b == a) 
34         {
35                 Debug("Equal!");
36                 return a;
37         }
38         Debug("Not equal!");
39         
40         if (a > b) return gcd(a-b,b);
41         return gcd(a, b-a);
42 }
43 */
44
45 /** Greatest Common Divisor of p and q **/
46 template <class T>
47 T gcd(const T & p, const T & q)
48 {
49
50
51         T g(1);
52         T big(p);
53         T small(q);
54         if (p < q)
55         {
56                 big = q;
57                 small = p;
58         }
59         if (small == T(0))
60                 return g;
61         while ((g = big % small) > T(0))
62         {
63                 //Debug("big = %li, small = %li", int64_t(big), int64_t(small));
64                 big = small;
65                 small = g;
66                 //Debug("Loop %u", ++count);
67         }
68         return small;
69 }       
70
71
72 template <class T = int64_t>
73 struct Rational
74 {
75         /** Construct from a double.**/
76         Rational(double d=0) : P(d*1e6), Q(1e6) // Possibly the worst thing ever...
77         {
78                 Simplify();
79         }
80
81         Rational(const T & _P, const T & _Q) : P(_P), Q(_Q)
82         {
83                 Simplify();
84         }
85
86         Rational(const Rational & cpy) : P(cpy.P), Q(cpy.Q)
87         {
88                 Simplify();
89         }
90
91         void Simplify()
92         {
93                 if (Q < T(0)) 
94                 {
95                         P = -P;
96                         Q = -Q;
97                 }
98                 if (P == T(0))
99                 {
100                         Q = T(1);
101                         return;
102                 }
103                 if (P == Q)
104                 {
105                         P = Q = T(1);
106                         return;
107                 }
108                 T g = gcd(Tabs(P), Tabs(Q));
109                 //Debug("Got gcd!");
110                 P /= g;
111                 Q /= g;
112         }
113
114         bool operator==(const Rational & r)  const
115         {
116                 if (P == r.P && Q == r.Q) return true;
117                 return ToDouble() == r.ToDouble();
118         }
119
120
121         bool operator<(const Rational & r) const {return (P*r.Q < r.P * Q);}
122         bool operator>(const Rational & r) const {return !(*this < r);}
123         bool operator<=(const Rational & r) const {return *this == r || *this < r;}
124         bool operator>=(const Rational & r) const {return *this == r || *this > r;}
125         bool operator!=(const Rational & r) const {return !(*this == r);}
126
127         Rational operator+(const Rational & r) const 
128         {
129                 Rational result = (r.P == T(0)) ? Rational(P,Q) : Rational(P*r.Q + r.P*Q, Q*r.Q);
130                 //if (!result.CheckAccuracy(ToDouble() * r.ToDouble(),"+"))
131                 //{
132                 //      Debug("This is %s (%f) and r is %s (%f)", Str().c_str(), ToDouble(), r.Str().c_str(), r.ToDouble());
133                 //}
134                 return result;
135         }
136         Rational operator-(const Rational & r) const 
137         {
138                 Rational result = (r.P == T(0)) ? Rational(P,Q) : Rational(P*r.Q - r.P*Q, Q*r.Q);
139                 //result.CheckAccuracy(ToDouble() - r.ToDouble(),"-");
140                 return result;
141         }
142         Rational operator*(const Rational & r) const 
143         {
144                 Rational result(P * r.P, Q * r.Q);
145                 //if (!result.CheckAccuracy(ToDouble() * r.ToDouble(),"*"))
146                 //{
147                 //      Debug("This is %s (%f) and r is %s (%f)", Str().c_str(), ToDouble(), r.Str().c_str(), r.ToDouble());
148                 //}
149                 return result;
150         }
151         Rational operator/(const Rational & r) const 
152         {
153                 Rational result(P * r.Q, Q*r.P);
154                 //if (!result.CheckAccuracy(ToDouble() / r.ToDouble(),"/"))
155                 //{
156                 //      Debug("This is %s (%f) and r is %s (%f)", Str().c_str(), ToDouble(), r.Str().c_str(), r.ToDouble());
157                 //}
158                 return result;
159         }       
160
161         /** To cheat, use these **/
162         //Rational operator+(const Rational & r) const {return Rational(ToDouble()+r.ToDouble());}
163         //Rational operator-(const Rational & r) const {return Rational(ToDouble()-r.ToDouble());}
164         //Rational operator*(const Rational & r) const {return Rational(ToDouble()*r.ToDouble());}
165         //Rational operator/(const Rational & r) const {return Rational(ToDouble()/r.ToDouble());}
166
167         Rational operator-() const {Rational r(*this); r.P = -r.P; return r;}
168         Rational & operator=(const Rational & r) {P = r.P; Q = r.Q; Simplify(); return *this;}
169         Rational & operator+=(const Rational & r) {this->operator=(*this+r); return *this;}
170         Rational & operator-=(const Rational & r) {this->operator=(*this-r); return *this;}
171         Rational & operator*=(const Rational & r) {this->operator=(*this*r); return *this;}
172         Rational & operator/=(const Rational & r) {this->operator=(*this/r); return *this;}
173
174         double ToDouble() const 
175         {
176                 T num = P, denom = Q;
177                 while (Tabs(num) > T(DBL_MAX))
178                 {
179                         num /= T(16);
180                         denom /= T(16);
181                 }
182                 return ((double)(num))/((double)(denom));
183         }
184         bool CheckAccuracy(double d, const char * msg, double threshold = 1e-3) const
185         {
186                 double result = fabs(ToDouble() - d);
187                 if (d != 0e0) result /= d;
188                 if (result > threshold)
189                 {
190                         Warn("(%s) : Rational %s (%f) is not close enough at representing %f (%f vs %f)", msg, Str().c_str(), ToDouble(), d, result, threshold);
191                         Backtrace();
192                         return false;
193                 }
194                 return true;
195         }
196         std::string Str() const
197         {
198                 std::stringstream s;
199                 s << int64_t(P) << "/" << int64_t(Q);
200                 return s.str();
201         }
202         
203         T P;
204         T Q;
205 };
206
207
208
209
210
211 }
212
213 #endif //_RATIONAL_H
214

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