Commit 6ed4581f authored by Damien George's avatar Damien George
Browse files

py/formatfloat: Fix number of digits and exponent sign when rounding.

This patch fixes 2 things when printing a floating-point number that
requires rounding up of the mantissa:
- retain the correct precision; eg 0.99 becomes 1.0, not 1.00
- if the exponent goes from -1 to 0 then render it as +0, not -0
parent d42b80fd
......@@ -376,11 +376,16 @@ int mp_format_float(FPTYPE f, char *buf, size_t buf_size, char fmt, int prec, ch
rs[1] = '0';
if (e_sign == '-') {
e--;
if (e == 0) {
e_sign = '+';
}
} else {
e++;
}
} else {
// Need at extra digit at the end to make room for the leading '1'
s++;
}
s++;
char *ss = s;
while (ss > rs) {
*ss = ss[-1];
......
......@@ -44,3 +44,6 @@ print(('%.40g' % 1e-3)[:2])
print(('%.40g' % 1e-4)[:2])
print("%.0g" % 1) # 0 precision 'g'
print('%.1e' % 9.99) # round up with positive exponent
print('%.1e' % 0.999) # round up with negative exponent
# uPy and CPython outputs differ for the following
print("%.1g" % -9.9) # round up 'g' with '-' sign
print("%.1e" % 9.99) # round up with positive exponent
print("%.1e" % 0.999) # round up with negative exponent
print("%.2g" % 99.9) # round up
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment