* otherwise. If the resulting nominator/denominator are relatively prime,
* this may not be possible.
*
+ * It is save to use the same variable for result and input values
+ *
* @retval SR_OK Success.
* @retval SR_ERR_ARG Resulting value to large
*
#endif
}
+/**
+ * Divide rational a by rational b
+ *
+ * @param[in] num numerator
+ * @param[in] div divisor
+ * @param[out] res Result
+ *
+ * The resulting nominator/denominator are reduced if the result would not fit
+ * otherwise. If the resulting nominator/denominator are relatively prime,
+ * this may not be possible.
+ *
+ * It is save to use the same variable for result and input values
+ *
+ * @retval SR_OK Success.
+ * @retval SR_ERR_ARG Division by zero
+ * @retval SR_ERR_ARG Denominator of divisor to large
+ * @retval SR_ERR_ARG Resulting value to large
+ *
+ * @since 0.5.0
+ */
+SR_API int sr_rational_div(struct sr_rational *res, const struct sr_rational *num,
+ const struct sr_rational *div)
+{
+ struct sr_rational t;
+
+ if (div->q > INT64_MAX)
+ return SR_ERR_ARG;
+ if (div->p == 0)
+ return SR_ERR_ARG;
+
+ if (div->p > 0) {
+ t.p = div->q;
+ t.q = div->p;
+ } else {
+ t.p = -div->q;
+ t.q = -div->p;
+ }
+
+ return sr_rational_mult(res, num, &t);
+}
+
/** @} */
}
END_TEST
+START_TEST(test_div_rational)
+{
+ const struct sr_rational r[][3] = {
+ /* a * b = c */
+ { { 1, 1 }, { 1, 1 }, { 1, 1 }},
+ { { 2, 1 }, { 1, 3 }, { 6, 1 }},
+ { { 1, 2 }, { 1, 2 }, { 1, 1 }},
+ /* Test negative numbers */
+ { { -1, 2 }, { 1, 2 }, { -1, 1 }},
+ { { -1, 2 }, { -1, 2 }, { 1, 1 }},
+ { { -(1ll<<20), (1ll<<10) }, { -1, (1ll<<20) }, { (1ll<<30), 1 }},
+ /* Test reduction */
+ { { INT32_MAX, (1ll<<12) }, { 1, (1<<2) }, { INT32_MAX, (1ll<<10) }},
+ { { INT64_MAX, (1ll<<63) }, { 1, (1<<3) }, { INT64_MAX, (1ll<<60) }},
+ /* Test large numbers */
+ { { (1ll<<40), (1ll<<10) }, { 1, (1ll<<30) }, { (1ll<<60), 1 }},
+ { { -(1ll<<40), (1ll<<10) }, { -1, (1ll<<30) }, { (1ll<<60), 1 }},
+
+ { { 10000*3, 4 }, { 1, 80000*3 }, { 200000000*9, 1 }},
+ { { 4, 10000*3 }, { 80000*3, 1 }, { 1, 200000000*9 }},
+
+ { { -10000*3, 4 }, { 1, 80000*3 }, { -200000000*9, 1 }},
+ { { 10000*3, 4 }, { -1, 80000*3 }, { -200000000*9, 1 }},
+ };
+
+ for (unsigned i = 0; i < ARRAY_SIZE(r); i++) {
+ struct sr_rational res;
+
+ int rc = sr_rational_div(&res, &r[i][0], &r[i][1]);
+ fail_unless(rc == SR_OK);
+ fail_unless(sr_rational_eq(&res, &r[i][2]) == 1,
+ "sr_rational_mult() failed: [%d] %ld/%lu != %ld/%lu.",
+ i, res.p, res.q, r[i][2].p, r[i][2].q);
+ }
+
+ {
+ struct sr_rational res;
+ int rc = sr_rational_div(&res, &r[0][0], &((struct sr_rational){ 0, 5 }));
+
+ fail_unless(rc == SR_ERR_ARG);
+ }
+}
+END_TEST
+
Suite *suite_analog(void)
{
Suite *s;
tcase_add_test(tc, test_set_rational_null);
tcase_add_test(tc, test_cmp_rational);
tcase_add_test(tc, test_mult_rational);
+ tcase_add_test(tc, test_div_rational);
suite_add_tcase(s, tc);
return s;