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

Correct float_next(+INF) and float_prior(-INF) #1135

Merged
merged 2 commits into from
May 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 24 additions & 8 deletions include/boost/math/special_functions/next.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,14 @@ T float_next_imp(const T& val, const std::true_type&, const Policy& pol)

int fpclass = (boost::math::fpclassify)(val);

if((fpclass == (int)FP_NAN) || (fpclass == (int)FP_INFINITE))
if (fpclass == (int)FP_INFINITE)
{
if(val < 0)
if (val < 0)
return -tools::max_value<T>();
return val; // +INF
}
else if (fpclass == (int)FP_NAN)
{
return policies::raise_domain_error<T>(
function,
"Argument must be finite, but got %1%", val, pol);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These error messages are no longer really accurate, the argument can be an infinity which is of course not finite.

Expand Down Expand Up @@ -243,10 +247,14 @@ T float_next_imp(const T& val, const std::false_type&, const Policy& pol)

int fpclass = (boost::math::fpclassify)(val);

if((fpclass == (int)FP_NAN) || (fpclass == (int)FP_INFINITE))
if (fpclass == (int)FP_INFINITE)
{
if(val < 0)
if (val < 0)
return -tools::max_value<T>();
return val; // +INF
}
else if (fpclass == (int)FP_NAN)
{
return policies::raise_domain_error<T>(
function,
"Argument must be finite, but got %1%", val, pol);
Expand Down Expand Up @@ -328,10 +336,14 @@ T float_prior_imp(const T& val, const std::true_type&, const Policy& pol)

int fpclass = (boost::math::fpclassify)(val);

if((fpclass == (int)FP_NAN) || (fpclass == (int)FP_INFINITE))
if (fpclass == (int)FP_INFINITE)
{
if(val > 0)
if (val > 0)
return tools::max_value<T>();
return val; // -INF
}
else if (fpclass == (int)FP_NAN)
{
return policies::raise_domain_error<T>(
function,
"Argument must be finite, but got %1%", val, pol);
Expand Down Expand Up @@ -378,10 +390,14 @@ T float_prior_imp(const T& val, const std::false_type&, const Policy& pol)

int fpclass = (boost::math::fpclassify)(val);

if((fpclass == (int)FP_NAN) || (fpclass == (int)FP_INFINITE))
if (fpclass == (int)FP_INFINITE)
{
if(val > 0)
if (val > 0)
return tools::max_value<T>();
return val; // -INF
}
else if (fpclass == (int)FP_NAN)
{
return policies::raise_domain_error<T>(
function,
"Argument must be finite, but got %1%", val, pol);
Expand Down
11 changes: 8 additions & 3 deletions test/test_next.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,12 @@ void test_values(const T& val, const char* name)
BOOST_CHECK_EQUAL(boost::math::float_advance(val, primes[i]), v1);
BOOST_CHECK_EQUAL(boost::math::float_advance(val, -primes[i]), v2);
}
if(std::numeric_limits<T>::is_specialized && (std::numeric_limits<T>::has_infinity))
BOOST_IF_CONSTEXPR(std::numeric_limits<T>::is_specialized && (std::numeric_limits<T>::has_infinity))
{
BOOST_CHECK_EQUAL(boost::math::float_prior(std::numeric_limits<T>::infinity()), (std::numeric_limits<T>::max)());
BOOST_CHECK_EQUAL(boost::math::float_next(-std::numeric_limits<T>::infinity()), -(std::numeric_limits<T>::max)());
BOOST_MATH_CHECK_THROW(boost::math::float_prior(-std::numeric_limits<T>::infinity()), std::domain_error);
BOOST_MATH_CHECK_THROW(boost::math::float_next(std::numeric_limits<T>::infinity()), std::domain_error);
BOOST_CHECK_EQUAL(boost::math::float_prior(-std::numeric_limits<T>::infinity()), -std::numeric_limits<T>::infinity());
BOOST_CHECK_EQUAL(boost::math::float_next(std::numeric_limits<T>::infinity()), std::numeric_limits<T>::infinity());
if(boost::math::policies:: BOOST_MATH_OVERFLOW_ERROR_POLICY == boost::math::policies::throw_on_error)
{
BOOST_MATH_CHECK_THROW(boost::math::float_prior(-(std::numeric_limits<T>::max)()), std::overflow_error);
Expand All @@ -188,6 +188,11 @@ void test_values(const T& val, const char* name)
BOOST_CHECK_EQUAL(boost::math::float_next((std::numeric_limits<T>::max)()), std::numeric_limits<T>::infinity());
}
}
BOOST_IF_CONSTEXPR(std::numeric_limits<T>::is_specialized && (std::numeric_limits<T>::has_quiet_NaN))
{
BOOST_MATH_CHECK_THROW(boost::math::float_prior((std::numeric_limits<T>::quiet_NaN)()), std::domain_error);
BOOST_MATH_CHECK_THROW(boost::math::float_next((std::numeric_limits<T>::quiet_NaN)()), std::domain_error);
}
//
// We need to test float_distance over multiple orders of magnitude,
// the only way to get an accurate true result is to count the representations
Expand Down
11 changes: 8 additions & 3 deletions test/test_next_decimal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,12 +176,12 @@ void test_values(const T& val, const char* name)
BOOST_CHECK_EQUAL(boost::math::float_advance(val, primes[i]), v1);
BOOST_CHECK_EQUAL(boost::math::float_advance(val, -primes[i]), v2);
}
if(std::numeric_limits<T>::is_specialized && (std::numeric_limits<T>::has_infinity))
BOOST_IF_CONSTEXPR(std::numeric_limits<T>::is_specialized && (std::numeric_limits<T>::has_infinity))
{
BOOST_CHECK_EQUAL(boost::math::float_prior(std::numeric_limits<T>::infinity()), (std::numeric_limits<T>::max)());
BOOST_CHECK_EQUAL(boost::math::float_next(-std::numeric_limits<T>::infinity()), -(std::numeric_limits<T>::max)());
BOOST_MATH_CHECK_THROW(boost::math::float_prior(-std::numeric_limits<T>::infinity()), std::domain_error);
BOOST_MATH_CHECK_THROW(boost::math::float_next(std::numeric_limits<T>::infinity()), std::domain_error);
BOOST_CHECK_EQUAL(boost::math::float_prior(-std::numeric_limits<T>::infinity()), -std::numeric_limits<T>::infinity());
BOOST_CHECK_EQUAL(boost::math::float_next(std::numeric_limits<T>::infinity()), std::numeric_limits<T>::infinity());
if(boost::math::policies:: BOOST_MATH_OVERFLOW_ERROR_POLICY == boost::math::policies::throw_on_error)
{
BOOST_MATH_CHECK_THROW(boost::math::float_prior(-(std::numeric_limits<T>::max)()), std::overflow_error);
Expand All @@ -193,6 +193,11 @@ void test_values(const T& val, const char* name)
BOOST_CHECK_EQUAL(boost::math::float_next((std::numeric_limits<T>::max)()), std::numeric_limits<T>::infinity());
}
}
BOOST_IF_CONSTEXPR(std::numeric_limits<T>::is_specialized && (std::numeric_limits<T>::has_quiet_NaN))
{
BOOST_MATH_CHECK_THROW(boost::math::float_prior(std::numeric_limits<T>::quiet_NaN()), std::domain_error);
BOOST_MATH_CHECK_THROW(boost::math::float_next(std::numeric_limits<T>::quiet_NaN()), std::domain_error);
}
}

BOOST_AUTO_TEST_CASE( test_main )
Expand Down