7#ifndef gbLAB_Rational_h_
8#define gbLAB_Rational_h_
14template <
typename IntScalarType>
struct Rational {
27 Rational(
const IntScalarType &n_in,
const IntScalarType &d_in)
29 IntegerMath<IntScalarType>::gcd(abs(n_in), abs(d_in))),
31 IntegerMath<IntScalarType>::gcd(abs(n_in), abs(d_in))) {}
33 double asDouble()
const {
return double(
n) / double(
d); }
46 return n == 0 && other == 0;
52 return !(*
this == other);
88 os << r.
n <<
"/" << r.
d;
97template <
typename IntScalarType>
98struct NumTraits<
oILAB::Rational<IntScalarType>>
99 : NumTraits<IntScalarType>
109 RequireInitialization = 1,
110 ReadCost = int(NumTraits<IntScalarType>::ReadCost),
111 AddCost = int(NumTraits<IntScalarType>::AddCost),
112 MulCost = int(NumTraits<IntScalarType>::MulCost)
Rational(const IntScalarType &n_in, const IntScalarType &d_in)
Rational operator-(const IntScalarType &i) const
Rational operator/(const Rational &r2) const
Rational operator*(const IntScalarType &i) const
Rational operator*(const Rational &r2) const
friend std::ostream & operator<<(std::ostream &os, const Rational &r)
bool operator==(const IntScalarType &other) const
Rational operator+(const Rational &r2) const
Rational(const IntScalarType &n_in)
Rational operator-(const Rational &r2) const
Rational operator/(const IntScalarType &i) const
Rational operator+(const IntScalarType &i) const
bool operator!=(const IntScalarType &other) const