Skip to content

Commit 993c2fc

Browse files
authored
Merge pull request #270 from boostorg/266
Fix fixed precision case where we are short zero to the right of the decimal point
2 parents 3bf1d22 + 4d9d950 commit 993c2fc

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

include/boost/charconv/detail/dragonbox/floff.hpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1333,6 +1333,7 @@ BOOST_CHARCONV_SAFEBUFFERS to_chars_result floff(const double x, int precision,
13331333

13341334
auto buffer_size = static_cast<std::size_t>(last - first);
13351335
auto buffer = first;
1336+
bool trailing_zeros_removed = false;
13361337

13371338
BOOST_CHARCONV_ASSERT(precision >= 0);
13381339
using namespace detail;
@@ -3849,6 +3850,7 @@ BOOST_CHARCONV_SAFEBUFFERS to_chars_result floff(const double x, int precision,
38493850
}
38503851

38513852
// Remove trailing zeros.
3853+
trailing_zeros_removed = true;
38523854
while (true)
38533855
{
38543856
auto prev = buffer - 1;
@@ -3903,7 +3905,16 @@ BOOST_CHARCONV_SAFEBUFFERS to_chars_result floff(const double x, int precision,
39033905
print_2_digits(static_cast<std::uint32_t>(decimal_exponent_normalized), buffer);
39043906
buffer += 2;
39053907
}
3906-
}
3908+
}
3909+
else if (!trailing_zeros_removed && buffer - (decimal_dot_pos + 1) < precision)
3910+
{
3911+
// If we have fixed precision, and we don't have enough digits after the decimal yet
3912+
// insert a sufficient amount of zeros
3913+
const auto remaining_zeros = precision - (buffer - (decimal_dot_pos + 1));
3914+
BOOST_CHARCONV_ASSERT(remaining_zeros > 0);
3915+
std::memset(buffer, '0', static_cast<std::size_t>(remaining_zeros));
3916+
buffer += remaining_zeros;
3917+
}
39073918

39083919
return {buffer, std::errc()};
39093920

test/Jamfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,5 @@ run github_issue_166.cpp ;
7070
run github_issue_166_float128.cpp ;
7171
run github_issue_186.cpp ;
7272
run github_issue_212.cpp ;
73+
run github_issue_266.cpp ;
7374
run github_issue_267.cpp ;

test/github_issue_266.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
// See: https://github.com/boostorg/charconv/issues/266
6+
7+
#include <boost/charconv.hpp>
8+
#include <boost/core/lightweight_test.hpp>
9+
10+
template <typename T>
11+
void test(T value, int precision, const char* correct_result)
12+
{
13+
char buffer[64] {};
14+
const auto r = boost::charconv::to_chars(buffer, buffer + sizeof(buffer), value, boost::charconv::chars_format::fixed, precision);
15+
BOOST_TEST_CSTR_EQ(buffer, correct_result);
16+
BOOST_TEST(r);
17+
}
18+
19+
int main()
20+
{
21+
test(123456789012345.6789012345678901, 5, "123456789012345.67188");
22+
test(1234567890123456.789012345678901, 5, "1234567890123456.75000");
23+
test(12345678901234567.89012345678901, 5, "12345678901234568.00000");
24+
test(123456789012345678.9012345678901, 5, "123456789012345680.00000");
25+
test(1234567890123456789.012345678901, 5, "1234567890123456768.00000");
26+
27+
test(123456789012345678901234567.8901, 5, "123456789012345678152597504.00000");
28+
test(12345678901234567890123456.78901, 5, "12345678901234568244756480.00000");
29+
test(1234567890123456789012345.678901, 5, "1234567890123456824475648.00000");
30+
test(123456789012345678901234.5678901, 5, "123456789012345685803008.00000");
31+
test(12345678901234567890123.45678901, 5, "12345678901234567741440.00000");
32+
test(123456789012345678901.2345678901, 5, "123456789012345683968.00000");
33+
34+
return boost::report_errors();
35+
}

0 commit comments

Comments
 (0)