X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;f=src%2Fanalog.c;h=7343b481e610d409d51bb45dd5f2be1a31138c77;hb=15a5bfe4815f9991a9bb532c05d6244a1818a0e4;hp=7c0dde1be5e9265c713371c78819b09f1189197b;hpb=ee1b6054d6d6a00698ddae421eae63019c452b6d;p=libsigrok.git diff --git a/src/analog.c b/src/analog.c index 7c0dde1b..7343b481 100644 --- a/src/analog.c +++ b/src/analog.c @@ -444,6 +444,8 @@ SR_API int sr_rational_eq(const struct sr_rational *a, const struct sr_rational * 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 * @@ -506,4 +508,45 @@ SR_API int sr_rational_mult(struct sr_rational *res, const struct sr_rational *a #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); +} + /** @} */