Skip to content

Commit 30ed86c

Browse files
authored
Merge pull request #810 from cppalliance/799
Fix narrowing in equality of mixed types
2 parents 234a760 + 1116018 commit 30ed86c

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

include/boost/decimal/detail/comparison.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ template <BOOST_DECIMAL_DECIMAL_FLOATING_TYPE DecimalType = decimal32, BOOST_DEC
9696
constexpr auto equal_parts_impl(T1 lhs_sig, U1 lhs_exp, bool lhs_sign,
9797
T2 rhs_sig, U2 rhs_exp, bool rhs_sign) noexcept -> std::enable_if_t<std::is_same<DecimalType, decimal32>::value || std::is_same<DecimalType, decimal64>::value || std::is_same<DecimalType, decimal128>::value, bool>
9898
{
99-
using comp_type = typename DecimalType::significand_type;
99+
using comp_type = std::conditional_t<(std::numeric_limits<T1>::digits10 > std::numeric_limits<T2>::digits10), T1, T2>;
100100

101101
BOOST_DECIMAL_ASSERT(lhs_sig >= 0);
102102
BOOST_DECIMAL_ASSERT(rhs_sig >= 0);

test/Jamfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ run crash_report_1.cpp ;
4949
run github_issue_426.cpp ;
5050
run github_issue_448.cpp ;
5151
run-fail github_issue_519.cpp ;
52+
run github_issue_799.cpp ;
5253
run link_1.cpp link_2.cpp link_3.cpp ;
5354
run quick.cpp ;
5455
run random_decimal32_comp.cpp ;

test/github_issue_799.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
#include <boost/decimal.hpp>
6+
#include <boost/core/lightweight_test.hpp>
7+
#include <iostream>
8+
9+
#ifdef BOOST_DECIMAL_HAS_INT128
10+
11+
template<typename Dec>
12+
void mixed_compare()
13+
{
14+
const Dec a{2, 1};
15+
const auto b = __uint128_t{std::numeric_limits<__uint128_t>::max() - std::numeric_limits<uint64_t>::max() + 20};
16+
BOOST_TEST(a != b);
17+
}
18+
19+
int main()
20+
{
21+
using namespace boost::decimal;
22+
23+
mixed_compare<decimal32>();
24+
mixed_compare<decimal32_fast>();
25+
mixed_compare<decimal64>();
26+
mixed_compare<decimal64_fast>();
27+
mixed_compare<decimal128>();
28+
mixed_compare<decimal128_fast>();
29+
30+
return boost::report_errors();
31+
}
32+
33+
#else
34+
35+
int main()
36+
{
37+
return 0;
38+
}
39+
40+
#endif

0 commit comments

Comments
 (0)