2 rational.hh -- declare rational helpers
4 source file of the Flower Library
6 (c) 1997--2007 Han-Wen Nienhuys <hanwen@xs4all.nl>
12 #include "flower-proto.hh"
13 #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.
32 void copy (Rational
const &);
35 void set_infinite (int sign
);
36 bool is_infinity () const;
38 I64
numerator () const { return sign_
* num_
; }
39 I64
denominator () const { return den_
; }
40 I64
num () const { return numerator (); }
41 I64
den () const { return denominator (); }
43 Rational
trunc_rat () const;
44 Rational
div_rat (Rational
) const;
45 Rational
mod_rat (Rational
) const;
46 Rational
abs () const;
50 operator double () const { return to_double (); }
51 double to_double () const;
53 Rational
operator - () const;
61 explicit Rational (I64
, I64
);
62 explicit Rational (double);
63 Rational (Rational
const &r
) { copy (r
);}
64 Rational
&operator = (Rational
const &r
)
66 copy (r
); return *this;
69 Rational
&operator *= (Rational
);
70 Rational
&operator /= (Rational
);
71 Rational
&operator += (Rational
);
72 Rational
&operator -= (Rational
);
73 Rational
&operator %= (Rational
);
74 static int compare (Rational
const &, Rational
const &);
76 string
to_string () const;
79 #include "arithmetic-operator.hh"
81 IMPLEMENT_ARITHMETIC_OPERATOR (Rational
, /);
82 IMPLEMENT_ARITHMETIC_OPERATOR (Rational
, +);
83 IMPLEMENT_ARITHMETIC_OPERATOR (Rational
, *);
84 IMPLEMENT_ARITHMETIC_OPERATOR (Rational
, -);
85 IMPLEMENT_ARITHMETIC_OPERATOR (Rational
, %);
87 INSTANTIATE_COMPARE (Rational
const &, Rational::compare
);
89 int compare (Rational
const &, Rational
const &);
90 int sign (Rational r
);
93 Rational::copy (Rational
const &r
)
102 operator << (ostream
&, Rational
);
105 const Rational
infinity_rat (U64_MAX
);
107 #endif // RATIONAL_HH