/* Copyright (c) 1997-2006
   Ewgenij Gawrilow, Michael Joswig (Technische Universitaet Berlin, Germany)
   http://www.math.tu-berlin.de/polymake,  mailto:polymake@math.tu-berlin.de

   This program is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by the
   Free Software Foundation; either version 2, or (at your option) any
   later version: http://www.gnu.org/licenses/gpl.txt.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
*/

#ifndef _POLYMAKE_RATIONAL_H
#define _POLYMAKE_RATIONAL_H "$Project: polymake $$Id: Rational.h 7315 2006-04-02 21:37:53Z gawrilow $"

#include_next <Rational.h>
#ifndef _POLYMAKE_OPERATIONS_BASIC_DEFS_H
#  include <operations_basic_defs.h>
#endif
#ifndef _POLYMAKE_COMPARATORS_BASIC_DEFS_H
#  include <comparators_basic_defs.h>
#endif
#ifndef _POLYMAKE_CONVERTERS_BASIC_DEFS_H
#  include <converters_basic_defs.h>
#endif

namespace pm {

template <> inline
cmp_value sign(const Rational& a) { return sign(mpq_numref(a.get_rep())->_mp_size); }

template <> inline
Rational& inv_sign(Rational& a) { return a.negate(); }

namespace operations {

template <>
struct cmp_numeric<Rational> : cmp_common<Rational> {
   using cmp_common<Rational>::operator();

   result_type operator() (const Rational& a, const Rational& b) const
   {
      return sign(a.compare(b));
   }
   result_type operator() (const Rational& a, long b) const
   {
      return sign(a.compare(b));
   }
   result_type operator() (long a, const Rational& b) const
   {
      return sign(-b.compare(a));
   }
   result_type operator() (const Rational& a, const Integer& b) const
   {
      return sign(a.compare(b));
   }
   result_type operator() (const Integer& a, const Rational& b) const
   {
      return sign(-b.compare(a));
   }
   result_type operator() (const Rational& a, double b) const
   {
      return sign(double(a)-b);
   }
   result_type operator() (double a, const Rational& b) const
   {
      return sign(a-double(b));
   }
   template <typename Iterator2>
   result_type operator() (partial_left, const Rational& a, const Iterator2&) const
   {
      return cmp_value(sign(a));
   }
   template <typename Iterator1>
   result_type operator() (partial_right, const Iterator1&, const Rational& b) const
   {
      return cmp_value(-sign(b));
   }
};

cmp_value cmp_numeric<Integer>::operator() (const Integer& a, const Rational& b) const
{
   return sign(-b.compare(a));
}
cmp_value cmp_numeric<Integer>::operator() (const Rational& a, const Integer& b) const
{
   return sign(a.compare(b));
}

} // end namespace operations

template <>
struct conv<Rational, std::string> {
   typedef Rational argument_type;
   typedef std::string result_type;
   result_type operator() (const Rational& a) const { return a.to_string(); }
};

} // end namespace pm

#endif // _POLYMAKE_RATIONAL_H

// Local Variables:
// mode:C++
// End:
