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

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