Skip to content

Commit

Permalink
Avoid negative shift. (#42)
Browse files Browse the repository at this point in the history
* Avoid negative shift.

When filling in fractional digits in a fixed representation we
might use all existing digits. When this happens, we can not look
at the next digit to see if we should round up.

Before this fix, we tried to shift by a negative amount to see if the
(non-existing) next bit was set to 1 (requiring the number to be rounded up).

Fixes #41.
  • Loading branch information
floitschG authored and floitsch committed Mar 6, 2017
1 parent d8d4e66 commit 4abe326
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
3 changes: 2 additions & 1 deletion double-conversion/fixed-dtoa.cc
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,8 @@ static void FillFractionals(uint64_t fractionals, int exponent,
fractionals -= static_cast<uint64_t>(digit) << point;
}
// If the first bit after the point is set we have to round up.
if (((fractionals >> (point - 1)) & 1) == 1) {
ASSERT(fractionals == 0 || point - 1 >= 0);
if ((fractionals != 0) && ((fractionals >> (point - 1)) & 1) == 1) {
RoundUp(buffer, length, decimal_point);
}
} else { // We need 128 bits.
Expand Down
4 changes: 4 additions & 0 deletions test/cctest/test-fixed-dtoa.cc
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,10 @@ TEST(FastFixedVariousDoubles) {
buffer, &length, &point));
CHECK_EQ("1000000000000000128", buffer.start());
CHECK_EQ(19, point);

CHECK(FastFixedDtoa(2.10861548515811875e+15, 17, buffer, &length, &point));
CHECK_EQ("210861548515811875", buffer.start());
CHECK_EQ(16, point);
}


Expand Down

0 comments on commit 4abe326

Please sign in to comment.