libcruft-util/rational.cpp

174 lines
3.8 KiB
C++
Raw Normal View History

2014-07-02 15:49:27 +10:00
/*
2018-08-04 15:14:06 +10:00
* 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/.
2014-07-02 15:49:27 +10:00
*
* Copyright 2014 Danny Robson <danny@nerdcruft.net>
*/
#include "rational.hpp"
2015-07-13 16:27:07 +10:00
#include "maths.hpp"
#include "debug.hpp"
2015-07-13 16:27:07 +10:00
2014-07-02 15:49:27 +10:00
#include <cstdint>
using cruft::rational;
2014-07-02 15:49:27 +10:00
2015-07-13 16:27:07 +10:00
///////////////////////////////////////////////////////////////////////////////
2014-07-02 15:49:27 +10:00
template <typename T>
rational<T>::rational (T _n, T _d):
n (_n),
d (_d)
2015-07-13 16:27:07 +10:00
{
if (n < 0 && d < 0) {
n *= -1;
d *= -1;
}
}
2014-07-02 15:49:27 +10:00
//-----------------------------------------------------------------------------
template <typename T>
2015-07-13 16:27:07 +10:00
rational<T>::rational (T v):
n (v),
d (1)
{ ; }
///////////////////////////////////////////////////////////////////////////////
template <typename T>
2014-07-02 15:49:27 +10:00
bool
2015-07-13 16:27:07 +10:00
rational<T>::operator== (const rational<T> rhs) const
{
2014-07-02 15:49:27 +10:00
return rhs.n == n && rhs.d == d;
}
//-----------------------------------------------------------------------------
template <typename T>
2015-07-13 16:27:07 +10:00
bool
rational<T>::operator!= (const rational<T> rhs) const
{
return !operator== (rhs);
}
//-----------------------------------------------------------------------------
template <typename T>
bool
rational<T>::operator< (const rational<T> rhs) const
{
return n * rhs.d < rhs.n * d;
}
//-----------------------------------------------------------------------------
template <typename T>
bool
rational<T>::operator>= (const rational<T> rhs) const
{
return n * rhs.d >= rhs.n * d;
}
///////////////////////////////////////////////////////////////////////////////
template <typename T>
rational<T>::operator float (void) const
{
2014-07-02 15:49:27 +10:00
return static_cast<float> (n) / d;
}
//-----------------------------------------------------------------------------
template <typename T>
2015-07-13 16:27:07 +10:00
rational<T>::operator double (void) const
{
2014-07-02 15:49:27 +10:00
return static_cast<double> (n) / d;
}
2015-09-21 16:35:31 +10:00
//-----------------------------------------------------------------------------
template <typename T>
rational<T>::operator int (void) const
{
return static_cast<int> (n / d);
}
2015-07-13 16:27:07 +10:00
///////////////////////////////////////////////////////////////////////////////
template <typename T>
rational<T>
rational<T>::reduced (void) const
{
CHECK_NEZ (d);
auto x = std::gcd (abs (n), abs (d));
2015-07-13 16:27:07 +10:00
return { n / x, d / x };
}
///////////////////////////////////////////////////////////////////////////////
template <typename T>
rational<T>
rational<T>::inverse (void) const
{
return rational<T> { d, n };
}
//-----------------------------------------------------------------------------
template <typename T>
2015-07-13 16:27:07 +10:00
rational<T>
rational<T>::operator+ (const T rhs) const
{
return { n + rhs * d, d };
}
//-----------------------------------------------------------------------------
template <typename T>
rational<T>
2015-07-13 16:27:07 +10:00
rational<T>::operator- (const T rhs) const
{
return { n - rhs * d, d };
}
//-----------------------------------------------------------------------------
template <typename T>
rational<T>
2015-07-13 16:27:07 +10:00
rational<T>::operator* (const T rhs) const
{
return { rhs * n, d };
}
//-----------------------------------------------------------------------------
template <typename T>
rational<T>
2015-07-13 16:27:07 +10:00
rational<T>::operator/ (const T rhs) const
{
return { n, rhs * d };
}
///////////////////////////////////////////////////////////////////////////////
template <typename ValueT>
std::ostream&
cruft::operator<< (std::ostream &os, cruft::rational<ValueT> val)
{
return os << val.n << "/" << val.d;
}
///////////////////////////////////////////////////////////////////////////////
#define INSTANTIATE(TYPE) \
template struct cruft::rational<TYPE>; \
template cruft::rational<TYPE> cruft::operator/ (TYPE, cruft::rational<TYPE>); \
template std::ostream& cruft::operator<< (std::ostream&, cruft::rational<TYPE>);
INSTANTIATE(uint32_t)
INSTANTIATE(int32_t)