5 * A really shoddy implementation of Rational numbers
15 /* Recursive version of GCD
17 T gcd(const T & a, const T & b)
19 Debug("Called on %li/%li", int64_t(a), int64_t(b));
20 if (a == T(1) || a == T(0)) return T(1);
21 if (b == T(0)) return a;
29 if (a > b) return gcd(a-b,b);
34 /** Greatest Common Divisor of p and q **/
36 T gcd(const T & p, const T & q)
50 while ((g = big % small) > T(0))
52 //Debug("big = %li, small = %li", int64_t(big), int64_t(small));
55 //Debug("Loop %u", ++count);
61 template <class T = int64_t>
64 /** Construct from a double.**/
65 Rational(double d=0) : P(d*1e6), Q(1e6) // Possibly the worst thing ever...
68 CheckAccuracy(d, "Construct from double");
71 Rational(const T & _P, const T & _Q) : P(_P), Q(_Q)
76 Rational(const Rational & cpy) : P(cpy.P), Q(cpy.Q)
93 T g = gcd(T(llabs(P)),T(llabs(Q)));
99 bool operator==(const Rational & r) const
101 if (P == r.P && Q == r.Q) return true;
102 return ToDouble() == r.ToDouble();
106 bool operator<(const Rational & r) const {return (P*r.Q < r.P * Q);}
107 bool operator>(const Rational & r) const {return !(*this < r);}
108 bool operator<=(const Rational & r) const {return *this == r || *this < r;}
109 bool operator>=(const Rational & r) const {return *this == r || *this > r;}
110 bool operator!=(const Rational & r) const {return !(*this == r);}
112 Rational operator+(const Rational & r) const
114 Rational result = (r.P == T(0)) ? Rational(P,Q) : Rational(P*r.Q + r.P*Q, Q*r.Q);
115 result.CheckAccuracy(ToDouble() + r.ToDouble(),"+");
118 Rational operator-(const Rational & r) const
120 Rational result = (r.P == T(0)) ? Rational(P,Q) : Rational(P*r.Q - r.P*Q, Q*r.Q);
121 result.CheckAccuracy(ToDouble() - r.ToDouble(),"-");
124 Rational operator*(const Rational & r) const
126 Rational result(P * r.P, 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(P * r.Q, Q*r.P);
136 if (!result.CheckAccuracy(ToDouble() / r.ToDouble(),"/"))
138 Debug("This is %s (%f) and r is %s (%f)", Str().c_str(), ToDouble(), r.Str().c_str(), r.ToDouble());
143 /** To cheat, use these **/
144 //Rational operator+(const Rational & r) const {return Rational(ToDouble()+r.ToDouble());}
145 //Rational operator-(const Rational & r) const {return Rational(ToDouble()-r.ToDouble());}
146 //Rational operator*(const Rational & r) const {return Rational(ToDouble()*r.ToDouble());}
147 //Rational operator/(const Rational & r) const {return Rational(ToDouble()/r.ToDouble());}
149 Rational & operator=(const Rational & r) {P = r.P; Q = r.Q; return *this;}
150 Rational & operator+=(const Rational & r) {this->operator=(*this+r); return *this;}
151 Rational & operator-=(const Rational & r) {this->operator=(*this-r); return *this;}
152 Rational & operator*=(const Rational & r) {this->operator=(*this*r); return *this;}
153 Rational & operator/=(const Rational & r) {this->operator=(*this/r); return *this;}
155 double ToDouble() const {return (double)(P) / (double)(Q);}
156 bool CheckAccuracy(double d, const char * msg, double threshold = 1e-3) const
158 double result = fabs(ToDouble() - d) / d;
159 if (result > threshold)
161 Warn("(%s) : Rational %s (%f) is not close enough at representing %f (%f vs %f)", msg, Str().c_str(), ToDouble(), d, result, threshold);
166 std::string Str() const
169 s << (int64_t)P << "/" << (int64_t)Q;
177 inline Rational<int64_t> pow(const Rational<int64_t> & a, const Rational<int64_t> & b)
179 //TODO:Implement properly
180 int64_t P = std::pow((double)a.P, b.ToDouble());
181 int64_t Q = std::pow((double)a.Q, b.ToDouble());
182 return Rational<int64_t>(P, Q);