|
Boost : |
From: Gero Peterhoff (g.peterhoff_at_[hidden])
Date: 2022-11-10 19:47:11
Hi Matt,
ccmath::abs() gives wrong result for FP-values std::numeric_limits<FP-Type>::min() in constexpr context.
test case:
using type = float;
constexpr type
a = std::numeric_limits<type>::min(),
b = boost::math::ccmath::abs(a);
std::cout << a << std::endl << b << std::endl;
1.17549e-38
nan
The Problem is:
template <typename T>
inline 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;
}
In the case of FP-types, only the sign-bit has to be deleted, since one cannot assume that quiet_NaN() and/or signaling_NaN() are always unsigned.
thx
Gero
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk