]> git.donarmstrong.com Git - lilypond.git/blob - flower/include/rational.hh
Merge branch 'master' into lilypond/translation
[lilypond.git] / flower / include / rational.hh
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2011 Han-Wen Nienhuys <hanwen@xs4all.nl>
5
6   LilyPond is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   LilyPond is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #ifndef RATIONAL_HH
21 #define RATIONAL_HH
22
23 #include "flower-proto.hh"
24 #include "std-string.hh"
25 #include <limits.h>
26
27 /**
28    Rational numbers.  Included is support for + and - infinity.
29 */
30 class Rational
31 {
32   /**
33      Sign of rational.
34      -2, .. 2
35
36      -2,2 is - and + infinity.
37      -1,1 is negative and positive.
38      0 if *this is zero.
39   */
40   int sign_;
41   U64 num_, den_;
42   void normalize ();
43   void copy (Rational const &);
44
45 public:
46   void set_infinite (int sign);
47   bool is_infinity () const;
48   void invert ();
49   I64 numerator () const { return sign_ * num_; }
50   I64 denominator () const { return den_; }
51   I64 num () const { return numerator (); }
52   I64 den () const { return denominator (); }
53
54   Rational trunc_rat () const;
55   Rational div_rat (Rational) const;
56   Rational mod_rat (Rational) const;
57   Rational abs () const;
58   void negate ();
59   int to_int () const;
60
61   operator double () const { return to_double (); }
62   double to_double () const;
63
64   Rational operator - () const;
65   /**
66      Initialize to 0.
67   */
68   Rational ();
69   Rational (int);
70   Rational (I64);
71   Rational (U64);
72   explicit Rational (I64, I64);
73   explicit Rational (double);
74   Rational (Rational const &r) { copy (r);}
75   Rational &operator = (Rational const &r)
76   {
77     copy (r); return *this;
78   }
79
80   Rational &operator *= (Rational);
81   Rational &operator /= (Rational);
82   Rational &operator += (Rational);
83   Rational &operator -= (Rational);
84   Rational &operator %= (Rational);
85   static int compare (Rational const &, Rational const &);
86   int sign () const;
87   string to_string () const;
88 };
89
90 #include "arithmetic-operator.hh"
91
92 IMPLEMENT_ARITHMETIC_OPERATOR (Rational, /);
93 IMPLEMENT_ARITHMETIC_OPERATOR (Rational, +);
94 IMPLEMENT_ARITHMETIC_OPERATOR (Rational, *);
95 IMPLEMENT_ARITHMETIC_OPERATOR (Rational, -);
96 IMPLEMENT_ARITHMETIC_OPERATOR (Rational, %);
97
98 INSTANTIATE_COMPARE (Rational const &, Rational::compare);
99
100 int compare (Rational const &, Rational const &);
101 int sign (Rational r);
102
103 inline void
104 Rational::copy (Rational const &r)
105 {
106   sign_ = r.sign_;
107   num_ = r.num_;
108   den_ = r.den_;
109 }
110
111 #if 0
112 ostream &
113 operator << (ostream &, Rational);
114 #endif
115
116 const Rational infinity_rat (U64_MAX);
117
118 #endif // RATIONAL_HH