1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program 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 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program 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 this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #ifndef COMMON_RATIONAL_H
24 #define COMMON_RATIONAL_H
25 
26 #include "common/scummsys.h"
27 #include "common/frac.h"
28 
29 namespace Common {
30 
31 /**
32  * @defgroup common_rational Rational class
33  * @ingroup common
34  *
35  * @brief API for rational class.
36  *
37  * @{
38  */
39 
40 /** A simple rational class that holds fractions. */
41 class Rational {
42 public:
43 	Rational();
44 	Rational(int num);
45 	Rational(int num, int denom);
46 	Rational(const Rational &rational);
47 
48 	Rational &operator=(const Rational &right);
49 	Rational &operator=(int right);
50 
51 	Rational &operator+=(const Rational &right);
52 	Rational &operator-=(const Rational &right);
53 	Rational &operator*=(const Rational &right);
54 	Rational &operator/=(const Rational &right);
55 
56 	Rational &operator+=(int right);
57 	Rational &operator-=(int right);
58 	Rational &operator*=(int right);
59 	Rational &operator/=(int right);
60 
61 	const Rational operator-() const;
62 
63 	const Rational operator+(const Rational &right) const;
64 	const Rational operator-(const Rational &right) const;
65 	const Rational operator*(const Rational &right) const;
66 	const Rational operator/(const Rational &right) const;
67 
68 	const Rational operator+(int right) const;
69 	const Rational operator-(int right) const;
70 	const Rational operator*(int right) const;
71 	const Rational operator/(int right) const;
72 
73 	bool operator==(const Rational &right) const;
74 	bool operator!=(const Rational &right) const;
75 	bool operator>(const Rational &right) const;
76 	bool operator<(const Rational &right) const;
77 	bool operator>=(const Rational &right) const;
78 	bool operator<=(const Rational &right) const;
79 
80 	bool operator==(int right) const;
81 	bool operator!=(int right) const;
82 	bool operator>(int right) const;
83 	bool operator<(int right) const;
84 	bool operator>=(int right) const;
85 	bool operator<=(int right) const;
86 
87 	void invert();
88 	Rational getInverse() const;
89 
90 	int toInt() const;
91 	double toDouble() const;
92 	frac_t toFrac() const;
93 
getNumerator()94 	int getNumerator() const { return _num; }
getDenominator()95 	int getDenominator() const { return _denom; }
96 
isOne()97 	bool isOne() const { return _num == _denom; }
98 
99 	void debugPrint(int debuglevel = 0, const char *caption = "Rational:") const;
100 
101 private:
102 	int _num;
103 	int _denom;
104 
105 	void cancel();
106 };
107 
108 const Rational operator+(int left, const Rational &right);
109 const Rational operator-(int left, const Rational &right);
110 const Rational operator*(int left, const Rational &right);
111 const Rational operator/(int left, const Rational &right);
112 
113 bool operator==(int left, const Rational &right);
114 bool operator!=(int left, const Rational &right);
115 bool operator>(int left, const Rational &right);
116 bool operator<(int left, const Rational &right);
117 bool operator>=(int left, const Rational &right);
118 bool operator<=(int left, const Rational &right);
119 
120 /** @} */
121 
122 } // End of namespace Common
123 
124 #endif
125