diff --git a/py/formatfloat.c b/py/formatfloat.c index f7762b07dd..21ed2d5508 100644 --- a/py/formatfloat.c +++ b/py/formatfloat.c @@ -198,7 +198,7 @@ int mp_format_float(FPTYPE f, char *buf, size_t buf_size, char fmt, int prec, ch if (e == 0) { e_sign_char = '+'; } - } else { + } else if (fp_isless1(f)) { e++; f *= FPCONST(10.0); } @@ -250,6 +250,12 @@ int mp_format_float(FPTYPE f, char *buf, size_t buf_size, char fmt, int prec, ch } } + // It can be that f was right on the edge of an entry in pos_pow needs to be reduced + if (f >= FPCONST(10.0)) { + e += 1; + f *= FPCONST(0.1); + } + // If the user specified fixed format (fmt == 'f') and e makes the // number too big to fit into the available buffer, then we'll // switch to the 'e' format.