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, 2006 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 
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 
00087         const int& num () const;
00089         const unsigned& denom () const;
00091 
00092         std::ostream&   print (std::ostream& ostr) const;
00093 
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 
00103         RationalNumber& operator+= (const RationalNumber& nb);
00104         RationalNumber& operator-= (const RationalNumber& nb);
00105         RationalNumber& operator*= (const RationalNumber& nb);
00106         RationalNumber& operator/= (const RationalNumber& nb);
00107 
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;
00112         bool operator== (const RationalNumber& nb) const;
00113         bool operator!= (const RationalNumber& nb) const;
00115 
00118 
00126         int    to_int () const;
00127         double to_double () const;
00129 
00130       protected:
00131         int num_;
00132         unsigned int denom_;
00133 
00136 
00144         RationalNumber& set_result ();
00145         RationalNumber& set_result (int num, unsigned int denom);
00147     };
00148 
00149     // Provides generic operators on rational (calls the explicit generic
00150     // constructor for rational).
00151 # define RATIONAL_NUMBER_OPERATOR(type, op)                     \
00152     template <class T>                                          \
00153     type                                                        \
00154     operator op (const RationalNumber& nb, const T nb2)         \
00155     {                                                           \
00156       return nb op RationalNumber (nb2);                        \
00157     }                                                           \
00158                                                                 \
00159     template <class T>                                          \
00160     type                                                        \
00161     operator op (const T nb2, const RationalNumber& nb)         \
00162     {                                                           \
00163       return RationalNumber (nb2) op nb;                        \
00164     }
00165 
00166     RATIONAL_NUMBER_OPERATOR (bool, <);
00167     RATIONAL_NUMBER_OPERATOR (bool, <=);
00168     RATIONAL_NUMBER_OPERATOR (bool, >);
00169     RATIONAL_NUMBER_OPERATOR (bool, >=);
00170     RATIONAL_NUMBER_OPERATOR (bool, ==);
00171     RATIONAL_NUMBER_OPERATOR (bool, !=);
00172 
00173     RATIONAL_NUMBER_OPERATOR (RationalNumber, +);
00174     RATIONAL_NUMBER_OPERATOR (RationalNumber, -);
00175     RATIONAL_NUMBER_OPERATOR (RationalNumber, *);
00176     RATIONAL_NUMBER_OPERATOR (RationalNumber, /);
00177 
00178 # undef RATIONAL_NUMBER_OPERATOR
00179 
00180 
00181 # define RATIONAL_NUMBER_IN_PLACE_OPERATOR(op)                          \
00182     template <class T>                                                  \
00183     RationalNumber&                                                     \
00184     operator op (RationalNumber& nb, const T nb2)                       \
00185     {                                                                   \
00186       return nb op RationalNumber (nb2);                                \
00187     }
00188 
00189     RATIONAL_NUMBER_IN_PLACE_OPERATOR (+=);
00190     RATIONAL_NUMBER_IN_PLACE_OPERATOR (-=);
00191     RATIONAL_NUMBER_IN_PLACE_OPERATOR (*=);
00192     RATIONAL_NUMBER_IN_PLACE_OPERATOR (/=);
00193 
00194 # undef RATIONAL_NUMBER_IN_PLACE_OPERATOR
00195 
00196     // @todo FIXME: Add other goodies for standard library.
00197     std::ostream& operator<< (std::ostream& ostr, const RationalNumber& nb);
00198 
00199     // @todo FIXME: We might prefer to define gcd and lcm somewhere else.
00200     int gcd (int a, unsigned int b);
00201     int lcm (int a, unsigned int b);
00202 
00206   } // End of namespace algebra.
00207 } // End of namespace vcsn.
00208 
00209 namespace std
00210 {
00211   template <>
00212   struct numeric_limits< ::vcsn::algebra::RationalNumber >
00213   {
00214       static inline ::vcsn::algebra::RationalNumber min ();
00215       static inline ::vcsn::algebra::RationalNumber max ();
00216   };
00217 
00218 } // End of namespace std.
00219 
00222 namespace vcsn
00223 {
00224   namespace misc
00225   {
00226     template <>
00227     struct limits< ::vcsn::algebra::RationalNumber > :
00228         public std::numeric_limits< ::vcsn::algebra::RationalNumber >
00229     {
00230     };
00231   } // misc
00232 } // vcsn
00233 
00234 # if !defined VCSN_USE_INTERFACE_ONLY || defined VCSN_USE_LIB
00235 #  include <vaucanson/algebra/implementation/semiring/rational_number.hxx>
00236 # endif // VCSN_USE_INTERFACE_ONLY
00237 
00238 #endif // ! VCSN_ALGEBRA_IMPLEMENTATION_SEMIRING_RATIONAL_NUMBER_HH

Generated on Wed Jun 13 17:00:28 2007 for Vaucanson by  doxygen 1.5.1