Skip to content
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
4 changes: 2 additions & 2 deletions include/boost/decimal/detail/cmath/round.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
1 change: 1 addition & 0 deletions test/Jamfile
Original file line number Diff line number Diff line change
Expand Up @@ -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 ;
Expand Down
59 changes: 59 additions & 0 deletions test/github_issue_1294.cpp
Original file line number Diff line number Diff line change
@@ -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 <boost/decimal.hpp>
#include <boost/core/lightweight_test.hpp>

using namespace boost::decimal;

template <typename T>
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 <typename T>
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<T>();
}

int main()
{
test<decimal32_t>();
test<decimal64_t>();
test<decimal128_t>();

test<decimal_fast32_t>();
test<decimal_fast64_t>();
test<decimal_fast128_t>();

return boost::report_errors();
}
Loading