Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion double-conversion/double-to-string.cc
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,12 @@ void DoubleToStringConverter::DoubleToAscii(double v,
return;
}

if (v == 0) {
// In SHORTEST_SINGLE mode the value is rendered as a single. A positive
// double below the smallest positive float rounds to +0.0f, which is a
// single zero even though the double is non-zero. Grisu3 would then take the
// boundaries from Single(0.0f), whose NormalizedBoundaries precondition
// (value > 0) is violated, and emit far more digits than the buffer holds.
if (v == 0 || (mode == SHORTEST_SINGLE && static_cast<float>(v) == 0.0f)) {
vector[0] = '0';
vector[1] = '\0';
*length = 1;
Expand Down
8 changes: 8 additions & 0 deletions test/cctest/test-dtoa.cc
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,14 @@ TEST(DtoaVariousDoubles) {
CHECK_EQ("0", buffer.start());
CHECK_EQ(1, point);

// A positive double below the smallest positive float rounds to +0.0f, so in
// single mode it is a zero. It must render as "0" and not spill past the
// buffer while generating digits at the double's magnitude.
DoubleToAscii(5e-324, SHORTEST_SINGLE, 0, buffer, &sign, &length, &point);
CHECK_EQ(1, length);
CHECK_EQ("0", buffer.start());
CHECK_EQ(1, point);

DoubleToAscii(0.0, FIXED, 2, buffer, &sign, &length, &point);
CHECK_EQ(1, length);
CHECK_EQ("0", buffer.start());
Expand Down
Loading