Skip to content

Commit

Permalink
Fixes mpaland#120 (and thus fixes #24): When printing am "%f" form wi…
Browse files Browse the repository at this point in the history
…thin an "%e" printing, we now check whether the rounding of the fractional part according to the precision limits breaks the "%e" constraint of a single-digit integral part.
  • Loading branch information
eyalroz committed Aug 4, 2021
1 parent 74c3662 commit 42aa0e2
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion printf.c
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,17 @@ static size_t sprint_exponential_number(out_fct_type out, char* buffer, size_t i

// output the floating part
const size_t start_idx = idx;
idx = sprint_decimal_number(out, buffer, idx, maxlen, negative ? -abs_number : abs_number, precision, fwidth, flags, buf, len);
struct double_components number_ = get_components(negative ? -abs_number : abs_number, precision);
// For "%e" notation, the integral part must be between 1 and 9; but the rounding can potentially
// bring it up from, say, 9.999something to 10 - in which case we must "steal" this extra 10 in
// favor of the exponent
if (!(flags & FLAGS_ADAPT_EXP) && number_.integral >= 10) {
number_.integral = 1;
number_.fractional = 0;
exp10++;
}
// TODO: Do we need to check for number_.integral being 0?
idx = sprint_broken_up_decimal(number_, out, buffer, idx, maxlen, precision, fwidth, flags, buf, len);

// output the exp10 part
if (minwidth) {
Expand Down

0 comments on commit 42aa0e2

Please sign in to comment.