Skip to content

Commit 7b050fa

Browse files
committed
py/formatfloat: Fix case where floats could render with a ":" character.
Prior to this patch, some architectures (eg unix x86) could render floats with a ":" character in them, eg 1e+39 would come out as ":e+38" (":" is just after "9" in ASCII so this is like 10e+38). This patch fixes some of these cases.
1 parent bc12eca commit 7b050fa

2 files changed

Lines changed: 5 additions & 1 deletion

File tree

py/formatfloat.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ int mp_format_float(FPTYPE f, char *buf, size_t buf_size, char fmt, int prec, ch
258258
}
259259

260260
// It can be that f was right on the edge of an entry in pos_pow needs to be reduced
261-
if (f >= FPCONST(10.0)) {
261+
if ((int)f >= 10) {
262262
e += 1;
263263
f *= FPCONST(0.1);
264264
}

tests/float/float_format.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,7 @@
99
# make sure rounding is done at the correct precision
1010
for prec in range(8):
1111
print(('%%.%df' % prec) % 6e-5)
12+
13+
# check certain cases that had a digit value of 10 render as a ":" character
14+
print('%.2e' % float('9' * 51 + 'e-39'))
15+
print('%.2e' % float('9' * 40 + 'e-21'))

0 commit comments

Comments
 (0)