Divide some numbers by 5.
[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 abs(a);
19 }
20 template <> Arbint Tabs(const Arbint & a);
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                 if (P == Q)
101                 {
102                         P = Q = T(1);
103                         return;
104                 }
105                 T g = gcd(Tabs(P), Tabs(Q));
106                 //Debug("Got gcd!");
107                 P /= g;
108                 Q /= g;
109         }
110
111         bool operator==(const Rational & r)  const
112         {
113                 if (P == r.P && Q == r.Q) return true;
114                 return ToDouble() == r.ToDouble();
115         }
116
117
118         bool operator<(const Rational & r) const {return (P*r.Q < r.P * Q);}
119         bool operator>(const Rational & r) const {return !(*this < r);}
120         bool operator<=(const Rational & r) const {return *this == r || *this < r;}
121         bool operator>=(const Rational & r) const {return *this == r || *this > r;}
122         bool operator!=(const Rational & r) const {return !(*this == r);}
123
124         Rational operator+(const Rational & r) const 
125         {
126                 Rational result = (r.P == T(0)) ? Rational(P,Q) : Rational(P*r.Q + r.P*Q, Q*r.Q);
127                 //if (!result.CheckAccuracy(ToDouble() * r.ToDouble(),"+"))
128                 //{
129                 //      Debug("This is %s (%f) and r is %s (%f)", Str().c_str(), ToDouble(), r.Str().c_str(), r.ToDouble());
130                 //}
131                 return result;
132         }
133         Rational operator-(const Rational & r) const 
134         {
135                 Rational result = (r.P == T(0)) ? Rational(P,Q) : Rational(P*r.Q - r.P*Q, Q*r.Q);
136                 //result.CheckAccuracy(ToDouble() - r.ToDouble(),"-");
137                 return result;
138         }
139         Rational operator*(const Rational & r) const 
140         {
141                 Rational result(P * r.P, Q * r.Q);
142                 //if (!result.CheckAccuracy(ToDouble() * r.ToDouble(),"*"))
143                 //{
144                 //      Debug("This is %s (%f) and r is %s (%f)", Str().c_str(), ToDouble(), r.Str().c_str(), r.ToDouble());
145                 //}
146                 return result;
147         }
148         Rational operator/(const Rational & r) const 
149         {
150                 Rational result(P * r.Q, Q*r.P);
151                 //if (!result.CheckAccuracy(ToDouble() / r.ToDouble(),"/"))
152                 //{
153                 //      Debug("This is %s (%f) and r is %s (%f)", Str().c_str(), ToDouble(), r.Str().c_str(), r.ToDouble());
154                 //}
155                 return result;
156         }       
157
158         /** To cheat, use these **/
159         //Rational operator+(const Rational & r) const {return Rational(ToDouble()+r.ToDouble());}
160         //Rational operator-(const Rational & r) const {return Rational(ToDouble()-r.ToDouble());}
161         //Rational operator*(const Rational & r) const {return Rational(ToDouble()*r.ToDouble());}
162         //Rational operator/(const Rational & r) const {return Rational(ToDouble()/r.ToDouble());}
163
164         Rational operator-() const {Rational r(*this); r.P = -r.P;}
165         Rational & operator=(const Rational & r) {P = r.P; Q = r.Q; Simplify(); return *this;}
166         Rational & operator+=(const Rational & r) {this->operator=(*this+r); return *this;}
167         Rational & operator-=(const Rational & r) {this->operator=(*this-r); return *this;}
168         Rational & operator*=(const Rational & r) {this->operator=(*this*r); return *this;}
169         Rational & operator/=(const Rational & r) {this->operator=(*this/r); return *this;}
170
171         double ToDouble() const 
172         {
173                 return (double)P/(double)Q;
174         }
175         bool CheckAccuracy(double d, const char * msg, double threshold = 1e-3) const
176         {
177                 double result = fabs(ToDouble() - d);
178                 if (d != 0e0) result /= d;
179                 if (result > threshold)
180                 {
181                         Warn("(%s) : Rational %s (%f) is not close enough at representing %f (%f vs %f)", msg, Str().c_str(), ToDouble(), d, result, threshold);
182                         Backtrace();
183                         return false;
184                 }
185                 return true;
186         }
187         std::string Str() const
188         {
189                 std::stringstream s;
190                 s << int64_t(P) << "/" << int64_t(Q);
191                 return s.str();
192         }
193         
194         T P;
195         T Q;
196 };
197
198 inline Rational<int64_t> pow(const Rational<int64_t> & a, const Rational<int64_t> & b)
199 {
200         //TODO:Implement properly
201         int64_t P = std::pow((double)a.P, b.ToDouble());
202         int64_t Q = std::pow((double)a.Q, b.ToDouble());
203         return Rational<int64_t>(P, Q);
204 }
205
206
207
208
209 }
210
211 #endif //_RATIONAL_H
212

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