summaryrefslogtreecommitdiffstatshomepage
path: root/py/formatfloat.c
diff options
context:
space:
mode:
Diffstat (limited to 'py/formatfloat.c')
-rw-r--r--py/formatfloat.c29
1 files changed, 17 insertions, 12 deletions
diff --git a/py/formatfloat.c b/py/formatfloat.c
index 9ff80d9f63..35cd5d51af 100644
--- a/py/formatfloat.c
+++ b/py/formatfloat.c
@@ -1,5 +1,5 @@
/*
- * This file is part of the Micro Python project, http://micropython.org/
+ * This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
@@ -118,7 +118,7 @@ int mp_format_float(FPTYPE f, char *buf, size_t buf_size, char fmt, int prec, ch
*s++ = '?';
}
if (buf_size >= 1) {
- *s++ = '\0';
+ *s = '\0';
}
return buf_size >= 2;
}
@@ -161,7 +161,7 @@ int mp_format_float(FPTYPE f, char *buf, size_t buf_size, char fmt, int prec, ch
if (fmt == 'g' && prec == 0) {
prec = 1;
}
- int e, e1;
+ int e, e1;
int dec = 0;
char e_sign = '\0';
int num_digits = 0;
@@ -209,7 +209,7 @@ int mp_format_float(FPTYPE f, char *buf, size_t buf_size, char fmt, int prec, ch
e_sign_char = '+';
}
} else if (fp_isless1(f)) {
- e++;
+ e++;
f *= FPCONST(10.0);
}
@@ -232,7 +232,7 @@ int mp_format_float(FPTYPE f, char *buf, size_t buf_size, char fmt, int prec, ch
num_digits = prec;
if (num_digits) {
- *s++ = '.';
+ *s++ = '.';
while (--e && num_digits) {
*s++ = '0';
num_digits--;
@@ -266,7 +266,7 @@ int mp_format_float(FPTYPE f, char *buf, size_t buf_size, char fmt, int prec, ch
f *= FPCONST(0.1);
}
- // If the user specified fixed format (fmt == 'f') and e makes the
+ // 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.
@@ -327,12 +327,12 @@ int mp_format_float(FPTYPE f, char *buf, size_t buf_size, char fmt, int prec, ch
if (prec == 0) {
prec = 1;
}
- num_digits = prec;
+ num_digits = prec;
}
// Print the digits of the mantissa
for (int i = 0; i < num_digits; ++i, --dec) {
- int32_t d = f;
+ int32_t d = (int32_t)f;
*s++ = '0' + d;
if (dec == 0 && prec > 0) {
*s++ = '.';
@@ -365,7 +365,7 @@ int mp_format_float(FPTYPE f, char *buf, size_t buf_size, char fmt, int prec, ch
if (rs == buf) {
break;
}
- rs--;
+ rs--;
}
if (*rs == '0') {
// We need to insert a 1
@@ -376,12 +376,17 @@ 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++;
+ e++;
}
+ } else {
+ // Need at extra digit at the end to make room for the leading '1'
+ s++;
}
- s++;
- char *ss = s;
+ char *ss = s;
while (ss > rs) {
*ss = ss[-1];
ss--;