From 3d4013fc26580f5007d11d6f3408bd4f0bbad63a Mon Sep 17 00:00:00 2001 From: lighting9999 Date: Sat, 29 Nov 2025 16:06:09 +0800 Subject: [PATCH] Refactor safe_max to improve stability and remove overload Refactor safe_max function to prevent division by zero and overflow. Removed overloaded long double version. --- include/boost/math/complex/details.hpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/include/boost/math/complex/details.hpp b/include/boost/math/complex/details.hpp index 0a3d35347d..8e7c99272a 100644 --- a/include/boost/math/complex/details.hpp +++ b/include/boost/math/complex/details.hpp @@ -14,6 +14,7 @@ #include #include #include +#include #include #include #include @@ -41,13 +42,12 @@ inline std::complex mult_minus_i(const std::complex& t) template inline T safe_max(T t) { - return std::sqrt((std::numeric_limits::max)()) / t; -} -inline long double safe_max(long double t) -{ - // long double sqrt often returns infinity due to - // insufficient internal precision: - return std::sqrt((std::numeric_limits::max)()) / t; + static const T max_val = (std::numeric_limits::max)(); + static const T min_val = (std::numeric_limits::min)(); + + // Prevent division by zero and overflow + T denominator = (std::max)(std::abs(t), min_val); + return std::sqrt(max_val) / denominator; } template