]> git.donarmstrong.com Git - lilypond.git/blob - flower/include/rational.hh
Add '-dcrop' option to ps and svg backends
[lilypond.git] / flower / include / rational.hh
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2015 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 "compare.hh"
24 #include "flower-proto.hh"
25 #include "std-string.hh"
26 #include <limits.h>
27
28 /**
29    Rational numbers.  Included is support for + and - infinity.
30 */
31 class Rational
32 {
33   /**
34      Sign of rational.
35      -2, .. 2
36
37      -2,2 is - and + infinity.
38      -1,1 is negative and positive.
39      0 if *this is zero.
40   */
41   int sign_;
42   U64 num_, den_;
43   void normalize ();
44   void copy (Rational const &);
45
46 public:
47   void set_infinite (int sign);
48   bool is_infinity () const;
49   void invert ();
50   I64 numerator () const { return sign_ * num_; }
51   I64 denominator () const { return den_; }
52   I64 num () const { return numerator (); }
53   I64 den () const { return denominator (); }
54
55   Rational trunc_rat () const;
56   Rational div_rat (Rational) const;
57   Rational mod_rat (Rational) const;
58   Rational abs () const;
59   void negate ();
60   int to_int () const;
61
62   operator double () const { return to_double (); }
63   double to_double () const;
64
65   Rational operator - () const;
66   /**
67      Initialize to 0.
68   */
69   Rational ();
70   Rational (int);
71   Rational (I64);
72   Rational (U64);
73   explicit Rational (I64, I64);
74   explicit Rational (double);
75   Rational (Rational const &r) { copy (r);}
76   Rational &operator = (Rational const &r)
77   {
78     copy (r);
79     return *this;
80   }
81
82   Rational &operator *= (Rational);
83   Rational &operator /= (Rational);
84   Rational &operator += (Rational);
85   Rational &operator -= (Rational);
86   Rational &operator %= (Rational);
87   static int compare (Rational const &, Rational const &);
88   int sign () const;
89   string to_string () const;
90 };
91
92 #include "arithmetic-operator.hh"
93
94 IMPLEMENT_ARITHMETIC_OPERATOR (Rational, / );
95 IMPLEMENT_ARITHMETIC_OPERATOR (Rational, +);
96 IMPLEMENT_ARITHMETIC_OPERATOR (Rational, *);
97 IMPLEMENT_ARITHMETIC_OPERATOR (Rational, -);
98 IMPLEMENT_ARITHMETIC_OPERATOR (Rational, % );
99
100 INSTANTIATE_COMPARE (Rational const &, Rational::compare);
101
102 int compare (Rational const &, Rational const &);
103 int sign (Rational r);
104
105 inline void
106 Rational::copy (Rational const &r)
107 {
108   sign_ = r.sign_;
109   num_ = r.num_;
110   den_ = r.den_;
111 }
112
113 #if 0
114 ostream &
115 operator << (ostream &, Rational);
116 #endif
117
118 const Rational infinity_rat (U64_MAX);
119
120 #endif // RATIONAL_HH