aboutsummaryrefslogtreecommitdiff
path: root/py/formatfloat.c
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2018-03-01 17:00:02 +1100
committerDamien George <damien.p.george@gmail.com>2018-03-01 17:00:02 +1100
commit955ee6477f4b1d3a70bfe97a1e7727848bf2d06d (patch)
tree195d1942743e669f25c77874e1bf69bf49ab6781 /py/formatfloat.c
parent7b050fa76c6a763043739d40c82dde839d7f8fd9 (diff)
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.
Diffstat (limited to 'py/formatfloat.c')
-rw-r--r--py/formatfloat.c6
1 files changed, 5 insertions, 1 deletions
diff --git a/py/formatfloat.c b/py/formatfloat.c
index 60dcee6f5..dc7fc1d1f 100644
--- a/py/formatfloat.c
+++ b/py/formatfloat.c
@@ -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++ = '.';
}