Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Floating Point Edge Case in ccmath::abs #871

Closed
mborland opened this issue Nov 11, 2022 · 0 comments · Fixed by #872
Closed

Floating Point Edge Case in ccmath::abs #871

mborland opened this issue Nov 11, 2022 · 0 comments · Fixed by #872

Comments

@mborland
Copy link
Member

From the ML:

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
mborland added a commit to mborland/math that referenced this issue Nov 11, 2022
@mborland mborland linked a pull request Nov 11, 2022 that will close this issue
mborland added a commit that referenced this issue Nov 13, 2022
AreaZR pushed a commit to AreaZR/math that referenced this issue Nov 26, 2022
mborland added a commit that referenced this issue Dec 7, 2022
mborland added a commit that referenced this issue Dec 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant