diff --git a/Makefile.am b/Makefile.am index ab78f268..b3495aed 100644 --- a/Makefile.am +++ b/Makefile.am @@ -124,6 +124,8 @@ UTIL_FILES = \ random.ipp \ range.cpp \ range.hpp \ + rational.cpp \ + rational.hpp \ region.cpp \ region.hpp \ si.cpp \ diff --git a/rational.cpp b/rational.cpp new file mode 100644 index 00000000..351dfac9 --- /dev/null +++ b/rational.cpp @@ -0,0 +1,59 @@ +/* + * This file is part of libgim. + * + * libgim is free software: you can redistribute it and/or modify it under the + * terms of the GNU General Public License as published by the Free Software + * Foundation, either version 3 of the License, or (at your option) any later + * version. + * + * libgim is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License + * along with libgim. If not, see . + * + * 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 struct rational; +template struct rational; diff --git a/rational.hpp b/rational.hpp new file mode 100644 index 00000000..0ccf141b --- /dev/null +++ b/rational.hpp @@ -0,0 +1,39 @@ +/* + * This file is part of libgim. + * + * libgim is free software: you can redistribute it and/or modify it under the + * terms of the GNU General Public License as published by the Free Software + * Foundation, either version 3 of the License, or (at your option) any later + * version. + * + * libgim is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License + * along with libgim. If not, see . + * + * Copyright 2014 Danny Robson + */ + +#ifndef __UTIL_RATIONAL_HPP +#define __UTIL_RATIONAL_HPP + +namespace util { + template + struct rational { + rational (T _n, T _d); + rational& operator= (const rational&) = default; + + bool operator== (const rational&); + + explicit operator float (void) const; + explicit operator double (void) const; + + T n; + T d; + }; +} + +#endif