From 76844e33e7c40fdc37c393e9740a9b67c415ec0f Mon Sep 17 00:00:00 2001 From: docentYT <63965954+docentYT@users.noreply.github.com> Date: Sat, 2 May 2026 00:07:06 +0200 Subject: [PATCH] Refactor clamp_circular --- utilities/utilities.h | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/utilities/utilities.h b/utilities/utilities.h index 353d722b..d0646f96 100644 --- a/utilities/utilities.h +++ b/utilities/utilities.h @@ -252,13 +252,19 @@ template bool is_equal(T const &Left, T const &Right, T const Epsil } // keeps the provided value in specified range 0-Range, as if the range was circular buffer -template Type_ clamp_circular(Type_ Value, Type_ const Range = static_cast(360)) +template T clamp_circular(T Value, T const Range = T(360)) { + if constexpr (std::is_floating_point_v) + { + Value = std::fmod(Value, Range); + } + else + { + Value %= Range; + } - Value -= Range * (int)(Value / Range); // clamp the range to 0-360 - if (Value < Type_(0)) + if (Value < T(0)) Value += Range; - return Value; }