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