Skip to content

Commit 03ac231

Browse files
authored
Fix for issue #871 (#872)
1 parent 1923ba5 commit 03ac231

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

@@ -28,19 +29,29 @@ namespace boost::math::ccmath {
2829
namespace detail {
2930

3031
template <typename T>
31-
inline constexpr T abs_impl(T x) noexcept
32+
constexpr T abs_impl(T x) noexcept
3233
{
33-
return boost::math::ccmath::isnan(x) ? std::numeric_limits<T>::quiet_NaN() :
34-
boost::math::ccmath::isinf(x) ? std::numeric_limits<T>::infinity() :
35-
x == -0 ? T(0) :
36-
x == (std::numeric_limits<T>::min)() ? std::numeric_limits<T>::quiet_NaN() :
37-
x > 0 ? x : -x;
34+
if (boost::math::ccmath::isnan(x))
35+
{
36+
return std::numeric_limits<T>::quiet_NaN();
37+
}
38+
else if (x == static_cast<T>(-0))
39+
{
40+
return static_cast<T>(0);
41+
}
42+
43+
if constexpr (std::is_integral_v<T>)
44+
{
45+
BOOST_MATH_ASSERT(x != (std::numeric_limits<T>::min)());
46+
}
47+
48+
return x >= 0 ? x : -x;
3849
}
3950

4051
} // Namespace detail
4152

4253
template <typename T, std::enable_if_t<!std::is_unsigned_v<T>, bool> = true>
43-
inline constexpr T abs(T x) noexcept
54+
constexpr T abs(T x) noexcept
4455
{
4556
if(BOOST_MATH_IS_CONSTANT_EVALUATED(x))
4657
{
@@ -56,7 +67,7 @@ inline constexpr T abs(T x) noexcept
5667
// If abs() is called with an argument of type X for which is_unsigned_v<X> is true and if X
5768
// cannot be converted to int by integral promotion (7.3.7), the program is ill-formed.
5869
template <typename T, std::enable_if_t<std::is_unsigned_v<T>, bool> = true>
59-
inline constexpr T abs(T x) noexcept
70+
constexpr T abs(T x) noexcept
6071
{
6172
if constexpr (std::is_convertible_v<T, int>)
6273
{
@@ -69,12 +80,12 @@ inline constexpr T abs(T x) noexcept
6980
}
7081
}
7182

72-
inline constexpr long int labs(long int j) noexcept
83+
constexpr long int labs(long int j) noexcept
7384
{
7485
return boost::math::ccmath::abs(j);
7586
}
7687

77-
inline constexpr long long int llabs(long long int j) noexcept
88+
constexpr long long int llabs(long long int j) noexcept
7889
{
7990
return boost::math::ccmath::abs(j);
8091
}

0 commit comments

Comments
 (0)