My eyes, they burn! Also runs faster, slightly less buggy.
[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
27 /* Recursive version  of GCD
28 template <class T>
29 T gcd(const T & a, const T & b)
30 {
31         Debug("Called on %li/%li", int64_t(a), int64_t(b));
32         if (a == T(1) || a == T(0)) return T(1);
33         if (b == T(0)) return a;
34         if (b == a) 
35         {
36                 Debug("Equal!");
37                 return a;
38         }
39         Debug("Not equal!");
40         
41         if (a > b) return gcd(a-b,b);
42         return gcd(a, b-a);
43 }
44 */
45
46 /** Greatest Common Divisor of p and q **/
47 template <class T>
48 T gcd(const T & p, const T & q)
49 {
50
51
52         T g(1);
53         T big(p);
54         T small(q);
55         if (p < q)
56         {
57                 big = q;
58                 small = p;
59         }
60         if (small == T(0))
61                 return g;
62         while ((g = big % small) > T(0))
63         {
64                 //Debug("big = %li, small = %li", int64_t(big), int64_t(small));
65                 big = small;
66                 small = g;
67                 //Debug("Loop %u", ++count);
68         }
69         return small;
70 }       
71
72
73 template <class T = int64_t>
74 struct Rational
75 {
76         /** Construct from a double.**/
77         Rational(double d=0) : P(d*1e6), Q(1e6) // Possibly the worst thing ever...
78         {
79                 Simplify();
80         }
81         
82
83
84         Rational(const T & _P, const T & _Q) : P(_P), Q(_Q)
85         {
86                 Simplify();
87         }
88
89         Rational(const Rational & cpy) : P(cpy.P), Q(cpy.Q)
90         {
91                 Simplify();
92         }
93
94         void Simplify()
95         {
96                 if (Q < T(0)) 
97                 {
98                         P = -P;
99                         Q = -Q;
100                 }
101                 if (P == T(0))
102                 {
103                         Q = T(1);
104                         return;
105                 }
106                 if (P == Q)
107                 {
108                         P = Q = T(1);
109                         return;
110                 }
111                 T g = gcd(Tabs(P), Tabs(Q));
112                 //Debug("Got gcd!");
113                 P /= g;
114                 Q /= g;
115         }
116
117         bool operator==(const Rational & r)  const
118         {
119                 if (P == r.P && Q == r.Q) return true;
120                 return ToDouble() == r.ToDouble();
121         }
122
123
124         bool operator<(const Rational & r) const {return (P*r.Q < r.P * Q);}
125         bool operator>(const Rational & r) const {return !(*this < r);}
126         bool operator<=(const Rational & r) const {return *this == r || *this < r;}
127         bool operator>=(const Rational & r) const {return *this == r || *this > r;}
128         bool operator!=(const Rational & r) const {return !(*this == r);}
129
130         Rational operator+(const Rational & r) const 
131         {
132                 Rational result = (r.P == T(0)) ? Rational(P,Q) : Rational(P*r.Q + r.P*Q, Q*r.Q);
133                 //if (!result.CheckAccuracy(ToDouble() * r.ToDouble(),"+"))
134                 //{
135                 //      Debug("This is %s (%f) and r is %s (%f)", Str().c_str(), ToDouble(), r.Str().c_str(), r.ToDouble());
136                 //}
137                 return result;
138         }
139         Rational operator-(const Rational & r) const 
140         {
141                 Rational result = (r.P == T(0)) ? Rational(P,Q) : Rational(P*r.Q - r.P*Q, Q*r.Q);
142                 //result.CheckAccuracy(ToDouble() - r.ToDouble(),"-");
143                 return result;
144         }
145         Rational operator*(const Rational & r) const 
146         {
147                 Rational result(P * r.P, Q * r.Q);
148                 //if (!result.CheckAccuracy(ToDouble() * r.ToDouble(),"*"))
149                 //{
150                 //      Debug("This is %s (%f) and r is %s (%f)", Str().c_str(), ToDouble(), r.Str().c_str(), r.ToDouble());
151                 //}
152                 return result;
153         }
154         Rational operator/(const Rational & r) const 
155         {
156                 Rational result(P * r.Q, Q*r.P);
157                 //if (!result.CheckAccuracy(ToDouble() / r.ToDouble(),"/"))
158                 //{
159                 //      Debug("This is %s (%f) and r is %s (%f)", Str().c_str(), ToDouble(), r.Str().c_str(), r.ToDouble());
160                 //}
161                 return result;
162         }       
163
164         /** To cheat, use these **/
165         //Rational operator+(const Rational & r) const {return Rational(ToDouble()+r.ToDouble());}
166         //Rational operator-(const Rational & r) const {return Rational(ToDouble()-r.ToDouble());}
167         //Rational operator*(const Rational & r) const {return Rational(ToDouble()*r.ToDouble());}
168         //Rational operator/(const Rational & r) const {return Rational(ToDouble()/r.ToDouble());}
169
170         Rational operator-() const {Rational r(*this); r.P = -r.P; return r;}
171         Rational & operator=(const Rational & r) {P = r.P; Q = r.Q; Simplify(); return *this;}
172         Rational & operator+=(const Rational & r) {this->operator=(*this+r); return *this;}
173         Rational & operator-=(const Rational & r) {this->operator=(*this-r); return *this;}
174         Rational & operator*=(const Rational & r) {this->operator=(*this*r); return *this;}
175         Rational & operator/=(const Rational & r) {this->operator=(*this/r); return *this;}
176         Rational Sqrt() const
177         {
178                 return Rational(sqrt(ToDouble()));
179         }
180
181         int64_t ToInt64() const
182         {
183                 return (int64_t)ToDouble();
184         }
185
186         double ToDouble() const 
187         {
188                 T num = P, denom = Q;
189                 while (Tabs(num) > T(1e10) || Tabs(denom) > T(1e10))
190                 {
191                         num /= T(16);
192                         denom /= T(16);
193                 }
194                 return ((double)(num))/((double)(denom));
195         }
196         bool CheckAccuracy(double d, const char * msg, double threshold = 1e-3) const
197         {
198                 double result = fabs(ToDouble() - d);
199                 if (d != 0e0) result /= d;
200                 if (result > threshold)
201                 {
202                         Warn("(%s) : Rational %s (%f) is not close enough at representing %f (%f vs %f)", msg, Str().c_str(), ToDouble(), d, result, threshold);
203                         Backtrace();
204                         return false;
205                 }
206                 return true;
207         }
208         std::string Str() const
209         {
210                 std::stringstream s;
211                 s << int64_t(P) << "/" << int64_t(Q);
212                 return s.str();
213         }
214         
215         T P;
216         T Q;
217 };
218
219
220 template <class P>
221 Rational<P> Abs(const Rational<P> & a)
222 {
223         return Rational<P>(Tabs(a.P), a.Q);
224 }
225
226 }
227
228 #endif //_RATIONAL_H
229

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