/* * 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-2015 Danny Robson */ #ifndef __UTIL_RATIONAL_HPP #define __UTIL_RATIONAL_HPP #include #include namespace cruft { template struct rational { static_assert (std::is_integral::value, "only defined for integer types"); rational (const rational&) = default; rational (T n, T d); explicit rational (T); bool operator== (rational) const; bool operator!= (rational) const; bool operator< (rational) const; bool operator>= (rational) const; explicit operator float (void) const; explicit operator double (void) const; explicit operator int (void) const; rational reduced (void) const; rational inverse (void) const; rational& invert (void); rational operator+ (T) const; rational operator- (T) const; rational operator* (T) const; rational operator/ (T) const; T n; T d; }; /////////////////////////////////////////////////////////////////////////// template rational::type> operator/ (U lhs, rational rhs) { return rhs.inverse () * lhs; } //------------------------------------------------------------------------- template rational::type> operator* (U lhs, rational rhs) { return rhs * lhs; } /////////////////////////////////////////////////////////////////////////// template std::ostream& operator<< (std::ostream&, rational); }; #endif