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

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