Skip to content

Commit

Permalink
fix(printf): remove float comparison
Browse files Browse the repository at this point in the history
Add more float test cases
  • Loading branch information
mpaland committed Jan 31, 2019
1 parent e9375ed commit 369b7bb
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
6 changes: 1 addition & 5 deletions printf.c
Original file line number Diff line number Diff line change
Expand Up @@ -363,11 +363,7 @@ static size_t _ftoa(out_fct_type out, char* buffer, size_t idx, size_t maxlen, d

if (prec == 0U) {
diff = value - (double)whole;
if (diff > 0.5) {
// greater than 0.5, round up, e.g. 1.6 -> 2
++whole;
}
else if ((diff == 0.5) && (whole & 1)) {
if ((!(diff < 0.5) || (diff > 0.5)) && (whole & 1)) {
// exactly 0.5 and ODD, then round up
// 1.5 -> 2, but 2.5 -> 2
++whole;
Expand Down
12 changes: 12 additions & 0 deletions test/test_suite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1036,6 +1036,15 @@ TEST_CASE("float", "[]" ) {
test::sprintf(buffer, "%.0f", 34.1415354);
REQUIRE(!strcmp(buffer, "34"));

test::sprintf(buffer, "%.0f", 1.3);
REQUIRE(!strcmp(buffer, "1"));

test::sprintf(buffer, "%.0f", 1.55);
REQUIRE(!strcmp(buffer, "2"));

test::sprintf(buffer, "%.1f", 1.64);
REQUIRE(!strcmp(buffer, "1.6"));

test::sprintf(buffer, "%.2f", 42.8952);
REQUIRE(!strcmp(buffer, "42.90"));

Expand Down Expand Up @@ -1082,6 +1091,9 @@ TEST_CASE("float", "[]" ) {
test::sprintf(buffer, "%.0f", 3.5);
REQUIRE(!strcmp(buffer, "4"));

test::sprintf(buffer, "%.0f", 4.5);
REQUIRE(!strcmp(buffer, "4"));

test::sprintf(buffer, "%.0f", 3.49);
REQUIRE(!strcmp(buffer, "3"));

Expand Down

0 comments on commit 369b7bb

Please sign in to comment.