1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * Helper functions for rational numbers.
5 * Copyright (C) 2009 emlix GmbH, Oskar Schirmer <oskar@scara.com>
6 * Copyright (C) 2019 Trent Piepho <tpiepho@gmail.com>
9 #include <commonlib/helpers.h>
10 #include <commonlib/rational.h>
14 * For theoretical background, see:
15 * https://en.wikipedia.org/wiki/Continued_fraction
17 void rational_best_approximation(
18 unsigned long numerator
, unsigned long denominator
,
19 unsigned long max_numerator
, unsigned long max_denominator
,
20 unsigned long *best_numerator
, unsigned long *best_denominator
)
23 * n/d is the starting rational, where both n and d will
24 * decrease in each iteration using the Euclidean algorithm.
26 * dp is the value of d from the prior iteration.
28 * n2/d2, n1/d1, and n0/d0 are our successively more accurate
29 * approximations of the rational. They are, respectively,
30 * the current, previous, and two prior iterations of it.
32 * a is current term of the continued fraction.
34 unsigned long n
, d
, n0
, d0
, n1
, d1
, n2
, d2
;
46 * Find next term in continued fraction, 'a', via
47 * Euclidean algorithm.
55 * Calculate the current rational approximation (aka
56 * convergent), n2/d2, using the term just found and
57 * the two prior approximations.
63 * If the current convergent exceeds the maximum, then
64 * return either the previous convergent or the
65 * largest semi-convergent, the final term of which is
68 if ((n2
> max_numerator
) || (d2
> max_denominator
)) {
69 unsigned long t
= ULONG_MAX
;
72 t
= (max_denominator
- d0
) / d1
;
74 t
= MIN(t
, (max_numerator
- n0
) / n1
);
77 * This tests if the semi-convergent is closer than the previous
78 * convergent. If d1 is zero there is no previous convergent as
79 * this is the 1st iteration, so always choose the semi-convergent.
81 if (!d1
|| 2u * t
> a
|| (2u * t
== a
&& d0
* dp
> d1
* d
)) {
94 *best_denominator
= d1
;