5 * A really shoddy implementation of Rational numbers
19 template <class T> T Tabs(const T & a)
23 template <> Arbint Tabs(const Arbint & a);
24 template <> Gmpint Tabs(const Gmpint & a);
26 /* Recursive version of GCD
28 T gcd(const T & a, const T & b)
30 Debug("Called on %li/%li", int64_t(a), int64_t(b));
31 if (a == T(1) || a == T(0)) return T(1);
32 if (b == T(0)) return a;
40 if (a > b) return gcd(a-b,b);
45 /** Greatest Common Divisor of p and q **/
47 T gcd(const T & p, const T & q)
61 while ((g = big % small) > T(0))
63 //Debug("big = %li, small = %li", int64_t(big), int64_t(small));
66 //Debug("Loop %u", ++count);
72 template <class T = int64_t>
75 /** Construct from a double.**/
76 Rational(double d=0) : P(d*1e6), Q(1e6) // Possibly the worst thing ever...
81 Rational(const T & _P, const T & _Q) : P(_P), Q(_Q)
86 Rational(const Rational & cpy) : P(cpy.P), Q(cpy.Q)
108 T g = gcd(Tabs(P), Tabs(Q));
114 bool operator==(const Rational & r) const
116 if (P == r.P && Q == r.Q) return true;
117 return ToDouble() == r.ToDouble();
121 bool operator<(const Rational & r) const {return (P*r.Q < r.P * Q);}
122 bool operator>(const Rational & r) const {return !(*this < r);}
123 bool operator<=(const Rational & r) const {return *this == r || *this < r;}
124 bool operator>=(const Rational & r) const {return *this == r || *this > r;}
125 bool operator!=(const Rational & r) const {return !(*this == r);}
127 Rational operator+(const Rational & r) const
129 Rational result = (r.P == T(0)) ? Rational(P,Q) : Rational(P*r.Q + r.P*Q, Q*r.Q);
130 //if (!result.CheckAccuracy(ToDouble() * r.ToDouble(),"+"))
132 // Debug("This is %s (%f) and r is %s (%f)", Str().c_str(), ToDouble(), r.Str().c_str(), r.ToDouble());
136 Rational operator-(const Rational & r) const
138 Rational result = (r.P == T(0)) ? Rational(P,Q) : Rational(P*r.Q - r.P*Q, Q*r.Q);
139 //result.CheckAccuracy(ToDouble() - r.ToDouble(),"-");
142 Rational operator*(const Rational & r) const
144 Rational result(P * r.P, Q * r.Q);
145 //if (!result.CheckAccuracy(ToDouble() * r.ToDouble(),"*"))
147 // Debug("This is %s (%f) and r is %s (%f)", Str().c_str(), ToDouble(), r.Str().c_str(), r.ToDouble());
151 Rational operator/(const Rational & r) const
153 Rational result(P * r.Q, Q*r.P);
154 //if (!result.CheckAccuracy(ToDouble() / r.ToDouble(),"/"))
156 // Debug("This is %s (%f) and r is %s (%f)", Str().c_str(), ToDouble(), r.Str().c_str(), r.ToDouble());
161 /** To cheat, use these **/
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 //Rational operator/(const Rational & r) const {return Rational(ToDouble()/r.ToDouble());}
167 Rational operator-() const {Rational r(*this); r.P = -r.P; return r;}
168 Rational & operator=(const Rational & r) {P = r.P; Q = r.Q; Simplify(); 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 Rational & operator*=(const Rational & r) {this->operator=(*this*r); return *this;}
172 Rational & operator/=(const Rational & r) {this->operator=(*this/r); return *this;}
174 double ToDouble() const
176 T num = P, denom = Q;
177 while (Tabs(num) > T(DBL_MAX))
182 return ((double)(num))/((double)(denom));
184 bool CheckAccuracy(double d, const char * msg, double threshold = 1e-3) const
186 double result = fabs(ToDouble() - d);
187 if (d != 0e0) result /= d;
188 if (result > threshold)
190 Warn("(%s) : Rational %s (%f) is not close enough at representing %f (%f vs %f)", msg, Str().c_str(), ToDouble(), d, result, threshold);
196 std::string Str() const
199 s << int64_t(P) << "/" << int64_t(Q);