Skip to content

Commit f395de0

Browse files
authored
Merge pull request #897 from boostorg/871
Fix for issue #871 (#872)
2 parents 62dfa02 + bfb8e7d commit f395de0

File tree

1 file changed

+21
-10
lines changed

1 file changed

+21
-10
lines changed

include/boost/math/ccmath/abs.hpp

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <type_traits>
1313
#include <limits>
1414
#include <boost/math/tools/is_constant_evaluated.hpp>
15+
#include <boost/math/tools/assert.hpp>
1516
#include <boost/math/ccmath/isnan.hpp>
1617
#include <boost/math/ccmath/isinf.hpp>
1718

@@ -20,19 +21,29 @@ namespace boost::math::ccmath {
2021
namespace detail {
2122

2223
template <typename T>
23-
inline constexpr T abs_impl(T x) noexcept
24+
constexpr T abs_impl(T x) noexcept
2425
{
25-
return boost::math::ccmath::isnan(x) ? std::numeric_limits<T>::quiet_NaN() :
26-
boost::math::ccmath::isinf(x) ? std::numeric_limits<T>::infinity() :
27-
x == -0 ? T(0) :
28-
x == (std::numeric_limits<T>::min)() ? std::numeric_limits<T>::quiet_NaN() :
29-
x > 0 ? x : -x;
26+
if (boost::math::ccmath::isnan(x))
27+
{
28+
return std::numeric_limits<T>::quiet_NaN();
29+
}
30+
else if (x == static_cast<T>(-0))
31+
{
32+
return static_cast<T>(0);
33+
}
34+
35+
if constexpr (std::is_integral_v<T>)
36+
{
37+
BOOST_MATH_ASSERT(x != (std::numeric_limits<T>::min)());
38+
}
39+
40+
return x >= 0 ? x : -x;
3041
}
3142

3243
} // Namespace detail
3344

3445
template <typename T, std::enable_if_t<!std::is_unsigned_v<T>, bool> = true>
35-
inline constexpr T abs(T x) noexcept
46+
constexpr T abs(T x) noexcept
3647
{
3748
if(BOOST_MATH_IS_CONSTANT_EVALUATED(x))
3849
{
@@ -48,7 +59,7 @@ inline constexpr T abs(T x) noexcept
4859
// If abs() is called with an argument of type X for which is_unsigned_v<X> is true and if X
4960
// cannot be converted to int by integral promotion (7.3.7), the program is ill-formed.
5061
template <typename T, std::enable_if_t<std::is_unsigned_v<T>, bool> = true>
51-
inline constexpr T abs(T x) noexcept
62+
constexpr T abs(T x) noexcept
5263
{
5364
if constexpr (std::is_convertible_v<T, int>)
5465
{
@@ -61,12 +72,12 @@ inline constexpr T abs(T x) noexcept
6172
}
6273
}
6374

64-
inline constexpr long int labs(long int j) noexcept
75+
constexpr long int labs(long int j) noexcept
6576
{
6677
return boost::math::ccmath::abs(j);
6778
}
6879

69-
inline constexpr long long int llabs(long long int j) noexcept
80+
constexpr long long int llabs(long long int j) noexcept
7081
{
7182
return boost::math::ccmath::abs(j);
7283
}

0 commit comments

Comments
 (0)