From 133bc50d530eca9281c4701c8e379fa9924ac1af Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Sat, 14 Dec 2024 08:17:20 +0100 Subject: [PATCH] core: make interpolate() a template Make the type the interpolate() function works on a template type, so that this can be used for arbitrary unit types without warnings. Internally, it does its math using floating point arithmetics anyway, so no risk of overflow. (Exception: when just taking the middle point, but I would hope that no part of the code lives on the edge such that addition of two values gives an overflow.) Moreover, add an "math.h" include so that the header becomes independent of others. Signed-off-by: Berthold Stoeger --- core/interpolate.h | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/core/interpolate.h b/core/interpolate.h index acb213acd..924c35574 100644 --- a/core/interpolate.h +++ b/core/interpolate.h @@ -2,8 +2,16 @@ #ifndef INTERPOLATE_H #define INTERPOLATE_H -/* Linear interpolation between 'a' and 'b', when we are 'part'way into the 'whole' distance from a to b */ -static inline int interpolate(int a, int b, int part, int whole) +#include +#include // lrint() + +/* Linear interpolation between 'a' and 'b', when we are 'part'way into the 'whole' distance from a to b + * Since we're currently stuck with C++17, we have this horrible way of testing whether the template + * parameter is an integral type. + */ +template ::value, bool> = true> +T interpolate(T a, T b, int part, int whole) { /* It is doubtful that we actually need floating point for this, but whatever */ if (whole) {