Skip to content

Commit fe8207f

Browse files
authored
Merge pull request #901 from cppalliance/900
Fix comparisons to zero
2 parents 36b0954 + 4c86689 commit fe8207f

File tree

4 files changed

+48
-10
lines changed

4 files changed

+48
-10
lines changed

include/boost/decimal/detail/comparison.hpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,12 @@ BOOST_DECIMAL_FORCE_INLINE constexpr auto equality_impl(DecimalType lhs, Decimal
101101
template <BOOST_DECIMAL_FAST_DECIMAL_FLOATING_TYPE DecimalType>
102102
BOOST_DECIMAL_FORCE_INLINE constexpr auto fast_equality_impl(const DecimalType& lhs, const DecimalType& rhs) noexcept -> bool
103103
{
104+
if (lhs.significand_ == 0U && rhs.significand_ == 0U)
105+
{
106+
// -0 == +0
107+
return true;
108+
}
109+
104110
if (lhs.exponent_ != rhs.exponent_)
105111
{
106112
return false;
@@ -117,11 +123,6 @@ BOOST_DECIMAL_FORCE_INLINE constexpr auto fast_equality_impl(const DecimalType&
117123
}
118124
#endif
119125

120-
if (lhs.significand_ == 0U)
121-
{
122-
return true; // -0 == +0
123-
}
124-
125126
return lhs.sign_ == rhs.sign_;
126127
}
127128

include/boost/decimal/detail/power_tables.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -208,17 +208,17 @@ static_assert(sizeof(emulated_256_pow10) == sizeof(boost::decimal::detail::uint2
208208

209209
} // namespace impl
210210

211+
#if defined(__GNUC__) && __GNUC__ >= 7
212+
# pragma GCC diagnostic push
213+
# pragma GCC diagnostic ignored "-Warray-bounds"
214+
#endif
215+
211216
template <typename T>
212217
constexpr auto pow10(T n) noexcept -> T
213218
{
214219
return static_cast<T>(impl::powers_of_10[static_cast<std::size_t>(n)]);
215220
}
216221

217-
#if defined(__GNUC__) && __GNUC__ >= 7
218-
# pragma GCC diagnostic push
219-
# pragma GCC diagnostic ignored "-Warray-bounds"
220-
#endif
221-
222222
template <>
223223
constexpr auto pow10(const boost::int128::uint128_t n) noexcept -> boost::int128::uint128_t
224224
{

test/Jamfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ run github_issue_799.cpp ;
5454
run github_issue_802.cpp ;
5555
run github_issue_805.cpp ;
5656
run github_issue_808.cpp ;
57+
run github_issue_900.cpp ;
5758
run link_1.cpp link_2.cpp link_3.cpp ;
5859
run quick.cpp ;
5960
run random_decimal32_comp.cpp ;

test/github_issue_900.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
8+
using namespace boost::decimal;
9+
10+
template <typename Dec>
11+
void to_chars_helper()
12+
{
13+
const Dec default_value;
14+
char buffer[64];
15+
16+
const auto r = to_chars(buffer, buffer + sizeof(buffer), default_value);
17+
18+
if (BOOST_TEST(r))
19+
{
20+
*r.ptr = '\0';
21+
BOOST_TEST_CSTR_EQ(buffer, "0.0e+00");
22+
}
23+
}
24+
25+
int main()
26+
{
27+
to_chars_helper<decimal32>();
28+
to_chars_helper<decimal64>();
29+
to_chars_helper<decimal128>();
30+
31+
to_chars_helper<decimal32_fast>();
32+
to_chars_helper<decimal64_fast>();
33+
to_chars_helper<decimal128_fast>();
34+
35+
return boost::report_errors();
36+
}

0 commit comments

Comments
 (0)