summaryrefslogtreecommitdiff
path: root/src/commonlib/include
diff options
context:
space:
mode:
Diffstat (limited to 'src/commonlib/include')
-rw-r--r--src/commonlib/include/commonlib/rational.h22
1 files changed, 22 insertions, 0 deletions
diff --git a/src/commonlib/include/commonlib/rational.h b/src/commonlib/include/commonlib/rational.h
new file mode 100644
index 0000000000..f172e0b191
--- /dev/null
+++ b/src/commonlib/include/commonlib/rational.h
@@ -0,0 +1,22 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#ifndef _COMMONLIB_RATIONAL_H_
+#define _COMMONLIB_RATIONAL_H_
+
+#include <stddef.h>
+
+/*
+ * Calculate the best rational approximation for a given fraction,
+ * with the restriction of maximum numerator and denominator.
+ * For example, to find the approximation of 3.1415 with 5 bit denominator
+ * and 8 bit numerator fields:
+ *
+ * rational_best_approximation(31415, 10000,
+ * (1 << 8) - 1, (1 << 5) - 1, &n, &d);
+ */
+void rational_best_approximation(
+ unsigned long numerator, unsigned long denominator,
+ unsigned long max_numerator, unsigned long max_denominator,
+ unsigned long *best_numerator, unsigned long *best_denominator);
+
+#endif /* _COMMONLIB_RATIONAL_H_ */