Skip to content

Commit

Permalink
Merge pull request #897 from boostorg/871
Browse files Browse the repository at this point in the history
Fix for issue #871 (#872)
  • Loading branch information
mborland committed Dec 8, 2022
2 parents 62dfa02 + bfb8e7d commit f395de0
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions include/boost/math/ccmath/abs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <type_traits>
#include <limits>
#include <boost/math/tools/is_constant_evaluated.hpp>
#include <boost/math/tools/assert.hpp>
#include <boost/math/ccmath/isnan.hpp>
#include <boost/math/ccmath/isinf.hpp>

Expand All @@ -20,19 +21,29 @@ namespace boost::math::ccmath {
namespace detail {

template <typename T>
inline constexpr T abs_impl(T x) noexcept
constexpr T abs_impl(T x) noexcept
{
return boost::math::ccmath::isnan(x) ? std::numeric_limits<T>::quiet_NaN() :
boost::math::ccmath::isinf(x) ? std::numeric_limits<T>::infinity() :
x == -0 ? T(0) :
x == (std::numeric_limits<T>::min)() ? std::numeric_limits<T>::quiet_NaN() :
x > 0 ? x : -x;
if (boost::math::ccmath::isnan(x))
{
return std::numeric_limits<T>::quiet_NaN();
}
else if (x == static_cast<T>(-0))
{
return static_cast<T>(0);
}

if constexpr (std::is_integral_v<T>)
{
BOOST_MATH_ASSERT(x != (std::numeric_limits<T>::min)());
}

return x >= 0 ? x : -x;
}

} // Namespace detail

template <typename T, std::enable_if_t<!std::is_unsigned_v<T>, bool> = true>
inline constexpr T abs(T x) noexcept
constexpr T abs(T x) noexcept
{
if(BOOST_MATH_IS_CONSTANT_EVALUATED(x))
{
Expand All @@ -48,7 +59,7 @@ inline constexpr T abs(T x) noexcept
// If abs() is called with an argument of type X for which is_unsigned_v<X> is true and if X
// cannot be converted to int by integral promotion (7.3.7), the program is ill-formed.
template <typename T, std::enable_if_t<std::is_unsigned_v<T>, bool> = true>
inline constexpr T abs(T x) noexcept
constexpr T abs(T x) noexcept
{
if constexpr (std::is_convertible_v<T, int>)
{
Expand All @@ -61,12 +72,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);
}
Expand Down

0 comments on commit f395de0

Please sign in to comment.