summaryrefslogtreecommitdiffstatshomepage
path: root/py
diff options
context:
space:
mode:
authorPaul Sokolovsky <pfalcon@users.sourceforge.net>2015-11-22 20:02:16 +0200
committerPaul Sokolovsky <pfalcon@users.sourceforge.net>2015-11-22 20:05:08 +0200
commit3d6240ba1b4f6f33a27b9eed7e51286f95d4cbe9 (patch)
treebb93f0b546a853a6e08c9e9c7fd5aa9a6353cebd /py
parent3c4c0698021a9eed5ab66b1c47c0180dd1fabcf4 (diff)
downloadmicropython-3d6240ba1b4f6f33a27b9eed7e51286f95d4cbe9.tar.gz
micropython-3d6240ba1b4f6f33a27b9eed7e51286f95d4cbe9.zip
py/formatfloat: Handle calculation of integer digit for %f format properly.
%f prints true integer digit, so its calculation should happen before any exponential scaling.
Diffstat (limited to 'py')
-rw-r--r--py/formatfloat.c14
1 files changed, 9 insertions, 5 deletions
diff --git a/py/formatfloat.c b/py/formatfloat.c
index ec90906527..78e38831c4 100644
--- a/py/formatfloat.c
+++ b/py/formatfloat.c
@@ -170,6 +170,15 @@ int mp_format_float(FPTYPE f, char *buf, size_t buf_size, char fmt, int prec, ch
num_digits = prec + 1;
}
} else if (fp_isless1(f)) {
+ // We need to figure out what an integer digit will be used
+ // in case 'f' is used (or we revert other format to it below).
+ // As we just tested number to be <1, this is obviously 0,
+ // but we can round it up to 1 below.
+ char first_dig = '0';
+ if (f >= FPROUND_TO_ONE) {
+ first_dig = '1';
+ }
+
// Build negative exponent
for (e = 0, e1 = FPDECEXP; e1; e1 >>= 1, pos_pow++, neg_pow++) {
if (*neg_pow > f) {
@@ -177,14 +186,9 @@ int mp_format_float(FPTYPE f, char *buf, size_t buf_size, char fmt, int prec, ch
f *= *pos_pow;
}
}
- char first_dig = '0';
char e_sign_char = '-';
if (fp_isless1(f) && f >= FPROUND_TO_ONE) {
f = FPCONST(1.0);
- if (e > 1) {
- // numbers less than 1.0 start with 0.xxx
- first_dig = '1';
- }
if (e == 0) {
e_sign_char = '+';
}