]> git.donarmstrong.com Git - lilypond.git/blob - flower/include/rational.hh
Run grand-replace (issue 3765)
[lilypond.git] / flower / include / rational.hh
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2014 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);
78     return *this;
79   }
80
81   Rational &operator *= (Rational);
82   Rational &operator /= (Rational);
83   Rational &operator += (Rational);
84   Rational &operator -= (Rational);
85   Rational &operator %= (Rational);
86   static int compare (Rational const &, Rational const &);
87   int sign () const;
88   string to_string () const;
89 };
90
91 #include "arithmetic-operator.hh"
92
93 IMPLEMENT_ARITHMETIC_OPERATOR (Rational, / );
94 IMPLEMENT_ARITHMETIC_OPERATOR (Rational, +);
95 IMPLEMENT_ARITHMETIC_OPERATOR (Rational, *);
96 IMPLEMENT_ARITHMETIC_OPERATOR (Rational, -);
97 IMPLEMENT_ARITHMETIC_OPERATOR (Rational, % );
98
99 INSTANTIATE_COMPARE (Rational const &, Rational::compare);
100
101 int compare (Rational const &, Rational const &);
102 int sign (Rational r);
103
104 inline void
105 Rational::copy (Rational const &r)
106 {
107   sign_ = r.sign_;
108   num_ = r.num_;
109   den_ = r.den_;
110 }
111
112 #if 0
113 ostream &
114 operator << (ostream &, Rational);
115 #endif
116
117 const Rational infinity_rat (U64_MAX);
118
119 #endif // RATIONAL_HH