diff --git a/include/boost/math/special_functions/round.hpp b/include/boost/math/special_functions/round.hpp index d3e661635..8849437a0 100644 --- a/include/boost/math/special_functions/round.hpp +++ b/include/boost/math/special_functions/round.hpp @@ -14,6 +14,7 @@ #include #include #include +#include #include #include @@ -27,13 +28,15 @@ inline ResultType float_to_int(T x) { BOOST_MATH_STD_USING + constexpr int sign = std::is_signed::value ? -1 : 0; + T y = floor(x); if (y < static_cast(0.0L)) { return 0; } - if (y > ldexp(static_cast(1.0L), sizeof(ResultType) * CHAR_BIT)) + if (y >= ldexp(static_cast(1.0L), (sizeof(ResultType) * CHAR_BIT) + sign) + sign) { return (std::numeric_limits::max)(); } @@ -161,8 +164,8 @@ inline long long llround(const T& v, const Policy& pol) long long return_val = boost::math::detail::float_to_int(r); bool representable = boost::math::detail::is_representable(r); - if (r < static_cast((std::numeric_limits::min)()) || - (return_val == (std::numeric_limits::max)() && !representable) || + if ((return_val == (std::numeric_limits::max)() && !representable) || + r < static_cast((std::numeric_limits::min)()) || r > static_cast((std::numeric_limits::max)())) { return static_cast(policies::raise_rounding_error("boost::math::llround<%1%>(%1%)", nullptr, v, static_cast(0), pol)); diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index 9a3ea8ae2..7396bc4a8 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -535,6 +535,7 @@ test-suite special_fun : [ run test_polygamma.cpp test_instances//test_instances pch_light ../../test/build//boost_unit_test_framework ] [ run test_trigamma.cpp test_instances//test_instances ../../test/build//boost_unit_test_framework ] [ run test_round.cpp pch ../../test/build//boost_unit_test_framework ] + [ run git_issue_430.cpp pch ../../test/build//boost_unit_test_framework ] [ run test_spherical_harmonic.cpp test_instances//test_instances pch_light ../../test/build//boost_unit_test_framework ] [ run test_sign.cpp ../../test/build//boost_unit_test_framework ] [ run test_tgamma_for_issue396_part1.cpp ../../test/build//boost_unit_test_framework : : : [ check-target-builds ../config//has_float128 "GCC libquadmath and __float128 support" : "-Bstatic -lquadmath -Bdynamic" ] gcc-mingw:-Wa,-mbig-obj off msvc:/bigobj [ check-target-builds ../config//is_cygwin_run "Cygwin CI run" : no ] ] @@ -890,7 +891,6 @@ test-suite distribution_tests : [ run complex_test.cpp ../../test/build//boost_unit_test_framework ] [ compile test_dist_deduction_guides.cpp : [ requires cpp_deduction_guides cpp_variadic_templates ] ] - [ run git_issue_430.cpp ../../test/build//boost_unit_test_framework ] [ run git_issue_800.cpp ../../test/build//boost_unit_test_framework ] [ run git_issue_845.cpp ../../test/build//boost_unit_test_framework ] [ run scipy_issue_14901.cpp ../../test/build//boost_unit_test_framework ] diff --git a/test/git_issue_430.cpp b/test/git_issue_430.cpp index b23ffc587..fd6bc89a9 100644 --- a/test/git_issue_430.cpp +++ b/test/git_issue_430.cpp @@ -2,6 +2,8 @@ // 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) +// +// See: https://godbolt.org/z/Ev4ManrsW #include #include