/* * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * 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)