/* * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. * * Copyright 2014 Danny Robson */ #include "rational.hpp" #include "maths.hpp" #include "debug.hpp" #include using util::rational; /////////////////////////////////////////////////////////////////////////////// template rational::rational (T _n, T _d): n (_n), d (_d) { if (n < 0 && d < 0) { n *= -1; d *= -1; } } //----------------------------------------------------------------------------- template rational::rational (T v): n (v), d (1) { ; } /////////////////////////////////////////////////////////////////////////////// template bool rational::operator== (const rational rhs) const { return rhs.n == n && rhs.d == d; } //----------------------------------------------------------------------------- template bool rational::operator!= (const rational rhs) const { return !operator== (rhs); } //----------------------------------------------------------------------------- template bool rational::operator< (const rational rhs) const { return n * rhs.d < rhs.n * d; } //----------------------------------------------------------------------------- template bool rational::operator>= (const rational rhs) const { return n * rhs.d >= rhs.n * d; } /////////////////////////////////////////////////////////////////////////////// template rational::operator float (void) const { return static_cast (n) / d; } //----------------------------------------------------------------------------- template rational::operator double (void) const { return static_cast (n) / d; } //----------------------------------------------------------------------------- template rational::operator int (void) const { return static_cast (n / d); } /////////////////////////////////////////////////////////////////////////////// template rational rational::reduced (void) const { CHECK_NEZ (d); auto x = std::gcd (abs (n), abs (d)); return { n / x, d / x }; } /////////////////////////////////////////////////////////////////////////////// template rational rational::inverse (void) const { return rational { d, n }; } //----------------------------------------------------------------------------- template rational rational::operator+ (const T rhs) const { return { n + rhs * d, d }; } //----------------------------------------------------------------------------- template rational rational::operator- (const T rhs) const { return { n - rhs * d, d }; } //----------------------------------------------------------------------------- template rational rational::operator* (const T rhs) const { return { rhs * n, d }; } //----------------------------------------------------------------------------- template rational rational::operator/ (const T rhs) const { return { n, rhs * d }; } /////////////////////////////////////////////////////////////////////////////// template std::ostream& util::operator<< (std::ostream &os, util::rational val) { return os << val.n << "/" << val.d; } /////////////////////////////////////////////////////////////////////////////// #define INSTANTIATE(TYPE) \ template struct util::rational; \ template util::rational util::operator/ (TYPE, util::rational); \ template std::ostream& util::operator<< (std::ostream&, util::rational); INSTANTIATE(uint32_t) INSTANTIATE(int32_t)