Skip to content

Commit

Permalink
Fix for issue boostorg#871
Browse files Browse the repository at this point in the history
  • Loading branch information
mborland committed Nov 11, 2022
1 parent 4631716 commit d0514e5
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions include/boost/math/ccmath/abs.hpp
Expand Up @@ -11,6 +11,7 @@
#include <cmath>
#include <type_traits>
#include <limits>
#include <cassert>
#include <boost/math/tools/is_constant_evaluated.hpp>
#include <boost/math/ccmath/isnan.hpp>
#include <boost/math/ccmath/isinf.hpp>
Expand All @@ -28,19 +29,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 x;
}
else if (x == static_cast<T>(-0))
{
return static_cast<T>(0);
}

if constexpr (std::is_integral_v<T>)
{
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 @@ -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<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 @@ -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);
}
Expand Down

0 comments on commit d0514e5

Please sign in to comment.