10 #include <type_traits>
54 template<class U, class EnableIfFloat = typename std::enable_if<std::is_floating_point<U>::value>::type>
115 denominator_{den == value_type{} ?
throw zero_denominator{
"zero denominator"} : den}
122 : numerator_{
static_cast<T
>(r /
double_divisor + 0.5)}, denominator_{double_divisor}
137 if (std::numeric_limits<T>::digits >= std::numeric_limits<U>::digits)
140 return T{value >> (std::numeric_limits<U>::digits - std::numeric_limits<T>::digits)};
396 template<
class T,
class U>
423 return a.
numerator() * b.denominator() < b.numerator() * a.denominator();
428 bool operator<(rational<T>
const& a, T
const& b)
430 return a.numerator() < b * a.denominator();
435 bool operator<(T const& a, rational<T>
const& b)
437 return a * b.denominator() < b.numerator();
441 template<
class T,
class U>
463 inline bool operator<=(rational<T>
const& a,
rational<T> const& b)
470 inline bool operator<=(rational<T>
const& a, T
const& b)
477 inline bool operator<=(T const& a, rational<T>
const& b)
525 template<
class T,
class Char,
class Traits>
528 typename std::basic_istream<Char, Traits>::sentry sentry{in,
false};
561 template<
class T,
class Char,
class Traits>
562 std::basic_ostream<Char, Traits>& operator<<(std::basic_ostream<Char, Traits>& out,
rational<T> const& rat)
564 typename std::basic_ostream<Char, Traits>::sentry sentry{out};
565 std::ostringstream stream;
566 stream << rat.
numerator() <<
'/' << rat.denominator();
bool operator!=(number a, number b)
rational & operator=(value_type)
Assignment of an integer.
bool operator==(number a, number b)
value_type denominator() const
Return the denominator.
rational< T > operator/(rational< T > lhs, rational< T > const &rhs)
Division.
rational & operator-=(rational const &rhs)
Subtraction assignment operator.
value_type numerator() const
Return the numerator.
rational & operator--()
Pre-decrement.
rational< T > absval(rational< T > const &r)
Save and restore I/O stream flags.
value_type scale(U value)
number operator-(number n)
rational & operator+=(rational const &rhs)
Addition assignment operator.
zero_denominator(std::string const &what)
Construct the exception object.
rational(value_type num=0)
rational & operator*=(rational const &rhs)
Multiplication assignment operator.
rational & operator/=(rational const &rhs)
Division assignment operator.
std::basic_istream< Char, Traits > & operator>>(std::basic_istream< Char, Traits > &in, rational< T > &rat)
Input operator.
Represent a rational number (fraction) as a numerator and denominator.
rational< T > operator+(rational< T > lhs, rational< T > const &rhs)
Addition.
T value_type
Convenience typedef for the integral type of the numerator and denominator.
rational< T > operator*(rational< T > lhs, rational< T > const &rhs)
Multiplication.
rational & operator++()
Pre-increment.
bool operator>(number a, number b)
Exception class if the denominator is ever zero.
static constexpr value_type double_divisor
bool operator>=(number a, number b)
void reduce()
Reduce the numerator and denominator by their GCD.