Skip to content

Commit 0e3d9cf

Browse files
authored
Merge pull request #164 from boostorg/158.2
2 parents bef2775 + 3293512 commit 0e3d9cf

File tree

1 file changed

+16
-29
lines changed
  • include/boost/charconv/detail/dragonbox

1 file changed

+16
-29
lines changed

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

+16-29
Original file line numberDiff line numberDiff line change
@@ -1782,10 +1782,19 @@ BOOST_CHARCONV_SAFEBUFFERS to_chars_result floff(const double x, const int preci
17821782
}
17831783

17841784
const auto initial_digits = static_cast<std::uint32_t>(prod >> 32);
1785-
decimal_exponent += (11 - (initial_digits < 10 ? 1 : 0) + remaining_digits_in_the_current_subsegment);
1785+
const auto exp_adjustment = 11 - (initial_digits < 10 ? 1 : 0);
1786+
decimal_exponent += exp_adjustment + remaining_digits_in_the_current_subsegment;
1787+
if (fmt == chars_format::fixed && decimal_exponent < 0)
1788+
{
1789+
remaining_digits += decimal_exponent;
1790+
remaining_digits -= initial_digits < 10 ? 0 : 1;
1791+
}
1792+
else
1793+
{
1794+
remaining_digits -= (2 - (initial_digits < 10 ? 1 : 0));
1795+
}
17861796

17871797
buffer -= (initial_digits < 10 ? 1 : 0);
1788-
remaining_digits -= (2 - (initial_digits < 10 ? 1 : 0));
17891798
print_2_digits(initial_digits, buffer);
17901799
buffer += 2;
17911800

@@ -3841,34 +3850,12 @@ BOOST_CHARCONV_SAFEBUFFERS to_chars_result floff(const double x, const int preci
38413850
// Need to memset leading zeros equal to a negative exponent
38423851
if (decimal_exponent < 0)
38433852
{
3844-
std::memmove(&buffer_starting_pos[1 - decimal_exponent], &buffer_starting_pos[1],
3845-
static_cast<std::size_t>(buffer - buffer_starting_pos + decimal_exponent + 1));
3846-
std::memset(&buffer_starting_pos[0], '0', static_cast<std::size_t>(-decimal_exponent) + 1U);
3853+
const auto offset = static_cast<std::size_t>(buffer - buffer_starting_pos);
3854+
std::size_t additional_zeros = static_cast<std::size_t>(-decimal_exponent) + 1U;
3855+
std::memmove(&buffer_starting_pos[1 - decimal_exponent], &buffer_starting_pos[1], offset);
3856+
std::memset(&buffer_starting_pos[0], '0', additional_zeros);
38473857
buffer_starting_pos[1] = '.';
3848-
3849-
char* const buffer_end = buffer + 1;
3850-
3851-
// Fix the rounding since we are truncating
3852-
if (*(buffer + 1) >= '5' && *(buffer + 1) <= '9')
3853-
{
3854-
if (*buffer != '9')
3855-
{
3856-
*buffer += static_cast<char>(1);
3857-
--buffer;
3858-
}
3859-
else
3860-
{
3861-
*buffer = '0';
3862-
--buffer;
3863-
while (*buffer == '9')
3864-
{
3865-
*buffer-- = 0;
3866-
}
3867-
*buffer += static_cast<char>(1);
3868-
}
3869-
}
3870-
3871-
return {buffer_end, std::errc()};
3858+
return {first + precision + 3, std::errc()};
38723859
}
38733860

38743861
// In the positive case the precision is still measured after the decimal point

0 commit comments

Comments
 (0)