Skip to content

Commit 88a0966

Browse files
authored
Merge pull request #1295 from cppalliance/1294
Fix conditional logic in `round` family
2 parents a33260d + b6954c2 commit 88a0966

File tree

3 files changed

+62
-2
lines changed

3 files changed

+62
-2
lines changed

include/boost/decimal/detail/cmath/round.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ constexpr auto round(const T num) noexcept
4343
T iptr {};
4444
const auto x {modf(num, &iptr)};
4545

46-
if (x >= half && iptr > 0)
46+
if (x >= half)
4747
{
4848
++iptr;
4949
}
50-
else if (abs(x) >= half && iptr < 0)
50+
else if (x <= -half)
5151
{
5252
--iptr;
5353
}

test/Jamfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ run github_issue_1110.cpp ;
8282
run github_issue_1112.cpp ;
8383
run github_issue_1174.cpp ;
8484
run github_issue_1260.cpp ;
85+
run github_issue_1294.cpp ;
8586

8687
run link_1.cpp link_2.cpp link_3.cpp ;
8788
run quick.cpp ;

test/github_issue_1294.cpp

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright 2025 Matt Borland
2+
// Distributed under the Boost Software License, Version 1.0.
3+
// https://www.boost.org/LICENSE_1_0.txt
4+
//
5+
// See: https://github.com/cppalliance/decimal/issues/1294
6+
7+
#include <boost/decimal.hpp>
8+
#include <boost/core/lightweight_test.hpp>
9+
10+
using namespace boost::decimal;
11+
12+
template <typename T>
13+
void test_round_down()
14+
{
15+
const T val {"0.499"};
16+
constexpr T ref_1 {1};
17+
constexpr T ref_0 {0};
18+
19+
BOOST_TEST_EQ(ceil(val), ref_1);
20+
BOOST_TEST_EQ(floor(val), ref_0);
21+
BOOST_TEST_EQ(trunc(val), ref_0);
22+
BOOST_TEST_EQ(round(val), ref_0);
23+
BOOST_TEST_EQ(lround(val), 0L);
24+
BOOST_TEST_EQ(nearbyint(val), 0);
25+
BOOST_TEST_EQ(lrint(val), 0L);
26+
BOOST_TEST_EQ(llrint(val), 0LL);
27+
}
28+
29+
template <typename T>
30+
void test()
31+
{
32+
const T val {"0.999"};
33+
constexpr T ref_1 {1};
34+
constexpr T ref_0 {0};
35+
36+
BOOST_TEST_EQ(ceil(val), ref_1);
37+
BOOST_TEST_EQ(floor(val), ref_0);
38+
BOOST_TEST_EQ(trunc(val), ref_0);
39+
BOOST_TEST_EQ(round(val), ref_1);
40+
BOOST_TEST_EQ(lround(val), 1L);
41+
BOOST_TEST_EQ(nearbyint(val), 1);
42+
BOOST_TEST_EQ(lrint(val), 1L);
43+
BOOST_TEST_EQ(llrint(val), 1LL);
44+
45+
test_round_down<T>();
46+
}
47+
48+
int main()
49+
{
50+
test<decimal32_t>();
51+
test<decimal64_t>();
52+
test<decimal128_t>();
53+
54+
test<decimal_fast32_t>();
55+
test<decimal_fast64_t>();
56+
test<decimal_fast128_t>();
57+
58+
return boost::report_errors();
59+
}

0 commit comments

Comments
 (0)