xoreos  0.0.5
rational.h
Go to the documentation of this file.
1 /* xoreos - A reimplementation of BioWare's Aurora engine
2  *
3  * xoreos is the legal property of its developers, whose names
4  * can be found in the AUTHORS file distributed with this source
5  * distribution.
6  *
7  * xoreos is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 3
10  * of the License, or (at your option) any later version.
11  *
12  * xoreos is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with xoreos. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
25 /* Based on ScummVM (<http://scummvm.org>) code, which is released
26  * under the terms of version 2 or later of the GNU General Public
27  * License.
28  *
29  * The original copyright note in ScummVM reads as follows:
30  *
31  * ScummVM is the legal property of its developers, whose names
32  * are too numerous to list here. Please refer to the COPYRIGHT
33  * file distributed with this source distribution.
34  *
35  * This program is free software; you can redistribute it and/or
36  * modify it under the terms of the GNU General Public License
37  * as published by the Free Software Foundation; either version 2
38  * of the License, or (at your option) any later version.
39  *
40  * This program is distributed in the hope that it will be useful,
41  * but WITHOUT ANY WARRANTY; without even the implied warranty of
42  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
43  * GNU General Public License for more details.
44  *
45  * You should have received a copy of the GNU General Public License
46  * along with this program; if not, write to the Free Software
47  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
48  */
49 
50 #ifndef COMMON_RATIONAL_H
51 #define COMMON_RATIONAL_H
52 
53 namespace Common {
54 
56 class Rational {
57 public:
58  Rational();
59  Rational(int num);
60  Rational(int num, int denom);
61 
62  Rational &operator=(const Rational &right);
63  Rational &operator=(int right);
64 
65  Rational &operator+=(const Rational &right);
66  Rational &operator-=(const Rational &right);
67  Rational &operator*=(const Rational &right);
68  Rational &operator/=(const Rational &right);
69 
70  Rational &operator+=(int right);
71  Rational &operator-=(int right);
72  Rational &operator*=(int right);
73  Rational &operator/=(int right);
74 
75  const Rational operator-() const;
76 
77  const Rational operator+(const Rational &right) const;
78  const Rational operator-(const Rational &right) const;
79  const Rational operator*(const Rational &right) const;
80  const Rational operator/(const Rational &right) const;
81 
82  const Rational operator+(int right) const;
83  const Rational operator-(int right) const;
84  const Rational operator*(int right) const;
85  const Rational operator/(int right) const;
86 
87  bool operator==(const Rational &right) const;
88  bool operator!=(const Rational &right) const;
89  bool operator>(const Rational &right) const;
90  bool operator<(const Rational &right) const;
91  bool operator>=(const Rational &right) const;
92  bool operator<=(const Rational &right) const;
93 
94  bool operator==(int right) const;
95  bool operator!=(int right) const;
96  bool operator>(int right) const;
97  bool operator<(int right) const;
98  bool operator>=(int right) const;
99  bool operator<=(int right) const;
100 
101  void invert();
102  Rational getInverse() const;
103 
104  int toInt() const;
105  double toDouble() const;
106 
107  int getNumerator() const { return _num; }
108  int getDenominator() const { return _denom; }
109 
110 private:
111  int _num;
112  int _denom;
113 
114  void cancel();
115 };
116 
117 const Rational operator+(int left, const Rational &right);
118 const Rational operator-(int left, const Rational &right);
119 const Rational operator*(int left, const Rational &right);
120 const Rational operator/(int left, const Rational &right);
121 
122 bool operator==(int left, const Rational &right);
123 bool operator!=(int left, const Rational &right);
124 bool operator>(int left, const Rational &right);
125 bool operator<(int left, const Rational &right);
126 bool operator>=(int left, const Rational &right);
127 bool operator<=(int left, const Rational &right);
128 
129 } // End of namespace Common
130 
131 #endif
Rational & operator=(const Rational &right)
Definition: rational.cpp:91
Definition: 2dafile.h:39
A simple rational class that holds fractions.
Definition: rational.h:56
const Rational operator*(const Rational &right) const
Definition: rational.cpp:183
bool operator!=(const Rational &right) const
Definition: rational.cpp:223
Rational & operator/=(const Rational &right)
Definition: rational.cpp:147
int toInt() const
Definition: rational.cpp:287
Rational & operator-=(const Rational &right)
Definition: rational.cpp:120
const Rational operator-(int left, const Rational &right)
Definition: rational.cpp:301
bool operator<=(const Rational &right) const
Definition: rational.cpp:239
bool operator<(int left, const Rational &right)
Definition: rational.cpp:331
double toDouble() const
Definition: rational.cpp:291
const Rational operator*(int left, const Rational &right)
Definition: rational.cpp:307
const Rational operator/(const Rational &right) const
Definition: rational.cpp:189
bool operator<=(int left, const Rational &right)
Definition: rational.cpp:339
bool operator>(const Rational &right) const
Definition: rational.cpp:227
bool operator>=(int left, const Rational &right)
Definition: rational.cpp:335
bool operator==(const Rational &right) const
Definition: rational.cpp:219
Rational & operator+=(const Rational &right)
Definition: rational.cpp:105
int getDenominator() const
Definition: rational.h:108
const Rational operator+(const Rational &right) const
Definition: rational.cpp:171
bool operator==(int left, const Rational &right)
Definition: rational.cpp:319
const Rational operator/(int left, const Rational &right)
Definition: rational.cpp:313
bool operator>(int left, const Rational &right)
Definition: rational.cpp:327
Rational & operator*=(const Rational &right)
Definition: rational.cpp:135
bool operator!=(int left, const Rational &right)
Definition: rational.cpp:323
bool operator>=(const Rational &right) const
Definition: rational.cpp:235
const Rational operator+(int left, const Rational &right)
Definition: rational.cpp:295
bool operator<(const Rational &right) const
Definition: rational.cpp:231
Rational getInverse() const
Definition: rational.cpp:279
const Rational operator-() const
Definition: rational.cpp:167
int getNumerator() const
Definition: rational.h:107