From cfec8308a39ae4a2a1a1b1fe0df0a099470d9c78 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Thu, 17 Oct 2024 02:58:16 +0100 Subject: Simple constexpr pow function --- lib/maths.h | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'lib/maths.h') diff --git a/lib/maths.h b/lib/maths.h index 32f2884..97b7ef5 100644 --- a/lib/maths.h +++ b/lib/maths.h @@ -261,6 +261,19 @@ midpoint(const std::pair & v) return std::midpoint(v.first, v.second); } +// std::pow is not constexpr +template + requires requires(T n) { n *= n; } +constexpr inline T +pow(const T base, std::integral auto exp) +{ + T res {1}; + while (exp--) { + res *= base; + } + return res; +} + // Conversions template inline constexpr auto -- cgit v1.2.3