aboutsummaryrefslogtreecommitdiff
path: root/common
diff options
context:
space:
mode:
authorMax Horn2010-05-17 22:07:58 +0000
committerMax Horn2010-05-17 22:07:58 +0000
commit00cd966f3da11aea4cd1bc5a28808e006f7eff9d (patch)
tree5599b8090d6111b3b4ebcd784e739fd73c12a27a /common
parentc7fa1074fbc55e9e519f9c7e08dea9603af22e61 (diff)
downloadscummvm-rg350-00cd966f3da11aea4cd1bc5a28808e006f7eff9d.tar.gz
scummvm-rg350-00cd966f3da11aea4cd1bc5a28808e006f7eff9d.tar.bz2
scummvm-rg350-00cd966f3da11aea4cd1bc5a28808e006f7eff9d.zip
Some tweaks and fixes for Common::Rational
* Fix Common::gcd to work with negative input * This fixes a bug in Common::Rational's multiplication code * Add some more basic unit tests (including one which checks for the now fixed multiplication bug) * cleanup svn-id: r49064
Diffstat (limited to 'common')
-rw-r--r--common/algorithm.h2
-rw-r--r--common/rational.cpp26
2 files changed, 3 insertions, 25 deletions
diff --git a/common/algorithm.h b/common/algorithm.h
index 06f2a279af..d3f518b225 100644
--- a/common/algorithm.h
+++ b/common/algorithm.h
@@ -227,6 +227,8 @@ void sort(T first, T last) {
*/
template<class T>
T gcd(T a, T b) {
+ if (a <= 0) a = -a;
+ if (b <= 0) b = -b;
while (a > 0) {
T tmp = a;
a = b % a;
diff --git a/common/rational.cpp b/common/rational.cpp
index e27e880a04..91d1c5a848 100644
--- a/common/rational.cpp
+++ b/common/rational.cpp
@@ -49,7 +49,7 @@ Rational::Rational(int num, int denom) {
void Rational::cancel() {
// Cancel the fraction by dividing both the num and the denom
- // by their greatest common denom.
+ // by their greatest common divisor.
int gcd = Common::gcd(_num, _denom);
@@ -144,65 +144,49 @@ const Rational Rational::operator-() const {
const Rational Rational::operator+(const Rational &right) const {
Rational tmp = *this;
-
tmp += right;
-
return tmp;
}
const Rational Rational::operator-(const Rational &right) const {
Rational tmp = *this;
-
tmp -= right;
-
return tmp;
}
const Rational Rational::operator*(const Rational &right) const {
Rational tmp = *this;
-
tmp *= right;
-
return tmp;
}
const Rational Rational::operator/(const Rational &right) const {
Rational tmp = *this;
-
tmp /= right;
-
return tmp;
}
const Rational Rational::operator+(int right) const {
Rational tmp = *this;
-
tmp += right;
-
return tmp;
}
const Rational Rational::operator-(int right) const {
Rational tmp = *this;
-
tmp -= right;
-
return tmp;
}
const Rational Rational::operator*(int right) const {
Rational tmp = *this;
-
tmp *= right;
-
return tmp;
}
const Rational Rational::operator/(int right) const {
Rational tmp = *this;
-
tmp /= right;
-
return tmp;
}
@@ -296,33 +280,25 @@ Rational::operator double() const {
const Rational operator+(int left, const Rational &right) {
Rational tmp = right;
-
tmp += left;
-
return tmp;
}
const Rational operator-(int left, const Rational &right) {
Rational tmp = right;
-
tmp -= left;
-
return tmp;
}
const Rational operator*(int left, const Rational &right) {
Rational tmp = right;
-
tmp *= left;
-
return tmp;
}
const Rational operator/(int left, const Rational &right) {
Rational tmp = right;
-
tmp /= left;
-
return tmp;
}