2 rational.hh -- declare rational helpers
4 source file of the Flower Library
6 (c) 1997--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
12 #include "std-string.hh"
17 Rational numbers. Included is support for + and - infinity.
25 -2,2 is - and + infinity.
26 -1,1 is negative and positive.
30 unsigned int num_, den_;
32 void copy (Rational const &);
35 void set_infinite (int sign);
36 bool is_infinity () const;
38 int numerator () const { return sign_ * num_; }
39 int denominator () const { return den_; }
40 int num () const { return numerator (); }
41 int den () const { return denominator (); }
43 Rational trunc_rat () const;
44 Rational div_rat (Rational) const;
45 Rational mod_rat (Rational) const;
48 operator bool () const;
49 operator double () const;
51 Rational operator - () const;
59 Rational (Rational const &r) { copy (r);}
60 Rational &operator = (Rational const &r)
62 copy (r); return *this;
65 Rational &operator *= (Rational);
66 Rational &operator /= (Rational);
67 Rational &operator += (Rational);
68 Rational &operator -= (Rational);
69 Rational &operator %= (Rational);
70 static int compare (Rational const &, Rational const &);
72 Std_string to_string () const;
75 IMPLEMENT_ARITHMETIC_OPERATOR (Rational, /);
76 IMPLEMENT_ARITHMETIC_OPERATOR (Rational, +);
77 IMPLEMENT_ARITHMETIC_OPERATOR (Rational, *);
78 IMPLEMENT_ARITHMETIC_OPERATOR (Rational, -);
79 IMPLEMENT_ARITHMETIC_OPERATOR (Rational, %);
81 INSTANTIATE_COMPARE (Rational const &, Rational::compare);
83 int compare (Rational const &, Rational const &);
84 int sign (Rational r);
87 Rational::copy (Rational const &r)
96 operator << (ostream &, Rational);
99 const Rational infinity_rat = INT_MAX;
101 #endif // RATIONAL_HH