ScummVM API documentation
rational.h
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 modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (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, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #ifndef COMMON_RATIONAL_H
23 #define COMMON_RATIONAL_H
24 
25 #include "common/scummsys.h"
26 #include "common/frac.h"
27 
28 namespace Common {
29 
40 class Rational {
41 public:
42  Rational();
43  Rational(int num);
44  Rational(int num, int denom);
45  Rational(const Rational &rational);
46 
47  Rational &operator=(const Rational &right);
48  Rational &operator=(int right);
49 
50  Rational &operator+=(const Rational &right);
51  Rational &operator-=(const Rational &right);
52  Rational &operator*=(const Rational &right);
53  Rational &operator/=(const Rational &right);
54 
55  Rational &operator+=(int right);
56  Rational &operator-=(int right);
57  Rational &operator*=(int right);
58  Rational &operator/=(int right);
59 
60  const Rational operator-() const;
61 
62  const Rational operator+(const Rational &right) const;
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 
67  const Rational operator+(int right) const;
68  const Rational operator-(int right) const;
69  const Rational operator*(int right) const;
70  const Rational operator/(int right) const;
71 
72  bool operator==(const Rational &right) const;
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 
79  bool operator==(int right) const;
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 
86  void invert();
87  Rational getInverse() const;
88 
89  int toInt() const;
90  double toDouble() const;
91  frac_t toFrac() const;
92 
93  int getNumerator() const { return _num; }
94  int getDenominator() const { return _denom; }
95 
96  bool isOne() const { return _num == _denom; }
97 
98  void debugPrint(int debuglevel = 0, const char *caption = "Rational:") const;
99 
100 private:
101  int _num;
102  int _denom;
103 
104  void cancel();
105 };
106 
107 const Rational operator+(int left, const Rational &right);
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 
112 bool operator==(int left, const Rational &right);
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 
121 } // End of namespace Common
122 
123 #endif
Definition: rational.h:40
Definition: algorithm.h:29
int32 frac_t
Definition: frac.h:52