include/types.hpp
author Dmitriy Morozov <morozov@cs.duke.edu>
Mon, 04 Dec 2006 22:39:35 -0500
changeset 3 76a2c73ecbbf
parent 0 d95020656286
permissions -rw-r--r--
Moved config to a separate directory

/* Implementations */

template<typename T>
Rational<T>::
Rational(T v): 
	numerator(v), denominator(1)					
{}

template<typename T>
Rational<T>::
Rational(const Self& other): 
	numerator(other.numerator), denominator(other.denominator)
{}

template<typename T>
typename Rational<T>::Self& 
Rational<T>::
operator/=(const Self& rhs)
{ 
	numerator *= rhs.denominator; 
	denominator *= rhs.numerator; 

	if (denominator < 0)
	{
		numerator = -numerator;
		denominator = -denominator;
	}
	
	return *this; 
}

template<typename T>
bool 
Rational<T>::
operator<(const Self& rhs) const
{ return (numerator * rhs.denominator < denominator * rhs.numerator); }

template<typename T>
bool 
Rational<T>::
operator<=(const Self& rhs) const
{ return (numerator * rhs.denominator <= denominator * rhs.numerator); }

template<typename T>
bool 
Rational<T>::
operator>(const Self& rhs) const
{ return rhs < (*this); }

template<typename T>
bool 
Rational<T>::
operator>=(const Self& rhs) const
{ return rhs <= (*this); }

template<typename T>
typename Rational<T>::Self& 
Rational<T>::
operator=(const Self& rhs)
{ numerator = rhs.numerator; denominator = rhs.denominator; return *this; }

template<typename T>
std::ostream& 
Rational<T>::
operator<<(std::ostream& out) const
{ out << numerator << " / " << denominator << " = " << (numerator/denominator); return out; }

template<typename T>
RealType 
Rational<T>::
to_real() const
{ return numerator/denominator; }

		
template<typename T>
std::ostream& operator<<(std::ostream& out, const Rational<T>& r)
{
	return r.operator<<(out);
}