diff --git a/src/util/variant.c b/src/util/variant.c index 74d7adb1c..2ffe4a4e3 100644 --- a/src/util/variant.c +++ b/src/util/variant.c @@ -49,6 +49,12 @@ variant frac_add(variant a, variant b) return frac_make(num, den); } +variant frac_sub(variant a, variant b) +{ + b.sa[0] = -b.sa[0]; + return frac_add(a, b); +} + variant frac_mul(variant a, variant b) { return frac_make(a.sa[0] * b.sa[0], a.sa[1] * b.sa[1]); diff --git a/src/util/variant.h b/src/util/variant.h index f98fa0b80..4bd5d952d 100644 --- a/src/util/variant.h +++ b/src/util/variant.h @@ -23,6 +23,7 @@ extern "C" { variant frac_make(int num, int den); variant frac_add(variant a, variant b); + variant frac_sub(variant a, variant b); variant frac_mul(variant a, variant b); variant frac_div(variant a, variant b); diff --git a/src/util/variant.test.c b/src/util/variant.test.c index 6344e5c33..793e4f683 100644 --- a/src/util/variant.test.c +++ b/src/util/variant.test.c @@ -18,6 +18,10 @@ static void test_fractions(CuTest *tc) { a = frac_div(b, b); CuAssertIntEquals(tc, 1, a.sa[0]); CuAssertIntEquals(tc, 1, a.sa[1]); + a = frac_sub(a, a); + CuAssertIntEquals(tc, 0, a.sa[0]); + a = frac_mul(a, b); + CuAssertIntEquals(tc, 0, a.sa[0]); } CuSuite *get_variant_suite(void)