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"
|
2018-03-13 14:39:51 +11:00
|
|
|
#include "debug.hpp"
|
2015-07-13 16:27:07 +10:00
|
|
|
|
2014-07-02 15:49:27 +10:00
|
|
|
#include <cstdint>
|
|
|
|
|
2018-08-05 14:42:02 +10:00
|
|
|
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
|
|
|
|
{
|
2019-01-03 15:48:34 +11:00
|
|
|
if (n == 0) {
|
|
|
|
return { 0, 1 };
|
|
|
|
}
|
|
|
|
|
2018-03-13 14:39:51 +11:00
|
|
|
CHECK_NEZ (d);
|
2018-03-11 15:21:36 +11:00
|
|
|
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 };
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-10-20 12:56:28 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
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 };
|
2014-10-20 12:56:28 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
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 };
|
2014-10-20 12:56:28 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
template <typename T>
|
|
|
|
rational<T>
|
2015-07-13 16:27:07 +10:00
|
|
|
rational<T>::operator* (const T rhs) const
|
|
|
|
{
|
2014-10-20 12:56:28 +11:00
|
|
|
return { rhs * n, d };
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
template <typename T>
|
|
|
|
rational<T>
|
2015-07-13 16:27:07 +10:00
|
|
|
rational<T>::operator/ (const T rhs) const
|
|
|
|
{
|
2014-10-20 12:56:28 +11:00
|
|
|
return { n, rhs * d };
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-04-16 15:54:51 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
template <typename ValueT>
|
|
|
|
std::ostream&
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::operator<< (std::ostream &os, cruft::rational<ValueT> val)
|
2018-04-16 15:54:51 +10:00
|
|
|
{
|
|
|
|
return os << val.n << "/" << val.d;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
#define INSTANTIATE(TYPE) \
|
2018-08-05 14:42:02 +10:00
|
|
|
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>);
|
2014-10-20 12:56:28 +11:00
|
|
|
|
2018-04-16 15:54:51 +10:00
|
|
|
INSTANTIATE(uint32_t)
|
|
|
|
INSTANTIATE(int32_t)
|