23 Rational(
const T &numerator = 0,
const T &denominator = 1):
27 assert(denominator != 0);
35 inline T getNumerator()
const
40 inline T getDenominator()
const
45 explicit inline operator T()
const
51 explicit inline operator double()
const
53 return double(n_) / double(d_);
65 explicit inline operator std::string()
const
81 ss << n_ <<
"/" << d_;
86 inline void print(std::ostream& os)
const
88 os << this->
operator std::string();
91 inline void simplify()
97 T g = utils::gcd(n_, d_);
141 T m = utils::lcm(d_, r.d_);
142 return Rational<T>((n_ * m / d_) + (r.n_ * m / r.d_), m);
151 T m = utils::lcm(d_, r.d_);
152 n_ = (n_ * m / d_) + (r.n_ * m / r.d_);
165 T m = utils::lcm(d_, r.d_);
166 return Rational<T>(utils::abs((n_ * m / d_) - (r.n_ * m / r.d_)), m);
175 T m = utils::lcm(d_, r.d_);
176 n_ = utils::abs((n_ * m / d_) - (r.n_ * m / r.d_));
179 n_ = utils::abs(n_ - r.n_);
195 return (n_ == t.n_) && (d_ == t.d_);