Skip to content

Commit 3d6240b

Browse files
committed
py/formatfloat: Handle calculation of integer digit for %f format properly.
%f prints true integer digit, so its calculation should happen before any exponential scaling.
1 parent 3c4c069 commit 3d6240b

1 file changed

Lines changed: 9 additions & 5 deletions

File tree

py/formatfloat.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -170,21 +170,25 @@ int mp_format_float(FPTYPE f, char *buf, size_t buf_size, char fmt, int prec, ch
170170
num_digits = prec + 1;
171171
}
172172
} else if (fp_isless1(f)) {
173+
// We need to figure out what an integer digit will be used
174+
// in case 'f' is used (or we revert other format to it below).
175+
// As we just tested number to be <1, this is obviously 0,
176+
// but we can round it up to 1 below.
177+
char first_dig = '0';
178+
if (f >= FPROUND_TO_ONE) {
179+
first_dig = '1';
180+
}
181+
173182
// Build negative exponent
174183
for (e = 0, e1 = FPDECEXP; e1; e1 >>= 1, pos_pow++, neg_pow++) {
175184
if (*neg_pow > f) {
176185
e += e1;
177186
f *= *pos_pow;
178187
}
179188
}
180-
char first_dig = '0';
181189
char e_sign_char = '-';
182190
if (fp_isless1(f) && f >= FPROUND_TO_ONE) {
183191
f = FPCONST(1.0);
184-
if (e > 1) {
185-
// numbers less than 1.0 start with 0.xxx
186-
first_dig = '1';
187-
}
188192
if (e == 0) {
189193
e_sign_char = '+';
190194
}

0 commit comments

Comments
 (0)