[Scummvm-cvs-logs] SF.net SVN: scummvm:[49066] scummvm/trunk/common

fingolfin at users.sourceforge.net fingolfin at users.sourceforge.net
Tue May 18 00:08:40 CEST 2010


Revision: 49066
          http://scummvm.svn.sourceforge.net/scummvm/?rev=49066&view=rev
Author:   fingolfin
Date:     2010-05-17 22:08:40 +0000 (Mon, 17 May 2010)

Log Message:
-----------
Get rid of Common::Rational::normalize and other redundant code

Modified Paths:
--------------
    scummvm/trunk/common/rational.cpp
    scummvm/trunk/common/rational.h

Modified: scummvm/trunk/common/rational.cpp
===================================================================
--- scummvm/trunk/common/rational.cpp	2010-05-17 22:08:19 UTC (rev 49065)
+++ scummvm/trunk/common/rational.cpp	2010-05-17 22:08:40 UTC (rev 49066)
@@ -41,10 +41,15 @@
 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 @@
 	_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 @@
 	_num   = _num * right._denom + right._num * _denom;
 	_denom = _denom * right._denom;
 
-	normalize();
+	cancel();
 
 	return *this;
 }
@@ -100,26 +89,25 @@
 	_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 @@
 
 	SWAP(_num, _denom);
 
-	normalize();
+	if (_denom < 0) {
+		_denom = -_denom;
+		_num = -_num;
+	}
 }
 
 Rational Rational::getInverse() const {
@@ -255,14 +246,10 @@
 }
 
 int Rational::toInt() const {
-	assert(_denom != 0);
-
 	return _num / _denom;
 }
 
 double Rational::toDouble() const {
-	assert(_denom != 0);
-
 	return ((double) _num) / ((double) _denom);
 }
 

Modified: scummvm/trunk/common/rational.h
===================================================================
--- scummvm/trunk/common/rational.h	2010-05-17 22:08:19 UTC (rev 49065)
+++ scummvm/trunk/common/rational.h	2010-05-17 22:08:40 UTC (rev 49066)
@@ -91,7 +91,6 @@
 	int _denom;
 
 	void cancel();
-	void normalize();
 };
 
 const Rational operator+(int left, const Rational &right);


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.




More information about the Scummvm-git-logs mailing list