ccmath::abs() gives wrong result
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
On Nov 10, 2022, at 11:47 AM, Gero Peterhoff via Boost
wrote: 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
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Gero, I have opened your issue in Boost.Math (https://github.com/boostorg/math/issues/871). I should be able to get a fix in shortly. Matt
participants (2)
-
Gero Peterhoff
-
Proton