2 rational.hh -- declare rational helpers
4 source file of the Flower Library
6 (c) 1997--2004 Han-Wen Nienhuys <hanwen@cs.uu.nl>
16 Rational numbers. Included is support for + and - infinity.
23 -2,2 is - and + infinity.
24 -1,1 is negative and positive.
28 unsigned int num_, den_;
30 void copy (Rational const &);
33 void set_infinite (int sign);
34 bool is_infinity () const;
36 int numerator () const { return sign_ * num_; }
37 int denominator () const { return den_; }
38 int num () const { return numerator(); }
39 int den () const { return denominator(); }
41 Rational trunc_rat () const;
42 Rational div_rat (Rational) const;
43 Rational mod_rat (Rational) const;
45 // operator bool () const;
47 operator double () const;
48 Rational operator - () const;
56 Rational (Rational const&r) { copy (r);}
57 Rational &operator = (Rational const &r) {
58 copy (r); return *this;
61 Rational &operator *= (Rational);
62 Rational &operator /= (Rational);
63 Rational &operator += (Rational);
64 Rational &operator -= (Rational);
65 Rational &operator %= (Rational);
66 static int compare (Rational const&, Rational const&);
68 String to_string () const;
71 IMPLEMENT_ARITHMETIC_OPERATOR (Rational, / );
72 IMPLEMENT_ARITHMETIC_OPERATOR (Rational, + );
73 IMPLEMENT_ARITHMETIC_OPERATOR (Rational, * );
74 IMPLEMENT_ARITHMETIC_OPERATOR (Rational, - );
75 IMPLEMENT_ARITHMETIC_OPERATOR (Rational, % );
77 INSTANTIATE_COMPARE (Rational const&, Rational::compare);
79 int compare (Rational const&,Rational const&);
80 int sign (Rational r);
83 Rational::copy (Rational const&r)
92 operator << (ostream &, Rational);
95 const Rational infinity_rat = INT_MAX;