mirror of
https://github.com/boostorg/math.git
synced 2026-02-26 16:52:27 +00:00
@@ -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>
|
||||
|
||||
@@ -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))
|
||||
{
|
||||
@@ -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>)
|
||||
{
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user