mirror of
https://github.com/libretro/scummvm.git
synced 2024-12-12 12:09:15 +00:00
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
This commit is contained in:
parent
c7fa1074fb
commit
00cd966f3d
@ -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;
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
@ -35,4 +35,60 @@ public:
|
||||
TS_ASSERT(!(7 > r4));
|
||||
TS_ASSERT(!(7 >= r4));
|
||||
}
|
||||
|
||||
void test_assign() {
|
||||
Common::Rational r0(6, 3);
|
||||
Common::Rational r1(1, 2);
|
||||
|
||||
TS_ASSERT(r0 == 2);
|
||||
TS_ASSERT(r1 == Common::Rational(1, 2));
|
||||
|
||||
r0 = r1;
|
||||
TS_ASSERT(r0 == r1);
|
||||
TS_ASSERT(r0 == Common::Rational(1, 2));
|
||||
}
|
||||
|
||||
void test_negative() {
|
||||
Common::Rational r0(6, 3);
|
||||
Common::Rational r1(1, 2);
|
||||
|
||||
r0 = -r0;
|
||||
r1 = -r1;
|
||||
TS_ASSERT(r0 == -2);
|
||||
TS_ASSERT(r1 == Common::Rational(-1, 2));
|
||||
TS_ASSERT(r1 == Common::Rational(1, -2));
|
||||
}
|
||||
|
||||
void test_add_sub() {
|
||||
const Common::Rational r0(6, 3);
|
||||
const Common::Rational r1(1, 2);
|
||||
|
||||
TS_ASSERT(r0 + r1 == Common::Rational(5, 2));
|
||||
TS_ASSERT(r1 + r0 == Common::Rational(5, 2));
|
||||
TS_ASSERT(r0 - r1 == Common::Rational(3, 2));
|
||||
TS_ASSERT(r1 - r0 == Common::Rational(-3, 2));
|
||||
}
|
||||
|
||||
void test_mul() {
|
||||
const Common::Rational r0(6, 3);
|
||||
const Common::Rational r1(1, 2);
|
||||
|
||||
const Common::Rational r2(15, 14);
|
||||
const Common::Rational r3(7,3);
|
||||
const Common::Rational r4(5,2);
|
||||
|
||||
TS_ASSERT_EQUALS(r0 * r1, 1);
|
||||
|
||||
TS_ASSERT_EQUALS(r2 * r3, r4);
|
||||
TS_ASSERT_EQUALS((-r2) * r3, -r4);
|
||||
TS_ASSERT_EQUALS(r2 * (-r3), -r4);
|
||||
TS_ASSERT_EQUALS((-r2) * (-r3), r4);
|
||||
}
|
||||
|
||||
void test_div() {
|
||||
Common::Rational r0(6, 3);
|
||||
Common::Rational r1(1, 2);
|
||||
|
||||
TS_ASSERT(r0 / r1 == 4);
|
||||
}
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user