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 /** A simple rational class that holds fractions. */
32 class Rational {
33 public:
34 	Rational();
35 	Rational(int num);
36 	Rational(int num, int denom);
37 
38 	Rational &operator=(const Rational &right);
39 	Rational &operator=(int right);
40 
41 	Rational &operator+=(const Rational &right);
42 	Rational &operator-=(const Rational &right);
43 	Rational &operator*=(const Rational &right);
44 	Rational &operator/=(const Rational &right);
45 
46 	Rational &operator+=(int right);
47 	Rational &operator-=(int right);
48 	Rational &operator*=(int right);
49 	Rational &operator/=(int right);
50 
51 	const Rational operator-() const;
52 
53 	const Rational operator+(const Rational &right) const;
54 	const Rational operator-(const Rational &right) const;
55 	const Rational operator*(const Rational &right) const;
56 	const Rational operator/(const Rational &right) const;
57 
58 	const Rational operator+(int right) const;
59 	const Rational operator-(int right) const;
60 	const Rational operator*(int right) const;
61 	const Rational operator/(int right) const;
62 
63 	bool operator==(const Rational &right) const;
64 	bool operator!=(const Rational &right) const;
65 	bool operator>(const Rational &right) const;
66 	bool operator<(const Rational &right) const;
67 	bool operator>=(const Rational &right) const;
68 	bool operator<=(const Rational &right) const;
69 
70 	bool operator==(int right) const;
71 	bool operator!=(int right) const;
72 	bool operator>(int right) const;
73 	bool operator<(int right) const;
74 	bool operator>=(int right) const;
75 	bool operator<=(int right) const;
76 
77 	void invert();
78 	Rational getInverse() const;
79 
80 	int toInt() const;
81 	double toDouble() const;
82 	frac_t toFrac() const;
83 
getNumerator()84 	int getNumerator() const { return _num; }
getDenominator()85 	int getDenominator() const { return _denom; }
86 
isOne()87 	bool isOne() const { return _num == _denom; }
88 
89 	void debugPrint(int debuglevel = 0, const char *caption = "Rational:") const;
90 
91 private:
92 	int _num;
93 	int _denom;
94 
95 	void cancel();
96 };
97 
98 const Rational operator+(int left, const Rational &right);
99 const Rational operator-(int left, const Rational &right);
100 const Rational operator*(int left, const Rational &right);
101 const Rational operator/(int left, const Rational &right);
102 
103 bool operator==(int left, const Rational &right);
104 bool operator!=(int left, const Rational &right);
105 bool operator>(int left, const Rational &right);
106 bool operator<(int left, const Rational &right);
107 bool operator>=(int left, const Rational &right);
108 bool operator<=(int left, const Rational &right);
109 
110 } // End of namespace Common
111 
112 #endif
113