Skip to content

Commit b217869

Browse files
dhylandsdpgeorge
authored andcommitted
py/formatfloat.c: Fix format of floating point numbers near 1.0.
In particular, numbers which are less than 1.0 but which round up to 1.0. This also makes those numbers which round up to 1.0 to print with e+00 rather than e-00 for those formats which print exponents. Addresses issue adafruit#1178.
1 parent 8b7faa3 commit b217869

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

py/formatfloat.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,14 @@ int mp_format_float(float f, char *buf, size_t buf_size, char fmt, int prec, cha
142142
num.f *= *pos_pow;
143143
}
144144
}
145+
char first_dig = '0';
146+
char e_sign_char = '-';
145147
if (num.f < 1.0F && num.f >= 0.9999995F) {
146148
num.f = 1.0F;
149+
first_dig = '1';
150+
if (e == 0) {
151+
e_sign_char = '+';
152+
}
147153
} else {
148154
e++;
149155
num.f *= 10.0F;
@@ -155,7 +161,7 @@ int mp_format_float(float f, char *buf, size_t buf_size, char fmt, int prec, cha
155161
if (fmt == 'f' || (fmt == 'g' && e <= 4)) {
156162
fmt = 'f';
157163
dec = -1;
158-
*s++ = '0';
164+
*s++ = first_dig;
159165

160166
if (prec + e + 1 > buf_remaining) {
161167
prec = buf_remaining - e - 1;
@@ -175,7 +181,7 @@ int mp_format_float(float f, char *buf, size_t buf_size, char fmt, int prec, cha
175181
} else {
176182
// For e & g formats, we'll be printing the exponent, so set the
177183
// sign.
178-
e_sign = '-';
184+
e_sign = e_sign_char;
179185
dec = 0;
180186

181187
if (prec > (buf_remaining - 6)) {

0 commit comments

Comments
 (0)