Skip to content

Commit 0344048

Browse files
authored
Merge pull request #1130 from cppalliance/1110v2
Fix detection of pure powers of 10 for large significands
2 parents 8b74b82 + 26b474e commit 0344048

File tree

7 files changed

+194
-149
lines changed

7 files changed

+194
-149
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ constexpr auto cbrt_impl(const T x) noexcept
6161
remove_trailing_zeros(gn)
6262
};
6363

64-
const bool is_pure { static_cast<unsigned>(zeros_removal.trimmed_number) == 1U };
64+
const bool is_pure { zeros_removal.trimmed_number == 1U };
6565

6666
if(is_pure)
6767
{

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ constexpr auto log10_impl(const T x) noexcept
6363
remove_trailing_zeros(gn)
6464
};
6565

66-
const bool is_pure { static_cast<unsigned>(zeros_removal.trimmed_number) == 1U };
66+
const bool is_pure { zeros_removal.trimmed_number == 1U };
6767

6868
if(is_pure)
6969
{

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ constexpr auto sqrt_impl(const T x) noexcept
6161
remove_trailing_zeros(gn)
6262
};
6363

64-
const bool is_pure { static_cast<unsigned>(zeros_removal.trimmed_number) == 1U };
64+
const bool is_pure { zeros_removal.trimmed_number == 1U };
6565

6666
constexpr T one { 1 };
6767

include/boost/decimal/detail/power_tables.hpp

Lines changed: 119 additions & 124 deletions
Large diffs are not rendered by default.

test/Jamfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ run github_issue_1057.cpp ;
7070
compile-fail github_issue_1087.cpp ;
7171
run github_issue_1091.cpp ;
7272
run github_issue_1094.cpp ;
73+
run github_issue_1110.cpp ;
7374
run github_issue_1112.cpp ;
7475

7576
run link_1.cpp link_2.cpp link_3.cpp ;

test/github_issue_1110.cpp

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Copyright 2025 Christopher Kormanyos
2+
// Distributed under the Boost Software License, Version 1.0.
3+
// https://www.boost.org/LICENSE_1_0.txt
4+
//
5+
// See: https://github.com/cppalliance/decimal/issues/1110
6+
7+
#include <boost/decimal/decimal128_t.hpp>
8+
#include <boost/decimal/cmath.hpp>
9+
#include <boost/decimal/iostream.hpp>
10+
11+
#include <boost/core/lightweight_test.hpp>
12+
13+
#include <iomanip>
14+
#include <iostream>
15+
#include <sstream>
16+
17+
using namespace boost::decimal;
18+
19+
namespace local {
20+
21+
auto test() -> void;
22+
23+
auto test() -> void
24+
{
25+
const boost::decimal::decimal128_t one { 1 };
26+
const boost::decimal::decimal128_t del { 1, -32 };
27+
const boost::decimal::decimal128_t sum { one + del };
28+
29+
const boost::decimal::decimal128_t sqr { sqrt(sum) };
30+
31+
{
32+
std::stringstream strm { };
33+
34+
strm << std::setprecision(std::numeric_limits<boost::decimal::decimal128_t>::digits10) << sqr;
35+
36+
BOOST_TEST_CSTR_EQ(strm.str().c_str(), "1.000000000000000000000000000000005");
37+
}
38+
39+
const boost::decimal::decimal128_t cbr { cbrt(sum) };
40+
41+
{
42+
std::stringstream strm { };
43+
44+
strm << std::setprecision(std::numeric_limits<boost::decimal::decimal128_t>::digits10)<< cbr;
45+
46+
// TODO: This tolerance isn't the best
47+
BOOST_TEST_CSTR_EQ(strm.str().c_str(), "1.000000000000000000000000000000641");
48+
}
49+
50+
const boost::decimal::decimal128_t lgt { log10(sum) };
51+
52+
{
53+
std::stringstream strm { };
54+
55+
strm << std::setprecision(std::numeric_limits<boost::decimal::decimal128_t>::digits10)<< lgt;
56+
57+
BOOST_TEST_CSTR_EQ(strm.str().c_str(), "4.4e-33");
58+
}
59+
}
60+
61+
} // namespace local
62+
63+
auto main() -> int
64+
{
65+
local::test();
66+
67+
return boost::report_errors();
68+
}

test/test_remove_trailing_zeros.cpp

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -39,30 +39,11 @@ void test()
3939
void test_extended()
4040
{
4141
using namespace boost::decimal;
42-
constexpr std::array<boost::int128::uint128_t, 18> powers_of_10 =
43-
{{
44-
boost::int128::uint128_t {UINT64_C(0x5), UINT64_C(0x6BC75E2D63100000)},
45-
boost::int128::uint128_t {UINT64_C(0x36), UINT64_C(0x35C9ADC5DEA00000)},
46-
boost::int128::uint128_t {UINT64_C(0x21E), UINT64_C(0x19E0C9BAB2400000)},
47-
boost::int128::uint128_t {UINT64_C(0x152D), UINT64_C(0x02C7E14AF6800000)},
48-
boost::int128::uint128_t {UINT64_C(0x84595), UINT64_C(0x161401484A000000)},
49-
boost::int128::uint128_t {UINT64_C(0x52B7D2), UINT64_C(0xDCC80CD2E4000000)},
50-
boost::int128::uint128_t {UINT64_C(0x33B2E3C), UINT64_C(0x9FD0803CE8000000)},
51-
boost::int128::uint128_t {UINT64_C(0x204FCE5E), UINT64_C(0x3E25026110000000)},
52-
boost::int128::uint128_t {UINT64_C(0x1431E0FAE), UINT64_C(0x6D7217CAA0000000)},
53-
boost::int128::uint128_t {UINT64_C(0xC9F2C9CD0), UINT64_C(0x4674EDEA40000000)},
54-
boost::int128::uint128_t {UINT64_C(0x7E37BE2022), UINT64_C(0xC0914B2680000000)},
55-
boost::int128::uint128_t {UINT64_C(0x4EE2D6D415B), UINT64_C(0x85ACEF8100000000)},
56-
boost::int128::uint128_t {UINT64_C(0x314DC6448D93), UINT64_C(0x38C15B0A00000000)},
57-
boost::int128::uint128_t {UINT64_C(0x1ED09BEAD87C0), UINT64_C(0x378D8E6400000000)},
58-
boost::int128::uint128_t {UINT64_C(0x13426172C74D82), UINT64_C(0x2B878FE800000000)},
59-
boost::int128::uint128_t {UINT64_C(0xC097CE7BC90715), UINT64_C(0xB34B9F1000000000)},
60-
boost::int128::uint128_t {UINT64_C(0x785EE10D5DA46D9), UINT64_C(0x00F436A000000000)},
61-
boost::int128::uint128_t {UINT64_C(0x4B3B4CA85A86C47A), UINT64_C(0x098A224000000000)}
62-
}};
42+
const auto powers_of_10 = detail::impl::BOOST_DECIMAL_DETAIL_INT128_pow10;
6343

64-
for (const auto& val : powers_of_10)
44+
for (std::size_t i {}; i < 39; ++i)
6545
{
46+
const auto val = powers_of_10[i];
6647
const auto temp {boost::decimal::detail::remove_trailing_zeros(val)};
6748
if (!BOOST_TEST_EQ(temp.trimmed_number, boost::int128::uint128_t(1)))
6849
{

0 commit comments

Comments
 (0)