Skip to content

Commit

Permalink
printf: Fix a bug in float printing
Browse files Browse the repository at this point in the history
  • Loading branch information
Qwinci authored and qookei committed Sep 17, 2024
1 parent fffd8b9 commit 736c716
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
7 changes: 1 addition & 6 deletions include/frg/formatting.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -299,19 +299,14 @@ namespace _fmt_basics {
n = static_cast<uint64_t>(number);
number -= n;
int i = 0;
while (n > 0 && i < precision) {
while (i < precision) {
sink.append('0' + n);
number *= 10;
n = static_cast<uint64_t>(number);
number -= n;
i++;
}

while (i < precision) {
sink.append('0');
i++;
}

if (left_justify) {
while (pad_length > 0) {
sink.append(padding);
Expand Down
7 changes: 7 additions & 0 deletions tests/tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,9 @@ TEST(formatting, printf) {
case 'd': case 'i': case 'o': case 'x': case 'X': case 'b': case 'B': case 'u':
frg::do_printf_ints(*sink_, t, opts, szmod, vsp_);
break;
case 'f':
frg::do_printf_floats(*sink_, t, opts, szmod, vsp_);
break;
default:
// Should not be reached
ADD_FAILURE();
Expand Down Expand Up @@ -366,6 +369,10 @@ TEST(formatting, printf) {
do_test("0", "%#X", 0);
do_test("0", "%#o", 0);

// Test 'f'.
do_test("1.100000", "%f", 1.1);
do_test("0.01", "%.2f", 0.01234);

// Test 'd' with different size mods to see
// if they work
do_test("12", "%d", 12);
Expand Down

0 comments on commit 736c716

Please sign in to comment.