also implement subtraction.

This commit is contained in:
Enno Rehling 2017-02-24 17:55:06 +01:00
parent 234eb62226
commit e0229be500
3 changed files with 11 additions and 0 deletions

View File

@ -49,6 +49,12 @@ variant frac_add(variant a, variant b)
return frac_make(num, den); 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) variant frac_mul(variant a, variant b)
{ {
return frac_make(a.sa[0] * b.sa[0], a.sa[1] * b.sa[1]); return frac_make(a.sa[0] * b.sa[0], a.sa[1] * b.sa[1]);

View File

@ -23,6 +23,7 @@ extern "C" {
variant frac_make(int num, int den); variant frac_make(int num, int den);
variant frac_add(variant a, variant b); variant frac_add(variant a, variant b);
variant frac_sub(variant a, variant b);
variant frac_mul(variant a, variant b); variant frac_mul(variant a, variant b);
variant frac_div(variant a, variant b); variant frac_div(variant a, variant b);

View File

@ -18,6 +18,10 @@ static void test_fractions(CuTest *tc) {
a = frac_div(b, b); a = frac_div(b, b);
CuAssertIntEquals(tc, 1, a.sa[0]); CuAssertIntEquals(tc, 1, a.sa[0]);
CuAssertIntEquals(tc, 1, a.sa[1]); 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) CuSuite *get_variant_suite(void)