Arbint class with += and -= operators
[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         if (a == 1 || a == 0) return 1;
20         if (b == 0) return a;
21         if (b == a) return a;
22         
23         if (a > b) return gcd(a-b,b);
24         return gcd(a, b-a);
25 }
26 */
27
28 /** Greatest Common Divisor of p and q **/
29 template <class T>
30 T gcd(const T & p, const T & q)
31 {
32         T g(1);
33         T big(p);
34         T small(q);
35         if (p < q)
36         {
37                 big = q;
38                 small = p;
39         }
40         if (small == 0)
41                 return g;
42         while ((g = big % small) > 0)
43         {
44                 big = small;
45                 small = g;
46         }
47         return small;
48 }        
49
50 template <class T = int64_t>
51 struct Rational
52 {
53         /** Construct from a double.**/
54         Rational(double d=0) : P(d*1e6), Q(1e6) // Possibly the worst thing ever...
55         {
56                 Simplify();
57                 CheckAccuracy(d, "Construct from double");
58         }
59
60         Rational(const T & _P, const T & _Q) : P(_P), Q(_Q)
61         {
62                 Simplify();
63         }
64
65         Rational(const Rational & cpy) : P(cpy.P), Q(cpy.Q)
66         {
67                 Simplify();
68         }
69
70         void Simplify()
71         {
72                 if (Q < 0) 
73                 {
74                         P = -P;
75                         Q = -Q;
76                 }
77                 if (P == 0)
78                 {
79                         Q = 1;
80                         return;
81                 }
82                 T g = gcd(llabs(P),llabs(Q));
83                 P /= g;
84                 Q /= g;
85         }
86
87         bool operator==(const Rational & r)  const
88         {
89                 if (P == r.P && Q == r.Q) return true;
90                 return ToDouble() == r.ToDouble();
91         }
92
93
94         bool operator<(const Rational & r) const {return (P*r.Q < r.P * Q);}
95         bool operator>(const Rational & r) const {return !(*this < r);}
96         bool operator<=(const Rational & r) const {return *this == r || *this < r;}
97         bool operator>=(const Rational & r) const {return *this == r || *this > r;}
98         bool operator!=(const Rational & r) const {return !(*this == r);}
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         Rational operator*(const Rational & r) const 
113         {
114                 Rational result(P * r.P, Q * r.Q);
115                 if (!result.CheckAccuracy(ToDouble() * r.ToDouble(),"*"))
116                 {
117                         Debug("This is %s (%f) and r is %s (%f)", Str().c_str(), ToDouble(), r.Str().c_str(), r.ToDouble());
118                 }
119                 return result;
120         }
121         Rational operator/(const Rational & r) const 
122         {
123                 Rational result(P * r.Q, Q*r.P);
124                 if (!result.CheckAccuracy(ToDouble() / r.ToDouble(),"/"))
125                 {
126                         Debug("This is %s (%f) and r is %s (%f)", Str().c_str(), ToDouble(), r.Str().c_str(), r.ToDouble());
127                 }
128                 return result;
129         }       
130
131         /** To cheat, use these **/
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-3) 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