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

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