Skip to content

Commit d5a1bee

Browse files
committed
Fix bug in constexpr round implementations causing values in the range 0.5-1.0 to round to zero
1 parent a10f3c8 commit d5a1bee

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

include/boost/math/ccmath/round.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ inline constexpr T round_impl(T arg) noexcept
3131
const T x = boost::math::ccmath::modf(arg, &iptr);
3232
constexpr T half = T(1)/2;
3333

34-
if(x >= half && iptr > 0)
34+
if(x >= half && iptr >= 0)
3535
{
3636
return iptr + 1;
3737
}
38-
else if(boost::math::ccmath::abs(x) >= half && iptr < 0)
38+
else if(boost::math::ccmath::abs(x) >= half && iptr <= 0)
3939
{
4040
return iptr - 1;
4141
}

test/ccmath_round_test.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,23 +40,41 @@ constexpr void test()
4040
static_assert(boost::math::ccmath::lround(std::numeric_limits<T>::infinity()) == 0l);
4141
static_assert(boost::math::ccmath::llround(std::numeric_limits<T>::infinity()) == 0ll);
4242

43+
static_assert(boost::math::ccmath::round(T(0.1)) == T(0));
44+
static_assert(boost::math::ccmath::round(T(0.5)) == T(1));
45+
static_assert(boost::math::ccmath::round(T(0.9)) == T(1));
4346
static_assert(boost::math::ccmath::round(T(2.3)) == T(2));
4447
static_assert(boost::math::ccmath::round(T(2.5)) == T(3));
4548
static_assert(boost::math::ccmath::round(T(2.7)) == T(3));
49+
static_assert(boost::math::ccmath::round(T(-0.1)) == T(0));
50+
static_assert(boost::math::ccmath::round(T(-0.5)) == T(-1));
51+
static_assert(boost::math::ccmath::round(T(-0.9)) == T(-1));
4652
static_assert(boost::math::ccmath::round(T(-2.3)) == T(-2));
4753
static_assert(boost::math::ccmath::round(T(-2.5)) == T(-3));
4854
static_assert(boost::math::ccmath::round(T(-2.7)) == T(-3));
4955

56+
static_assert(boost::math::ccmath::lround(T(0.1)) == 0l);
57+
static_assert(boost::math::ccmath::lround(T(0.5)) == 1l);
58+
static_assert(boost::math::ccmath::lround(T(0.9)) == 1l);
5059
static_assert(boost::math::ccmath::lround(T(2.3)) == 2l);
5160
static_assert(boost::math::ccmath::lround(T(2.5)) == 3l);
5261
static_assert(boost::math::ccmath::lround(T(2.7)) == 3l);
62+
static_assert(boost::math::ccmath::lround(T(-0.1)) == 0l);
63+
static_assert(boost::math::ccmath::lround(T(-0.5)) == -1l);
64+
static_assert(boost::math::ccmath::lround(T(-0.9)) == -1l);
5365
static_assert(boost::math::ccmath::lround(T(-2.3)) == -2l);
5466
static_assert(boost::math::ccmath::lround(T(-2.5)) == -3l);
5567
static_assert(boost::math::ccmath::lround(T(-2.7)) == -3l);
5668

69+
static_assert(boost::math::ccmath::llround(T(0.1)) == 0ll);
70+
static_assert(boost::math::ccmath::llround(T(0.5)) == 1ll);
71+
static_assert(boost::math::ccmath::llround(T(0.9)) == 1ll);
5772
static_assert(boost::math::ccmath::llround(T(2.3)) == 2ll);
5873
static_assert(boost::math::ccmath::llround(T(2.5)) == 3ll);
5974
static_assert(boost::math::ccmath::llround(T(2.7)) == 3ll);
75+
static_assert(boost::math::ccmath::llround(T(-0.1)) == 0ll);
76+
static_assert(boost::math::ccmath::llround(T(-0.5)) == -1ll);
77+
static_assert(boost::math::ccmath::llround(T(-0.9)) == -1ll);
6078
static_assert(boost::math::ccmath::llround(T(-2.3)) == -2ll);
6179
static_assert(boost::math::ccmath::llround(T(-2.5)) == -3ll);
6280
static_assert(boost::math::ccmath::llround(T(-2.7)) == -3ll);

0 commit comments

Comments
 (0)