2014-07-02 15:49:27 +10:00
|
|
|
/*
|
2015-04-13 18:05:28 +10:00
|
|
|
* 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
|
2014-07-02 15:49:27 +10:00
|
|
|
*
|
2015-04-13 18:05:28 +10:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2014-07-02 15:49:27 +10:00
|
|
|
*
|
2015-04-13 18:05:28 +10:00
|
|
|
* 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.
|
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"
|
|
|
|
|
2014-07-02 15:49:27 +10:00
|
|
|
#include <cstdint>
|
|
|
|
|
|
|
|
using util::rational;
|
|
|
|
|
|
|
|
|
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-07-13 16:27:07 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
template <typename T>
|
|
|
|
rational<T>
|
|
|
|
rational<T>::reduced (void) const
|
|
|
|
{
|
|
|
|
auto x = gcd (abs (n), abs (d));
|
|
|
|
|
|
|
|
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 };
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2015-07-13 16:27:07 +10:00
|
|
|
template <typename T>
|
2014-10-20 12:56:28 +11:00
|
|
|
rational<T>
|
2015-07-13 16:27:07 +10:00
|
|
|
util::operator/ (T lhs, rational<T> rhs) {
|
2014-10-20 12:56:28 +11:00
|
|
|
return rhs.inverse () * lhs;
|
|
|
|
}
|
|
|
|
|
2014-07-02 15:49:27 +10:00
|
|
|
//-----------------------------------------------------------------------------
|
2015-07-13 16:27:07 +10:00
|
|
|
template struct util::rational<uint32_t>;
|
|
|
|
template struct util::rational< int32_t>;
|
2014-10-20 12:56:28 +11:00
|
|
|
|
2015-07-13 16:27:07 +10:00
|
|
|
template util::rational<uint32_t> util::operator/ (uint32_t, util::rational<uint32_t>);
|