rational_number.hh

Go to the documentation of this file.
00001 // rational_number.hh: this file is part of the Vaucanson project.
00002 //
00003 // Vaucanson, a generic library for finite state machines.
00004 //
00005 // Copyright (C) 2004, 2005 The Vaucanson Group.
00006 //
00007 // This program is free software; you can redistribute it and/or
00008 // modify it under the terms of the GNU General Public License
00009 // as published by the Free Software Foundation; either version 2
00010 // of the License, or (at your option) any later version.
00011 //
00012 // The complete GNU General Public Licence Notice can be found as the
00013 // `COPYING' file in the root directory.
00014 //
00015 // The Vaucanson Group consists of people listed in the `AUTHORS' file.
00016 //
00017 #ifndef VCSN_ALGEBRA_IMPLEMENTATION_SEMIRING_RATIONAL_NUMBER_HH
00018 # define VCSN_ALGEBRA_IMPLEMENTATION_SEMIRING_RATIONAL_NUMBER_HH
00019 
00020 # include <iostream>
00021 
00022 # include <vaucanson/misc/limits.hh>
00023 /* @{ *//* @{ */
00037 namespace vcsn
00038 {
00039   namespace algebra
00040   {
00041 /* @{ *//* @{ */
00044 
00059     class RationalNumber
00060     {
00061     public:
00062 
00064 
00065 
00066       RationalNumber(int num, unsigned int denom);
00068       RationalNumber();
00070       RationalNumber(const RationalNumber& nb);
00080       template <typename T>
00081       explicit RationalNumber(const T num);
00083 
00085 
00086 
00087       const int&        num() const;
00089       const unsigned&   denom() const;
00091 
00092       std::ostream&     print(std::ostream& ostr) const;
00093 
00095 
00096 
00097       RationalNumber    operator+(const RationalNumber& nb) const;
00098       RationalNumber    operator-(const RationalNumber& nb) const;
00099       RationalNumber    operator-() const;
00100       RationalNumber    operator*(const RationalNumber& nb) const;
00101       RationalNumber    operator/(const RationalNumber& nb) const;
00102       RationalNumber&   operator+=(const RationalNumber& nb);
00103       RationalNumber&   operator-=(const RationalNumber& nb);
00104       RationalNumber&   operator*=(const RationalNumber& nb);
00105       RationalNumber&   operator/=(const RationalNumber& nb);
00106       bool      operator<(const RationalNumber& nb) const;
00107       bool      operator>(const RationalNumber& nb) const;
00108       bool      operator<=(const RationalNumber& nb) const;
00109       bool      operator>=(const RationalNumber& nb) const;
00110       bool      operator==(const RationalNumber& nb) const;
00111       bool      operator!=(const RationalNumber& nb) const;
00113 
00115 
00116 
00124       int               to_int() const;
00125       double            to_double() const;
00127 
00128     protected:
00129       int               num_;
00130       unsigned int      denom_;
00131 
00133 
00134 
00142       RationalNumber&   set_result();
00143       RationalNumber&   set_result(int num, unsigned int denom);
00145     };
00146 
00147     // Provides generic operators on rational (calls the explicit generic
00148     // constructor for rational).
00149 # define RATIONAL_NUMBER_OPERATOR(type, op)                     \
00150 template <class T>                                              \
00151 type operator op(const RationalNumber& nb, const T nb2)         \
00152 {                                                               \
00153   return nb op RationalNumber(nb2);                             \
00154 }                                                               \
00155                                                                 \
00156 template <class T>                                              \
00157 type operator op(const T nb2, const RationalNumber& nb)         \
00158 {                                                               \
00159   return RationalNumber(nb2) op nb;                             \
00160 }
00161 
00162 # define RATIONAL_NUMBER_IN_PLACE_OPERATOR(op)                  \
00163 template <class T>                                              \
00164 RationalNumber& operator op(RationalNumber& nb, const T nb2)    \
00165 {                                                               \
00166   return nb op RationalNumber(nb2);                             \
00167 }
00168 
00169     RATIONAL_NUMBER_OPERATOR(bool, <);
00170     RATIONAL_NUMBER_OPERATOR(bool, <=);
00171     RATIONAL_NUMBER_OPERATOR(bool, >);
00172     RATIONAL_NUMBER_OPERATOR(bool, >=);
00173     RATIONAL_NUMBER_OPERATOR(bool, ==);
00174     RATIONAL_NUMBER_OPERATOR(bool, !=);
00175     RATIONAL_NUMBER_OPERATOR(RationalNumber, +);
00176     RATIONAL_NUMBER_OPERATOR(RationalNumber, -);
00177     RATIONAL_NUMBER_OPERATOR(RationalNumber, *);
00178     RATIONAL_NUMBER_OPERATOR(RationalNumber, /);
00179     RATIONAL_NUMBER_IN_PLACE_OPERATOR(+=);
00180     RATIONAL_NUMBER_IN_PLACE_OPERATOR(-=);
00181     RATIONAL_NUMBER_IN_PLACE_OPERATOR(*=);
00182     RATIONAL_NUMBER_IN_PLACE_OPERATOR(/=);
00183 
00184 # undef RATIONAL_NUMBER_OPERATOR
00185 # undef RATIONAL_NUMBER_IN_PLACE_OPERATOR
00186 
00187     // FIXME: Add other goodies for standard library.
00188     std::ostream& operator<<(std::ostream& ostr, const RationalNumber& nb);
00189 
00190    // FIXME: We might prefer to define gcd and lcm somewhere else.
00191     int         gcd(int a, unsigned int b);
00192     int         lcm(int a, unsigned int b);
00193 
00197   } // End of namespace algebra.
00198 } // End of namespace vcsn.
00199 
00200 namespace std
00201 {
00202   template <>
00203   struct numeric_limits< ::vcsn::algebra::RationalNumber >
00204   {
00205     static ::vcsn::algebra::RationalNumber min();
00206     static ::vcsn::algebra::RationalNumber max();
00207   };
00208 
00209 } // End of namespace std.
00210 
00213 namespace utility
00214 {
00215   template <>
00216   struct limits< ::vcsn::algebra::RationalNumber > :
00217     public std::numeric_limits< ::vcsn::algebra::RationalNumber >
00218   {
00219   };
00220 }
00221 
00222 # ifndef VCSN_USE_INTERFACE_ONLY
00223 #  include <vaucanson/algebra/implementation/semiring/rational_number.hxx>
00224 # endif // VCSN_USE_INTERFACE_ONLY
00225 
00226 #endif // ! VCSN_ALGEBRA_IMPLEMENTATION_SEMIRING_RATIONAL_NUMBER_HH

Generated on Fri Jul 28 12:18:51 2006 for Vaucanson by  doxygen 1.4.6