xref: /aosp_15_r20/external/coreboot/src/commonlib/include/commonlib/rational.h (revision b9411a12aaaa7e1e6a6fb7c5e057f44ee179a49c)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 
3 #ifndef _COMMONLIB_RATIONAL_H_
4 #define _COMMONLIB_RATIONAL_H_
5 
6 #include <stddef.h>
7 
8 /*
9  * Calculate the best rational approximation for a given fraction,
10  * with the restriction of maximum numerator and denominator.
11  * For example, to find the approximation of 3.1415 with 5 bit denominator
12  * and 8 bit numerator fields:
13  *
14  * rational_best_approximation(31415, 10000,
15  *			       (1 << 8) - 1, (1 << 5) - 1, &n, &d);
16  */
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);
21 
22 #endif /* _COMMONLIB_RATIONAL_H_ */
23