2 rational.hh -- declare rational helpers
4 source file of the Flower Library
6 (c) 1997--2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>
14 #include "arithmetic-operator.hh"
15 #include "flower-proto.hh"
20 Rational numbers. Included is support for + and - infinity.
27 -2,2 is - and + infinity.
28 -1,1 is negative and positive.
32 unsigned int num_, den_;
34 void copy (Rational const &);
37 void set_infinite (int sign);
40 return sign_ == 2 || sign_ == -2;
43 int num () const { return sign_ * num_; }
44 int den () const { return den_; }
46 Rational trunc_rat () const;
47 Rational div_rat (Rational) const;
48 Rational mod_rat (Rational) const;
50 // operator bool () const;
52 operator double () const;
53 Rational operator - () const;
61 Rational (Rational const&r) { copy (r);}
63 Rational &operator = (Rational const &);
64 Rational &operator *= (Rational);
65 Rational &operator /= (Rational);
66 Rational &operator += (Rational);
67 Rational &operator -= (Rational);
68 Rational &operator %= (Rational);
69 static int compare (Rational const&, Rational const&);
74 IMPLEMENT_ARITHMETIC_OPERATOR (Rational, / );
75 IMPLEMENT_ARITHMETIC_OPERATOR (Rational, + );
76 IMPLEMENT_ARITHMETIC_OPERATOR (Rational, * );
77 IMPLEMENT_ARITHMETIC_OPERATOR (Rational, - );
78 IMPLEMENT_ARITHMETIC_OPERATOR (Rational, % );
80 INSTANTIATE_COMPARE (Rational const&, Rational::compare);
82 int compare (Rational const&,Rational const&);
83 int sign (Rational r);
86 Rational::copy (Rational const&r)
94 operator << (ostream &, Rational);
96 const Rational infinity_rat = INT_MAX;