forked from github/server
avoid division by zero for uninitialized btype->magres fractions
This commit is contained in:
parent
44ce77e0c4
commit
4e9bc53288
2 changed files with 18 additions and 0 deletions
|
@ -59,6 +59,12 @@ variant frac_make(int num, int den)
|
|||
variant frac_add(variant a, variant b)
|
||||
{
|
||||
int num, den;
|
||||
if (a.sa[0] == 0) {
|
||||
return b;
|
||||
}
|
||||
if (b.sa[0] == 0) {
|
||||
return a;
|
||||
}
|
||||
den = lcm(a.sa[1], b.sa[1]);
|
||||
num = a.sa[0] * den / a.sa[1] + b.sa[0] * den / b.sa[1];
|
||||
return frac_make(num, den);
|
||||
|
|
|
@ -40,6 +40,18 @@ static void test_fractions(CuTest *tc) {
|
|||
a = frac_make(480001, 3000);
|
||||
CuAssertIntEquals(tc, 32000, a.sa[0]);
|
||||
CuAssertIntEquals(tc, 200, a.sa[1]);
|
||||
|
||||
a = frac_make(0, 0);
|
||||
b = frac_make(4, 1);
|
||||
a = frac_add(b, a);
|
||||
CuAssertIntEquals(tc, 4, a.sa[0]);
|
||||
CuAssertIntEquals(tc, 1, a.sa[1]);
|
||||
|
||||
a = frac_make(0, 0);
|
||||
b = frac_make(4, 1);
|
||||
a = frac_add(a, b);
|
||||
CuAssertIntEquals(tc, 4, a.sa[0]);
|
||||
CuAssertIntEquals(tc, 1, a.sa[1]);
|
||||
}
|
||||
|
||||
CuSuite *get_variant_suite(void)
|
||||
|
|
Loading…
Reference in a new issue