From 03ac231b833443a660f90fef0bfe496f864a5770 Mon Sep 17 00:00:00 2001 From: Matt Borland Date: Sun, 13 Nov 2022 07:25:12 -0800 Subject: [PATCH] Fix for issue #871 (#872) --- include/boost/math/ccmath/abs.hpp | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/include/boost/math/ccmath/abs.hpp b/include/boost/math/ccmath/abs.hpp index 8ae745f7e0..fa798eba12 100644 --- a/include/boost/math/ccmath/abs.hpp +++ b/include/boost/math/ccmath/abs.hpp @@ -12,6 +12,7 @@ #include #include #include +#include #include #include @@ -28,19 +29,29 @@ namespace boost::math::ccmath { namespace detail { template -inline constexpr T abs_impl(T x) noexcept +constexpr T abs_impl(T x) noexcept { - return boost::math::ccmath::isnan(x) ? std::numeric_limits::quiet_NaN() : - boost::math::ccmath::isinf(x) ? std::numeric_limits::infinity() : - x == -0 ? T(0) : - x == (std::numeric_limits::min)() ? std::numeric_limits::quiet_NaN() : - x > 0 ? x : -x; + if (boost::math::ccmath::isnan(x)) + { + return std::numeric_limits::quiet_NaN(); + } + else if (x == static_cast(-0)) + { + return static_cast(0); + } + + if constexpr (std::is_integral_v) + { + BOOST_MATH_ASSERT(x != (std::numeric_limits::min)()); + } + + return x >= 0 ? x : -x; } } // Namespace detail template , bool> = true> -inline constexpr T abs(T x) noexcept +constexpr T abs(T x) noexcept { if(BOOST_MATH_IS_CONSTANT_EVALUATED(x)) { @@ -56,7 +67,7 @@ inline constexpr T abs(T x) noexcept // If abs() is called with an argument of type X for which is_unsigned_v is true and if X // cannot be converted to int by integral promotion (7.3.7), the program is ill-formed. template , bool> = true> -inline constexpr T abs(T x) noexcept +constexpr T abs(T x) noexcept { if constexpr (std::is_convertible_v) { @@ -69,12 +80,12 @@ inline constexpr T abs(T x) noexcept } } -inline constexpr long int labs(long int j) noexcept +constexpr long int labs(long int j) noexcept { return boost::math::ccmath::abs(j); } -inline constexpr long long int llabs(long long int j) noexcept +constexpr long long int llabs(long long int j) noexcept { return boost::math::ccmath::abs(j); }