Skip to content

Commit 77aaa67

Browse files
committed
Fix handling of comparisons to zero
1 parent 2653ea3 commit 77aaa67

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

include/boost/decimal/detail/comparison.hpp

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,12 @@ constexpr auto equal_parts_impl(T1 lhs_sig, U1 lhs_exp, bool lhs_sign,
182182
BOOST_DECIMAL_ASSERT(lhs_sig >= 0U);
183183
BOOST_DECIMAL_ASSERT(rhs_sig >= 0U);
184184

185+
if (lhs_sig == 0U && rhs_sig == 0U)
186+
{
187+
// +0 == -0
188+
return true;
189+
}
190+
185191
// We con compare signs right away
186192
if (lhs_sign != rhs_sign)
187193
{
@@ -192,10 +198,6 @@ constexpr auto equal_parts_impl(T1 lhs_sig, U1 lhs_exp, bool lhs_sign,
192198

193199
// Check the value of delta exp to avoid to large a value for pow10
194200
// Also if only one of the significands is 0 then we know the values have to be mismatched
195-
if (lhs_sig == 0U && rhs_sig == 0U)
196-
{
197-
return true;
198-
}
199201
if (delta_exp > detail::precision_v<DecimalType> || delta_exp < -detail::precision_v<DecimalType>)
200202
{
201203
return false;
@@ -270,14 +272,20 @@ constexpr auto equal_parts_impl(T1 lhs_sig, U1 lhs_exp, bool lhs_sign,
270272

271273
template <BOOST_DECIMAL_DECIMAL_FLOATING_TYPE DecimalType = decimal32_t, BOOST_DECIMAL_INTEGRAL T1,
272274
BOOST_DECIMAL_INTEGRAL U1, BOOST_DECIMAL_INTEGRAL T2, BOOST_DECIMAL_INTEGRAL U2>
273-
constexpr auto equal_parts_impl(T1 lhs_sig, U1 lhs_exp, bool lhs_sign,
274-
T2 rhs_sig, U2 rhs_exp, bool rhs_sign) noexcept -> std::enable_if_t<detail::is_fast_type_v<DecimalType>, bool>
275+
constexpr auto equal_parts_impl(T1 lhs_sig, U1 lhs_exp, const bool lhs_sign,
276+
T2 rhs_sig, U2 rhs_exp, const bool rhs_sign) noexcept -> std::enable_if_t<detail::is_fast_type_v<DecimalType>, bool>
275277
{
276278
using comp_type = std::conditional_t<(std::numeric_limits<T1>::digits10 > std::numeric_limits<T2>::digits10), T1, T2>;
277279

278280
BOOST_DECIMAL_ASSERT(lhs_sig >= 0U);
279281
BOOST_DECIMAL_ASSERT(rhs_sig >= 0U);
280282

283+
if (lhs_sig == 0U && rhs_sig == 0U)
284+
{
285+
// +0 == -0
286+
return true;
287+
}
288+
281289
auto new_lhs_sig {static_cast<comp_type>(lhs_sig)};
282290
auto new_rhs_sig {static_cast<comp_type>(rhs_sig)};
283291

0 commit comments

Comments
 (0)