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"
15 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;
44 Rational abs () const;
48 operator double () const { return to_double (); }
49 double to_double () const;
51 Rational operator - () const;
57 explicit Rational (int, int);
58 explicit Rational (double);
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 string to_string () const;
75 #include "arithmetic-operator.hh"
77 IMPLEMENT_ARITHMETIC_OPERATOR (Rational, /);
78 IMPLEMENT_ARITHMETIC_OPERATOR (Rational, +);
79 IMPLEMENT_ARITHMETIC_OPERATOR (Rational, *);
80 IMPLEMENT_ARITHMETIC_OPERATOR (Rational, -);
81 IMPLEMENT_ARITHMETIC_OPERATOR (Rational, %);
83 INSTANTIATE_COMPARE (Rational const &, Rational::compare);
85 int compare (Rational const &, Rational const &);
86 int sign (Rational r);
89 Rational::copy (Rational const &r)
98 operator << (ostream &, Rational);
101 const Rational infinity_rat (INT_MAX);
103 #endif // RATIONAL_HH