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

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