5 * A really shoddy implementation of Rational numbers
15 /** Greatest Common Divisor - Euclid's algorithm **/
18 T gcd(const T & a, const T & b)
20 if (a == 1 || a == 0) return 1;
24 if (a > b) return gcd(a-b,b);
30 T gcd(const T & p, const T & q)
43 while ((g = big % small) > 0)
51 template <class T = int64_t>
54 /** Construct from a double.**/
55 Rational(double d = 0) : P(d*1e3), Q(1e3) // Possibly the worst thing ever...
58 //CheckAccuracy(d, "Construct from double");
61 Rational(const T & _P, const T & _Q) : P(_P), Q(_Q)
66 Rational(const Rational & cpy) : P(cpy.P), Q(cpy.Q)
79 T g = gcd(llabs(P),llabs(Q));
84 bool operator==(const Rational & r) const
86 if (P == r.P && Q == r.Q) return true;
87 return ToDouble() == r.ToDouble();
91 bool operator<(const Rational & r) const {return (P*r.Q < r.P * Q);}
92 bool operator>(const Rational & r) const {return !(*this < r);}
93 bool operator<=(const Rational & r) const {return *this == r || *this < r;}
94 bool operator>=(const Rational & r) const {return *this == r || *this > r;}
95 bool operator!=(const Rational & r) const {return !(*this == r);}
100 Rational operator+(const Rational & r) const
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(),"+");
106 Rational operator-(const Rational & r) const
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(),"-");
113 Rational operator*(const Rational & r) const
115 Rational result(P * r.P, Q * r.Q);
116 if (!result.CheckAccuracy(ToDouble() * r.ToDouble(),"*"))
118 Debug("This is %s (%f) and r is %s (%f)", Str().c_str(), ToDouble(), r.Str().c_str(), r.ToDouble());
122 Rational operator/(const Rational & r) const
124 Rational result = (r.P == 0) ? Rational(P,Q) : Rational(P*r.Q + r.P*Q, Q*r.Q);
125 if (!result.CheckAccuracy(ToDouble() / r.ToDouble(),"*"))
127 Debug("This is %s (%f) and r is %s (%f)", Str().c_str(), ToDouble(), r.Str().c_str(), r.ToDouble());
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());}
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;}
143 double ToDouble() const {return (double)(P) / (double)(Q);}
144 bool CheckAccuracy(double d, const char * msg, double threshold = 1e-6) const
146 double result = fabs(ToDouble() - d) / d;
147 if (result > threshold)
149 Warn("(%s) : Rational %s (%f) is not close enough at representing %f (%f vs %f)", msg, Str().c_str(), ToDouble(), d, result, threshold);
154 std::string Str() const
157 s << (int64_t)P << "/" << (int64_t)Q;
165 inline Rational<int64_t> pow(const Rational<int64_t> & a, const Rational<int64_t> & b)
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);