00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
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
00148
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
00188 std::ostream& operator<<(std::ostream& ostr, const RationalNumber& nb);
00189
00190
00191 int gcd(int a, unsigned int b);
00192 int lcm(int a, unsigned int b);
00193
00197 }
00198 }
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 }
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