From 97cea77933bd02c5e4f390f37116525c3c298bb8 Mon Sep 17 00:00:00 2001 From: Aleksandr Kovalko Date: Sat, 9 May 2026 15:03:50 +0200 Subject: [PATCH] Optimized math hot-path (deg2rad, rad2deg, arc_hav) --- include/geo/detail/math.hpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/include/geo/detail/math.hpp b/include/geo/detail/math.hpp index d91ba8a..c96962c 100644 --- a/include/geo/detail/math.hpp +++ b/include/geo/detail/math.hpp @@ -25,12 +25,15 @@ inline constexpr double kEarthRadius = 6371009.0; // not portable to MSVC without _USE_MATH_DEFINES) or std::numbers::pi (C++20). inline constexpr double kPi = 3.14159265358979323846; +inline constexpr double kDegToRad = kPi / 180.0; +inline constexpr double kRadToDeg = 180.0 / kPi; + [[nodiscard]] inline double deg2rad(double degrees) noexcept { - return degrees * kPi / 180.0; + return degrees * kDegToRad; } [[nodiscard]] inline double rad2deg(double angle) noexcept { - return angle * 180.0 / kPi; + return angle * kRadToDeg; } // Returns the non-negative remainder of x / m. @@ -62,7 +65,7 @@ inline constexpr double kPi = 3.14159265358979323846; // Inverse haversine. arc_hav(x) == 2 * asin(sqrt(x)). Argument must be in [0, 1]. [[nodiscard]] inline double arc_hav(double x) noexcept { - return 2.0 * std::asin(std::sqrt(std::clamp(x, 0.0, 1.0))); + return 2.0 * std::asin(std::min(1.0, std::sqrt(std::max(0.0, x)))); } // Given h == hav(x), returns sin(abs(x)).