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

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