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