/* * 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 using util::rational; //----------------------------------------------------------------------------- template rational::rational (T _n, T _d): n (_n), d (_d) { ; } //----------------------------------------------------------------------------- template bool rational::operator== (const rational &rhs) { return rhs.n == n && rhs.d == 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 n / d; } //----------------------------------------------------------------------------- template rational rational::inverse (void) const { return rational { d, n }; } //----------------------------------------------------------------------------- template template rational rational::operator* (const U &rhs) const { return { rhs * n, d }; } //----------------------------------------------------------------------------- template template rational rational::operator/ (const U &rhs) const { return { n, rhs * d }; } //----------------------------------------------------------------------------- template rational util::operator/ (U lhs, rational rhs) { return rhs.inverse () * lhs; } //----------------------------------------------------------------------------- namespace util { template struct rational; template struct rational; template rational operator/ (int, rational); }