Skip to content

Commit 03b8bb7

Browse files
committed
py/formatfloat: Fix case of float format where leading digit was "10".
When taking the logarithm of the float to determine the exponent, there are some edge cases that finish the log loop too large. Eg for an input value of 1e32-epsilon, this is actually less than 1e32 from the log-loop table and finishes as 10.0e31 when it should be 1.0e32. It is thus rendered as :e32 (: comes after 9 in ascii). There was the same problem with numbers less than 1.
1 parent d88250c commit 03b8bb7

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

py/formatfloat.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ int mp_format_float(FPTYPE f, char *buf, size_t buf_size, char fmt, int prec, ch
198198
if (e == 0) {
199199
e_sign_char = '+';
200200
}
201-
} else {
201+
} else if (fp_isless1(f)) {
202202
e++;
203203
f *= FPCONST(10.0);
204204
}
@@ -250,6 +250,12 @@ int mp_format_float(FPTYPE f, char *buf, size_t buf_size, char fmt, int prec, ch
250250
}
251251
}
252252

253+
// It can be that f was right on the edge of an entry in pos_pow needs to be reduced
254+
if (f >= FPCONST(10.0)) {
255+
e += 1;
256+
f *= FPCONST(0.1);
257+
}
258+
253259
// If the user specified fixed format (fmt == 'f') and e makes the
254260
// number too big to fit into the available buffer, then we'll
255261
// switch to the 'e' format.

0 commit comments

Comments
 (0)