summaryrefslogtreecommitdiff
path: root/py/formatfloat.c
diff options
context:
space:
mode:
authorhathach <thach@tinyusb.org>2018-08-01 00:58:23 +0700
committerhathach <thach@tinyusb.org>2018-08-01 00:58:23 +0700
commit1e524f1b9800b4d734e0c3db0fb6434176c4d7a8 (patch)
tree06a6ffe912cb65d1bb78974d35f6b9eba3800c2a /py/formatfloat.c
parent20c25f61d92cfde9f259ee06ee47915e7a3ea3c5 (diff)
parentbf033123a7fa75394fd7a01d17ec99afd9abe645 (diff)
Merge branch 'master' into nrf52840_usb_hid
Diffstat (limited to 'py/formatfloat.c')
-rw-r--r--py/formatfloat.c10
1 files changed, 7 insertions, 3 deletions
diff --git a/py/formatfloat.c b/py/formatfloat.c
index 4228f99ff..dc7fc1d1f 100644
--- a/py/formatfloat.c
+++ b/py/formatfloat.c
@@ -258,7 +258,7 @@ 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)) {
+ if ((int)f >= 10) {
e += 1;
f *= FPCONST(0.1);
}
@@ -330,7 +330,11 @@ int mp_format_float(FPTYPE f, char *buf, size_t buf_size, char fmt, int prec, ch
// Print the digits of the mantissa
for (int i = 0; i < num_digits; ++i, --dec) {
int32_t d = (int32_t)f;
- *s++ = '0' + d;
+ if (d < 0) {
+ *s++ = '0';
+ } else {
+ *s++ = '0' + d;
+ }
if (dec == 0 && prec > 0) {
*s++ = '.';
}
@@ -341,7 +345,7 @@ int mp_format_float(FPTYPE f, char *buf, size_t buf_size, char fmt, int prec, ch
// Round
// If we print non-exponential format (i.e. 'f'), but a digit we're going
// to round by (e) is too far away, then there's nothing to round.
- if ((org_fmt != 'f' || e <= 1) && f >= FPCONST(5.0)) {
+ if ((org_fmt != 'f' || e <= num_digits) && f >= FPCONST(5.0)) {
char *rs = s;
rs--;
while (1) {