include/types.hpp
author Dmitriy Morozov <morozov@cs.duke.edu>
Mon, 30 Oct 2006 14:20:45 -0500
changeset 0 d95020656286
permissions -rw-r--r--
Initial conversion to Dionysus architecture

/* 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);
}