-
Notifications
You must be signed in to change notification settings - Fork 257
Guard against intermediate x*x overflow in Student's t pdf/cdf #1402
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
Open
a-leontyev
wants to merge
7
commits into
boostorg:develop
Choose a base branch
from
a-leontyev:fix/students-t-overflow
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+146
−0
Open
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4d863b1
Guard against intermediate x*x overflow in Student's t pdf/cdf
LeantionX bc966c5
Use std::fabs to avoid -Wabsolute-value truncation on long double
LeantionX 892b4ce
Use ADL fabs and extend overflow test to non-standard types
LeantionX efa8588
Disable testing of boost::mp in standalone mode
mborland 01e41d5
Also skip real_concept in standalone mode (lexical_cast unavailable)
a-leontyev 455bee3
Drop errno==0 assertions on success paths (UBSan report printing clob…
a-leontyev 1e7a2c7
Return exact tail values instead of raising overflow_error (review fe…
a-leontyev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| // Copyright Anton Leontev 2026. | ||
| // Use, modification and distribution are subject to the | ||
| // Boost Software License, Version 1.0. | ||
| // (See accompanying file LICENSE_1_0.txt or copy at | ||
| // http://www.boost.org/LICENSE_1_0.txt) | ||
| // | ||
| // Regression test for intermediate overflow of x*x in the Student's t | ||
| // distribution pdf() and cdf(). | ||
|
|
||
| #define BOOST_TEST_MAIN | ||
| #include <boost/test/unit_test.hpp> | ||
| #include <boost/test/tools/floating_point_comparison.hpp> | ||
| #include <boost/math/tools/test.hpp> | ||
|
|
||
| #include <boost/math/distributions/students_t.hpp> | ||
| #include <boost/math/policies/policy.hpp> | ||
| #include <boost/math/special_functions/fpclassify.hpp> | ||
| #include <boost/math/tools/precision.hpp> | ||
|
|
||
| #include <cerrno> | ||
| #include <cmath> | ||
| #include <limits> | ||
|
|
||
| template <class RealType> | ||
| RealType overflowing_deviate() | ||
| { | ||
| using boost::math::tools::max_value; | ||
| return 2 * std::sqrt(max_value<RealType>()); | ||
| } | ||
|
|
||
| template <class RealType> | ||
| void test_throws(RealType) | ||
| { | ||
| using namespace boost::math; | ||
| using namespace boost::math::policies; | ||
|
|
||
| typedef policy<overflow_error<throw_on_error> > throw_policy; | ||
| typedef students_t_distribution<RealType, throw_policy> dist_t; | ||
|
|
||
| dist_t dist(static_cast<RealType>(5)); | ||
| const RealType big = overflowing_deviate<RealType>(); | ||
|
|
||
| BOOST_CHECK((boost::math::isfinite)(big)); | ||
|
|
||
| BOOST_CHECK_THROW(pdf(dist, big), std::overflow_error); | ||
| BOOST_CHECK_THROW(pdf(dist, -big), std::overflow_error); | ||
| BOOST_CHECK_THROW(cdf(dist, big), std::overflow_error); | ||
| BOOST_CHECK_THROW(cdf(dist, -big), std::overflow_error); | ||
| } | ||
|
|
||
| template <class RealType> | ||
| void test_errno(RealType) | ||
| { | ||
| using namespace boost::math; | ||
| using namespace boost::math::policies; | ||
|
|
||
| typedef policy<overflow_error<errno_on_error> > errno_policy; | ||
| typedef students_t_distribution<RealType, errno_policy> dist_t; | ||
|
|
||
| dist_t dist(static_cast<RealType>(5)); | ||
| const RealType big = overflowing_deviate<RealType>(); | ||
|
|
||
| errno = 0; | ||
| RealType r = pdf(dist, big); | ||
| BOOST_CHECK_EQUAL(errno, ERANGE); | ||
| BOOST_CHECK(!(boost::math::isfinite)(r)); | ||
|
|
||
| errno = 0; | ||
| r = cdf(dist, big); | ||
| BOOST_CHECK_EQUAL(errno, ERANGE); | ||
| BOOST_CHECK(!(boost::math::isfinite)(r)); | ||
| } | ||
|
|
||
| template <class RealType> | ||
| void test_no_regression(RealType) | ||
| { | ||
| using namespace boost::math; | ||
| using namespace boost::math::policies; | ||
|
|
||
| typedef policy<overflow_error<errno_on_error> > errno_policy; | ||
| typedef students_t_distribution<RealType, errno_policy> dist_t; | ||
|
|
||
| dist_t dist(static_cast<RealType>(10)); | ||
| const RealType tol = boost::math::tools::epsilon<RealType>() * 100; | ||
|
|
||
| errno = 0; | ||
| BOOST_CHECK_CLOSE_FRACTION(cdf(dist, static_cast<RealType>(0)), | ||
| static_cast<RealType>(0.5), tol); | ||
| BOOST_CHECK_EQUAL(errno, 0); | ||
|
|
||
| errno = 0; | ||
| RealType p = pdf(dist, static_cast<RealType>(1.5)); | ||
| BOOST_CHECK_EQUAL(errno, 0); | ||
| BOOST_CHECK((boost::math::isfinite)(p)); | ||
| BOOST_CHECK(p > 0); | ||
|
|
||
| errno = 0; | ||
| RealType big_but_safe = static_cast<RealType>(1e6); | ||
| RealType c = cdf(dist, big_but_safe); | ||
| BOOST_CHECK_EQUAL(errno, 0); | ||
| BOOST_CHECK_CLOSE_FRACTION(c, static_cast<RealType>(1), tol); | ||
| } | ||
|
|
||
| BOOST_AUTO_TEST_CASE(students_t_overflow_test) | ||
|
mborland marked this conversation as resolved.
|
||
| { | ||
| test_throws(0.0F); | ||
| test_throws(0.0); | ||
| test_throws(0.0L); | ||
|
|
||
| test_errno(0.0F); | ||
| test_errno(0.0); | ||
| test_errno(0.0L); | ||
|
|
||
| test_no_regression(0.0F); | ||
| test_no_regression(0.0); | ||
| test_no_regression(0.0L); | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.