]> git.donarmstrong.com Git - lilypond.git/blob - flower/include/rational.hh
release: 1.0.1
[lilypond.git] / flower / include / rational.hh
1 /*
2   rational.hh -- declare rational helpers
3
4   source file of the Flower Library
5
6   (c)  1997--1998 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9
10 #ifndef RATIONAL_HH
11 #define RATIONAL_HH
12
13 #if PARANOIA
14 #ifndef Rational
15 #define Rational MyRational
16 #endif
17 #endif
18
19 #include "compare.hh"
20 #include "arithmetic-operator.hh"
21 #include "fproto.hh"
22
23 class Rational {
24   int sign_;
25   unsigned int num_, den_;
26   void normalise ();
27   void copy (Rational const &);
28
29 public:
30   void set_infinite (int sign);
31   bool infty_b () const;
32   void invert ();
33   int num  () const { return sign_ * num_; }
34   int den  () const { return den_; }
35   Rational truncated () const;
36   void negate ();
37   operator bool () const;
38   operator int () const;
39   operator double () const;
40   Rational operator - () const;
41   Rational ();
42   Rational (int, int =1);
43   Rational (double);
44   Rational (Rational const&);
45
46   Rational &operator = (Rational const &);
47   Rational &operator *= (Rational);
48   Rational  &operator /= (Rational);  
49   Rational  &operator += (Rational);
50   Rational  &operator -= (Rational);
51   static int compare (Rational const&, Rational const&);
52   int sign () const;
53   String str () const;
54 };
55
56 IMPLEMENT_ARITHMETIC_OPERATOR (Rational, / );
57 IMPLEMENT_ARITHMETIC_OPERATOR (Rational, + );
58 IMPLEMENT_ARITHMETIC_OPERATOR (Rational, * );
59 IMPLEMENT_ARITHMETIC_OPERATOR (Rational, - );
60
61 INSTANTIATE_COMPARE (Rational const&, Rational::compare);
62
63 int compare (Rational const&,Rational const&);
64 int sign (Rational r);
65
66 inline void
67 Rational::copy (Rational const&r)
68 {
69   sign_ = r.sign_;
70   num_ = r.num_;
71   den_ = r.den_;
72 }
73
74 class ostream;
75 ostream &
76 operator << (ostream &,  Rational);
77
78 #endif // RATIONAL_HH