More testing particularly of negatives
[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                 //if (!CheckAccuracy(d, "Construct from double"))
77                 {
78                         //Fatal("Bwah bwah :(");
79                 }
80         }
81
82         Rational(const T & _P, const T & _Q) : P(_P), Q(_Q)
83         {
84                 Simplify();
85         }
86
87         Rational(const Rational & cpy) : P(cpy.P), Q(cpy.Q)
88         {
89                 Simplify();
90         }
91
92         void Simplify()
93         {
94                 if (Q < T(0)) 
95                 {
96                         P = -P;
97                         Q = -Q;
98                 }
99                 if (P == T(0))
100                 {
101                         Q = T(1);
102                         return;
103                 }
104                 T g = gcd(Tabs(P), Tabs(Q));
105                 //Debug("Got gcd!");
106                 P /= g;
107                 Q /= g;
108         }
109
110         bool operator==(const Rational & r)  const
111         {
112                 if (P == r.P && Q == r.Q) return true;
113                 return ToDouble() == r.ToDouble();
114         }
115
116
117         bool operator<(const Rational & r) const {return (P*r.Q < r.P * Q);}
118         bool operator>(const Rational & r) const {return !(*this < r);}
119         bool operator<=(const Rational & r) const {return *this == r || *this < r;}
120         bool operator>=(const Rational & r) const {return *this == r || *this > r;}
121         bool operator!=(const Rational & r) const {return !(*this == r);}
122
123         Rational operator+(const Rational & r) const 
124         {
125                 Rational result = (r.P == T(0)) ? Rational(P,Q) : Rational(P*r.Q + r.P*Q, Q*r.Q);
126                 //if (!result.CheckAccuracy(ToDouble() * r.ToDouble(),"+"))
127                 //{
128                 //      Debug("This is %s (%f) and r is %s (%f)", Str().c_str(), ToDouble(), r.Str().c_str(), r.ToDouble());
129                 //}
130                 return result;
131         }
132         Rational operator-(const Rational & r) const 
133         {
134                 Rational result = (r.P == T(0)) ? Rational(P,Q) : Rational(P*r.Q - r.P*Q, Q*r.Q);
135                 result.CheckAccuracy(ToDouble() - r.ToDouble(),"-");
136                 return result;
137         }
138         Rational operator*(const Rational & r) const 
139         {
140                 Rational result(P * r.P, Q * r.Q);
141                 //if (!result.CheckAccuracy(ToDouble() * r.ToDouble(),"*"))
142                 //{
143                 //      Debug("This is %s (%f) and r is %s (%f)", Str().c_str(), ToDouble(), r.Str().c_str(), r.ToDouble());
144                 //}
145                 return result;
146         }
147         Rational operator/(const Rational & r) const 
148         {
149                 Rational result(P * r.Q, Q*r.P);
150                 //if (!result.CheckAccuracy(ToDouble() / r.ToDouble(),"/"))
151                 //{
152                 //      Debug("This is %s (%f) and r is %s (%f)", Str().c_str(), ToDouble(), r.Str().c_str(), r.ToDouble());
153                 //}
154                 return result;
155         }       
156
157         /** To cheat, use these **/
158         //Rational operator+(const Rational & r) const {return Rational(ToDouble()+r.ToDouble());}
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
163         Rational & operator=(const Rational & r) {P = r.P; Q = r.Q; return *this;}
164         Rational & operator+=(const Rational & r) {this->operator=(*this+r); return *this;}
165         Rational & operator-=(const Rational & r) {this->operator=(*this-r); 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
169         double ToDouble() const {return (double)(P) / (double)(Q);}
170         bool CheckAccuracy(double d, const char * msg, double threshold = 1e-3) const
171         {
172                 double result = fabs(ToDouble() - d);
173                 if (d != 0e0) result /= d;
174                 if (result > threshold)
175                 {
176                         Warn("(%s) : Rational %s (%f) is not close enough at representing %f (%f vs %f)", msg, Str().c_str(), ToDouble(), d, result, threshold);
177                         Backtrace();
178                         return false;
179                 }
180                 return true;
181         }
182         std::string Str() const
183         {
184                 std::stringstream s;
185                 s << int64_t(P) << "/" << int64_t(Q);
186                 return s.str();
187         }
188         
189         T P;
190         T Q;
191 };
192
193 inline Rational<int64_t> pow(const Rational<int64_t> & a, const Rational<int64_t> & b)
194 {
195         //TODO:Implement properly
196         int64_t P = std::pow((double)a.P, b.ToDouble());
197         int64_t Q = std::pow((double)a.Q, b.ToDouble());
198         return Rational<int64_t>(P, Q);
199 }
200
201
202
203
204 }
205
206 #endif //_RATIONAL_H
207

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