diff options
author | Max Horn | 2010-05-17 22:08:40 +0000 |
---|---|---|
committer | Max Horn | 2010-05-17 22:08:40 +0000 |
commit | 22caf0e1cf95634cb5cabc4ea52fb3ccea15679d (patch) | |
tree | e84e981305ae72d14bf9cbec2095cb8280a30d37 | |
parent | c56a481f88c23ef394c723cd4ef4c7efec6a7aa9 (diff) | |
download | scummvm-rg350-22caf0e1cf95634cb5cabc4ea52fb3ccea15679d.tar.gz scummvm-rg350-22caf0e1cf95634cb5cabc4ea52fb3ccea15679d.tar.bz2 scummvm-rg350-22caf0e1cf95634cb5cabc4ea52fb3ccea15679d.zip |
Get rid of Common::Rational::normalize and other redundant code
svn-id: r49066
-rw-r--r-- | common/rational.cpp | 47 | ||||
-rw-r--r-- | common/rational.h | 1 |
2 files changed, 17 insertions, 31 deletions
diff --git a/common/rational.cpp b/common/rational.cpp index 91d1c5a848..292c2ac940 100644 --- a/common/rational.cpp +++ b/common/rational.cpp @@ -41,10 +41,15 @@ Rational::Rational(int num) { Rational::Rational(int num, int denom) { assert(denom != 0); - _num = num; - _denom = denom; + if (denom > 0) { + _num = num; + _denom = denom; + } else { + _num = -num; + _denom = -denom; + } - normalize(); + cancel(); } void Rational::cancel() { @@ -57,22 +62,6 @@ void Rational::cancel() { _denom /= gcd; } -void Rational::normalize() { - // Is the fraction negative? - bool negative = !((!(_num < 0)) == (!(_denom < 0))); - - // Make both integers positive - _num = ABS(_num); - _denom = ABS(_denom); - - // Cancel the fraction - cancel(); - - // If the fraction is supposed to be negative, make the num negative - if (negative) - _num = -_num; -} - Rational &Rational::operator=(const Rational &right) { _num = right._num; _denom = right._denom; @@ -91,7 +80,7 @@ Rational &Rational::operator+=(const Rational &right) { _num = _num * right._denom + right._num * _denom; _denom = _denom * right._denom; - normalize(); + cancel(); return *this; } @@ -100,26 +89,25 @@ Rational &Rational::operator-=(const Rational &right) { _num = _num * right._denom - right._num * _denom; _denom = _denom * right._denom; - normalize(); + cancel(); return *this; } Rational &Rational::operator*=(const Rational &right) { - // Try to cross-cancel first, to avoid unnecessary overflow + // Cross-cancel to avoid unnecessary overflow; + // the result then is automatically normalized int gcd1 = Common::gcd(_num, right._denom); int gcd2 = Common::gcd(right._num, _denom); _num = (_num / gcd1) * (right._num / gcd2); _denom = (_denom / gcd2) * (right._denom / gcd1); - normalize(); - return *this; } Rational &Rational::operator/=(const Rational &right) { - return *this *= Rational(right._denom, right._num); + return *this *= right.getInverse(); } Rational &Rational::operator+=(int right) { @@ -243,7 +231,10 @@ void Rational::invert() { SWAP(_num, _denom); - normalize(); + if (_denom < 0) { + _denom = -_denom; + _num = -_num; + } } Rational Rational::getInverse() const { @@ -255,14 +246,10 @@ Rational Rational::getInverse() const { } int Rational::toInt() const { - assert(_denom != 0); - return _num / _denom; } double Rational::toDouble() const { - assert(_denom != 0); - return ((double) _num) / ((double) _denom); } diff --git a/common/rational.h b/common/rational.h index 1ad33de8a6..5ceac36209 100644 --- a/common/rational.h +++ b/common/rational.h @@ -91,7 +91,6 @@ private: int _denom; void cancel(); - void normalize(); }; const Rational operator+(int left, const Rational &right); |