diff --git a/include/boost/decimal/detail/cmath/round.hpp b/include/boost/decimal/detail/cmath/round.hpp index 0ed3fc16f..bbb478306 100644 --- a/include/boost/decimal/detail/cmath/round.hpp +++ b/include/boost/decimal/detail/cmath/round.hpp @@ -43,11 +43,11 @@ constexpr auto round(const T num) noexcept T iptr {}; const auto x {modf(num, &iptr)}; - if (x >= half && iptr > 0) + if (x >= half) { ++iptr; } - else if (abs(x) >= half && iptr < 0) + else if (x <= -half) { --iptr; } diff --git a/test/Jamfile b/test/Jamfile index 4d353e375..e318db689 100644 --- a/test/Jamfile +++ b/test/Jamfile @@ -82,6 +82,7 @@ run github_issue_1110.cpp ; run github_issue_1112.cpp ; run github_issue_1174.cpp ; run github_issue_1260.cpp ; +run github_issue_1294.cpp ; run link_1.cpp link_2.cpp link_3.cpp ; run quick.cpp ; diff --git a/test/github_issue_1294.cpp b/test/github_issue_1294.cpp new file mode 100644 index 000000000..f1b872d27 --- /dev/null +++ b/test/github_issue_1294.cpp @@ -0,0 +1,59 @@ +// Copyright 2025 Matt Borland +// Distributed under the Boost Software License, Version 1.0. +// https://www.boost.org/LICENSE_1_0.txt +// +// See: https://github.com/cppalliance/decimal/issues/1294 + +#include +#include + +using namespace boost::decimal; + +template +void test_round_down() +{ + const T val {"0.499"}; + constexpr T ref_1 {1}; + constexpr T ref_0 {0}; + + BOOST_TEST_EQ(ceil(val), ref_1); + BOOST_TEST_EQ(floor(val), ref_0); + BOOST_TEST_EQ(trunc(val), ref_0); + BOOST_TEST_EQ(round(val), ref_0); + BOOST_TEST_EQ(lround(val), 0L); + BOOST_TEST_EQ(nearbyint(val), 0); + BOOST_TEST_EQ(lrint(val), 0L); + BOOST_TEST_EQ(llrint(val), 0LL); +} + +template +void test() +{ + const T val {"0.999"}; + constexpr T ref_1 {1}; + constexpr T ref_0 {0}; + + BOOST_TEST_EQ(ceil(val), ref_1); + BOOST_TEST_EQ(floor(val), ref_0); + BOOST_TEST_EQ(trunc(val), ref_0); + BOOST_TEST_EQ(round(val), ref_1); + BOOST_TEST_EQ(lround(val), 1L); + BOOST_TEST_EQ(nearbyint(val), 1); + BOOST_TEST_EQ(lrint(val), 1L); + BOOST_TEST_EQ(llrint(val), 1LL); + + test_round_down(); +} + +int main() +{ + test(); + test(); + test(); + + test(); + test(); + test(); + + return boost::report_errors(); +}