Skip to content

Commit a4f6671

Browse files
committed
Use fenv rounding to achieve correct value
1 parent b9dcbee commit a4f6671

File tree

1 file changed

+7
-12
lines changed
  • include/boost/decimal/detail/cmath

1 file changed

+7
-12
lines changed

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

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,15 @@ namespace decimal {
2828

2929
namespace detail {
3030

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

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

38-
const auto trailing_num {static_cast<std::uint32_t>(sig % 10U)};
39-
sig /= 10U;
40-
41-
if (trailing_num >= 5U)
42-
{
43-
++sig;
44-
}
39+
detail::fenv_round<TargetType>(sig, is_neg, res.remainder != 0U);
4540
}
4641

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

100-
detail::rint_impl(sig, expptr, is_neg);
95+
detail::rint_impl<T>(sig, expptr, is_neg);
10196

10297
auto res {static_cast<Int>(sig)};
10398
if (is_neg)
@@ -147,7 +142,7 @@ constexpr auto rint(const T num) noexcept
147142
return is_neg ? -zero : zero;
148143
}
149144

150-
detail::rint_impl(sig, expptr, is_neg);
145+
detail::rint_impl<T>(sig, expptr, is_neg);
151146

152147
return {sig, 0, is_neg};
153148
}

0 commit comments

Comments
 (0)