Skip to content

Commit 955ee64

Browse files
committed
py/formatfloat: Fix case where floats could render with negative digits.
Prior to this patch, some architectures (eg unix x86) could render floats with "negative" digits, like ")". For example, '%.23e' % 1e-80 would come out as "1.0000000000000000/)/(,*0e-80". This patch fixes the known cases.
1 parent 7b050fa commit 955ee64

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

py/formatfloat.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,11 @@ int mp_format_float(FPTYPE f, char *buf, size_t buf_size, char fmt, int prec, ch
330330
// Print the digits of the mantissa
331331
for (int i = 0; i < num_digits; ++i, --dec) {
332332
int32_t d = (int32_t)f;
333-
*s++ = '0' + d;
333+
if (d < 0) {
334+
*s++ = '0';
335+
} else {
336+
*s++ = '0' + d;
337+
}
334338
if (dec == 0 && prec > 0) {
335339
*s++ = '.';
336340
}

tests/float/float_format.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,7 @@
1313
# check certain cases that had a digit value of 10 render as a ":" character
1414
print('%.2e' % float('9' * 51 + 'e-39'))
1515
print('%.2e' % float('9' * 40 + 'e-21'))
16+
17+
# check a case that would render negative digit values, eg ")" characters
18+
# the string is converted back to a float to check for no illegal characters
19+
float('%.23e' % 1e-80)

0 commit comments

Comments
 (0)