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
19 changes: 7 additions & 12 deletions include/boost/decimal/detail/cmath/rint.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,15 @@ namespace decimal {

namespace detail {

template <BOOST_DECIMAL_INTEGRAL T1, BOOST_DECIMAL_INTEGRAL T2>
constexpr auto rint_impl(T1& sig, T2 exp, const bool)
template <BOOST_DECIMAL_DECIMAL_FLOATING_TYPE TargetType, BOOST_DECIMAL_INTEGRAL T1, BOOST_DECIMAL_INTEGRAL T2>
constexpr auto rint_impl(T1& sig, T2 exp, const bool is_neg)
{
const T2 abs_exp { (exp < T2(0)) ? -exp : exp };

sig /= detail::pow10(static_cast<T1>(abs_exp - 1));
const auto res {detail::impl::divmod(sig, detail::pow10(static_cast<T1>(abs_exp - 1)))};
sig = res.quotient;

const auto trailing_num {static_cast<std::uint32_t>(sig % 10U)};
sig /= 10U;

if (trailing_num >= 5U)
{
++sig;
}
detail::fenv_round<TargetType>(sig, is_neg, res.remainder != 0U);
}

// MSVC 14.1 warns of unary minus being applied to unsigned type from numeric_limits::min
Expand Down Expand Up @@ -97,7 +92,7 @@ constexpr auto lrint_impl(const T num) noexcept -> Int
return 0;
}

detail::rint_impl(sig, expptr, is_neg);
detail::rint_impl<T>(sig, expptr, is_neg);

auto res {static_cast<Int>(sig)};
if (is_neg)
Expand Down Expand Up @@ -147,7 +142,7 @@ constexpr auto rint(const T num) noexcept
return is_neg ? -zero : zero;
}

detail::rint_impl(sig, expptr, is_neg);
detail::rint_impl<T>(sig, expptr, is_neg);

return {sig, 0, is_neg};
}
Expand Down
3 changes: 3 additions & 0 deletions test/Jamfile
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ run-fail benchmark_uint256.cpp ;

run compare_dec128_and_fast.cpp ;
compile-fail concepts_test.cpp ;

run crash_report_1.cpp ;
run github_issue_426.cpp ;
run github_issue_448.cpp ;
Expand All @@ -69,6 +70,8 @@ run github_issue_1057.cpp ;
compile-fail github_issue_1087.cpp ;
run github_issue_1091.cpp ;
run github_issue_1094.cpp ;
run github_issue_1112.cpp ;

run link_1.cpp link_2.cpp link_3.cpp ;
run quick.cpp ;
run random_decimal32_comp.cpp ;
Expand Down
63 changes: 63 additions & 0 deletions test/github_issue_1112.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// 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/1112

#include <boost/decimal.hpp>
#include <boost/core/lightweight_test.hpp>
#include <random>

static std::mt19937_64 rng(42);
std::uniform_int_distribution<> dist(-1, -1);

using namespace boost::decimal;

template <typename T>
void reproducer()
{
const auto dec_res {static_cast<int>(nearbyint(T{2325, -1}))};
const auto dbl_res {static_cast<int>(std::nearbyint(232.5))};
BOOST_TEST_EQ(dec_res, dbl_res);
}

template <typename T>
void test_rounding_up()
{
const auto mode {boost::decimal::fesetround(rounding_mode::fe_dec_upward)};
if (mode != rounding_mode::fe_dec_default)
{
const T value {2325, dist(rng)}; // The result will be wrong if computed at compile time
const auto dec_res {static_cast<int>(nearbyint(value))};
BOOST_TEST_EQ(dec_res, 233);
}
}

template <typename T>
void test_rounding_down()
{
const auto mode {boost::decimal::fesetround(rounding_mode::fe_dec_downward)};
if (mode != rounding_mode::fe_dec_default)
{
const T value {2325, dist(rng)}; // The result will be wrong if computed at compile time
const auto dec_res {static_cast<int>(nearbyint(value))};
BOOST_TEST_EQ(dec_res, 232);
}
}

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

test_rounding_up<decimal32_t>();
test_rounding_up<decimal64_t>();
test_rounding_up<decimal128_t>();

test_rounding_down<decimal32_t>();
test_rounding_down<decimal64_t>();
test_rounding_down<decimal128_t>();

return boost::report_errors();
}