]> git.donarmstrong.com Git - lilypond.git/blob - flower/include/rational.hh
release: 0.1.49
[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 Han-Wen Nienhuys <hanwen@stack.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 public:
29   void set_infinite (int sign);
30   bool infty_b () const;
31   void invert ();
32   int num  () const { return sign_ * num_; }
33   int den  () const { return den_; }
34   Rational truncated () const;
35   void negate ();
36   operator bool () const;
37   operator String () const;
38   Rational operator - () const;
39   Rational ();
40   Rational (int, int =1);
41   Rational (double);
42   Rational (Rational const&);
43
44   Rational &operator = (Rational const &);
45   Rational &operator *= (Rational);
46   Rational  &operator /= (Rational);  
47   Rational  &operator += (Rational);
48   Rational  &operator -= (Rational);
49   static int compare (Rational const&, Rational const&);
50   int sign () const;
51   String str () const;
52 };
53
54 IMPLEMENT_ARITHMETIC_OPERATOR (Rational, / );
55 IMPLEMENT_ARITHMETIC_OPERATOR (Rational, + );
56 IMPLEMENT_ARITHMETIC_OPERATOR (Rational, * );
57 IMPLEMENT_ARITHMETIC_OPERATOR (Rational, - );
58
59 INSTANTIATE_COMPARE (Rational const&, Rational::compare);
60
61 int compare (Rational const&,Rational const&);
62 int sign (Rational r);
63
64 inline void
65 Rational::copy (Rational const&r)
66 {
67   sign_ = r.sign_;
68   num_ = r.num_;
69   den_ = r.den_;
70 }
71
72 class ostream;
73 ostream &
74 operator << (ostream &,  Rational);
75
76 #endif // RATIONAL_HH