5 * A really shoddy implementation of Rational numbers
16 template <class T> T Tabs(const T & a)
20 template <> Arbint Tabs(const Arbint & a);
23 /* Recursive version of GCD
25 T gcd(const T & a, const T & b)
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;
37 if (a > b) return gcd(a-b,b);
42 /** Greatest Common Divisor of p and q **/
44 T gcd(const T & p, const T & q)
58 while ((g = big % small) > T(0))
60 //Debug("big = %li, small = %li", int64_t(big), int64_t(small));
63 //Debug("Loop %u", ++count);
69 template <class T = int64_t>
72 /** Construct from a double.**/
73 Rational(double d=0) : P(d*1e6), Q(1e6) // Possibly the worst thing ever...
78 Rational(const T & _P, const T & _Q) : P(_P), Q(_Q)
83 Rational(const Rational & cpy) : P(cpy.P), Q(cpy.Q)
105 T g = gcd(Tabs(P), Tabs(Q));
111 bool operator==(const Rational & r) const
113 if (P == r.P && Q == r.Q) return true;
114 return ToDouble() == r.ToDouble();
118 bool operator<(const Rational & r) const {return (P*r.Q < r.P * Q);}
119 bool operator>(const Rational & r) const {return !(*this < r);}
120 bool operator<=(const Rational & r) const {return *this == r || *this < r;}
121 bool operator>=(const Rational & r) const {return *this == r || *this > r;}
122 bool operator!=(const Rational & r) const {return !(*this == r);}
124 Rational operator+(const Rational & r) const
126 Rational result = (r.P == T(0)) ? Rational(P,Q) : Rational(P*r.Q + r.P*Q, Q*r.Q);
127 //if (!result.CheckAccuracy(ToDouble() * r.ToDouble(),"+"))
129 // Debug("This is %s (%f) and r is %s (%f)", Str().c_str(), ToDouble(), r.Str().c_str(), r.ToDouble());
133 Rational operator-(const Rational & r) const
135 Rational result = (r.P == T(0)) ? Rational(P,Q) : Rational(P*r.Q - r.P*Q, Q*r.Q);
136 //result.CheckAccuracy(ToDouble() - r.ToDouble(),"-");
139 Rational operator*(const Rational & r) const
141 Rational result(P * r.P, Q * r.Q);
142 //if (!result.CheckAccuracy(ToDouble() * r.ToDouble(),"*"))
144 // Debug("This is %s (%f) and r is %s (%f)", Str().c_str(), ToDouble(), r.Str().c_str(), r.ToDouble());
148 Rational operator/(const Rational & r) const
150 Rational result(P * r.Q, Q*r.P);
151 //if (!result.CheckAccuracy(ToDouble() / r.ToDouble(),"/"))
153 // Debug("This is %s (%f) and r is %s (%f)", Str().c_str(), ToDouble(), r.Str().c_str(), r.ToDouble());
158 /** To cheat, use these **/
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 //Rational operator/(const Rational & r) const {return Rational(ToDouble()/r.ToDouble());}
164 Rational operator-() const {Rational r(*this); r.P = -r.P;}
165 Rational & operator=(const Rational & r) {P = r.P; Q = r.Q; Simplify(); 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 Rational & operator*=(const Rational & r) {this->operator=(*this*r); return *this;}
169 Rational & operator/=(const Rational & r) {this->operator=(*this/r); return *this;}
171 double ToDouble() const
173 return (double)P/(double)Q;
175 bool CheckAccuracy(double d, const char * msg, double threshold = 1e-3) const
177 double result = fabs(ToDouble() - d);
178 if (d != 0e0) result /= d;
179 if (result > threshold)
181 Warn("(%s) : Rational %s (%f) is not close enough at representing %f (%f vs %f)", msg, Str().c_str(), ToDouble(), d, result, threshold);
187 std::string Str() const
190 s << int64_t(P) << "/" << int64_t(Q);
198 inline Rational<int64_t> pow(const Rational<int64_t> & a, const Rational<int64_t> & b)
200 //TODO:Implement properly
201 int64_t P = std::pow((double)a.P, b.ToDouble());
202 int64_t Q = std::pow((double)a.Q, b.ToDouble());
203 return Rational<int64_t>(P, Q);