]> git.donarmstrong.com Git - lilypond.git/blob - flower/include/rational.hh
patch::: 1.3.136.jcn3
[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--2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8
9
10 #ifndef RATIONAL_HH
11 #define RATIONAL_HH
12
13
14 #include "compare.hh"
15 #include "arithmetic-operator.hh"
16 #include "flower-proto.hh"
17 class String;
18
19 /**
20    Rational numbers.  Included is support for + and - infinity.
21  */
22 class Rational {
23   /**
24      Sign of rational.
25      -2, .. 2
26
27      -2,2 is - and + infinity.
28      -1,1 is negative and positive.
29      0 if *this is zero.
30    */
31   int sign_;
32   unsigned int num_, den_;
33   void normalise ();
34   void copy (Rational const &);
35
36 public:
37   void set_infinite (int sign);
38   bool infty_b () const;
39   void invert ();
40   int num () const { return sign_ * num_; }
41   int den () const { return den_; }
42   int num_i () const { return sign_ * num_; }
43   int den_i () const { return den_; }
44   Rational trunc_rat () const;
45   Rational div_rat (Rational) const;
46   Rational mod_rat (Rational) const;
47   void negate ();
48   operator bool () const;
49   operator int () const;
50   operator double () const;
51   Rational operator - () const;
52   /**
53      Initialize to 0. 
54    */
55   Rational ();
56   Rational (int, int =1);
57   Rational (double);
58   Rational (Rational const&r) {   copy (r);}
59
60   Rational &operator = (Rational const &);
61   Rational &operator *= (Rational);
62   Rational &operator /= (Rational);  
63   Rational &operator += (Rational);
64   Rational &operator -= (Rational);
65   Rational &operator %= (Rational);
66   static int compare (Rational const&, Rational const&);
67   int sign () const;
68   String str () const;
69 };
70
71 IMPLEMENT_ARITHMETIC_OPERATOR (Rational, / );
72 IMPLEMENT_ARITHMETIC_OPERATOR (Rational, + );
73 IMPLEMENT_ARITHMETIC_OPERATOR (Rational, * );
74 IMPLEMENT_ARITHMETIC_OPERATOR (Rational, - );
75 IMPLEMENT_ARITHMETIC_OPERATOR (Rational, % );
76
77 INSTANTIATE_COMPARE (Rational const&, Rational::compare);
78
79 int compare (Rational const&,Rational const&);
80 int sign (Rational r);
81
82 inline void
83 Rational::copy (Rational const&r)
84 {
85   sign_ = r.sign_;
86   num_ = r.num_;
87   den_ = r.den_;
88 }
89
90 class ostream;
91 ostream &
92 operator << (ostream &,  Rational);
93
94 const Rational infinity_rat = INT_MAX;
95
96 #endif // RATIONAL_HH