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);
27 /* Recursive version of GCD
29 T gcd(const T & a, const T & b)
31 Debug("Called on %li/%li", int64_t(a), int64_t(b));
32 if (a == T(1) || a == T(0)) return T(1);
33 if (b == T(0)) return a;
41 if (a > b) return gcd(a-b,b);
46 /** Greatest Common Divisor of p and q **/
48 T gcd(const T & p, const T & q)
62 while ((g = big % small) > T(0))
64 //Debug("big = %li, small = %li", int64_t(big), int64_t(small));
67 //Debug("Loop %u", ++count);
73 template <class T = int64_t>
76 /** Construct from a double.**/
77 Rational(double d=0) : P(d*1e6), Q(1e6) // Possibly the worst thing ever...
84 Rational(const T & _P, const T & _Q) : P(_P), Q(_Q)
89 Rational(const Rational & cpy) : P(cpy.P), Q(cpy.Q)
111 T g = gcd(Tabs(P), Tabs(Q));
117 bool operator==(const Rational & r) const
119 if (P == r.P && Q == r.Q) return true;
120 return ToDouble() == r.ToDouble();
124 bool operator<(const Rational & r) const {return (P*r.Q < r.P * Q);}
125 bool operator>(const Rational & r) const {return !(*this < r);}
126 bool operator<=(const Rational & r) const {return *this == r || *this < r;}
127 bool operator>=(const Rational & r) const {return *this == r || *this > r;}
128 bool operator!=(const Rational & r) const {return !(*this == r);}
130 Rational operator+(const Rational & r) const
132 Rational result = (r.P == T(0)) ? Rational(P,Q) : Rational(P*r.Q + r.P*Q, Q*r.Q);
133 //if (!result.CheckAccuracy(ToDouble() * r.ToDouble(),"+"))
135 // Debug("This is %s (%f) and r is %s (%f)", Str().c_str(), ToDouble(), r.Str().c_str(), r.ToDouble());
139 Rational operator-(const Rational & r) const
141 Rational result = (r.P == T(0)) ? Rational(P,Q) : Rational(P*r.Q - r.P*Q, Q*r.Q);
142 //result.CheckAccuracy(ToDouble() - r.ToDouble(),"-");
145 Rational operator*(const Rational & r) const
147 Rational result(P * r.P, Q * r.Q);
148 //if (!result.CheckAccuracy(ToDouble() * r.ToDouble(),"*"))
150 // Debug("This is %s (%f) and r is %s (%f)", Str().c_str(), ToDouble(), r.Str().c_str(), r.ToDouble());
154 Rational operator/(const Rational & r) const
156 Rational result(P * r.Q, Q*r.P);
157 //if (!result.CheckAccuracy(ToDouble() / r.ToDouble(),"/"))
159 // Debug("This is %s (%f) and r is %s (%f)", Str().c_str(), ToDouble(), r.Str().c_str(), r.ToDouble());
164 /** To cheat, use these **/
165 //Rational operator+(const Rational & r) const {return Rational(ToDouble()+r.ToDouble());}
166 //Rational operator-(const Rational & r) const {return Rational(ToDouble()-r.ToDouble());}
167 //Rational operator*(const Rational & r) const {return Rational(ToDouble()*r.ToDouble());}
168 //Rational operator/(const Rational & r) const {return Rational(ToDouble()/r.ToDouble());}
170 Rational operator-() const {Rational r(*this); r.P = -r.P; return r;}
171 Rational & operator=(const Rational & r) {P = r.P; Q = r.Q; Simplify(); return *this;}
172 Rational & operator+=(const Rational & r) {this->operator=(*this+r); return *this;}
173 Rational & operator-=(const Rational & r) {this->operator=(*this-r); return *this;}
174 Rational & operator*=(const Rational & r) {this->operator=(*this*r); return *this;}
175 Rational & operator/=(const Rational & r) {this->operator=(*this/r); return *this;}
176 Rational Sqrt() const
178 return Rational(sqrt(ToDouble()));
181 int64_t ToInt64() const
183 return (int64_t)ToDouble();
186 double ToDouble() const
188 T num = P, denom = Q;
189 while (Tabs(num) > T(1e10) || Tabs(denom) > T(1e10))
194 return ((double)(num))/((double)(denom));
196 bool CheckAccuracy(double d, const char * msg, double threshold = 1e-3) const
198 double result = fabs(ToDouble() - d);
199 if (d != 0e0) result /= d;
200 if (result > threshold)
202 Warn("(%s) : Rational %s (%f) is not close enough at representing %f (%f vs %f)", msg, Str().c_str(), ToDouble(), d, result, threshold);
208 std::string Str() const
211 s << int64_t(P) << "/" << int64_t(Q);
221 Rational<P> Abs(const Rational<P> & a)
223 return Rational<P>(Tabs(a.P), a.Q);