From 5f8ca6e30334b81bd39a67f87a011cee8f282c3c Mon Sep 17 00:00:00 2001 From: Nikias Bassen Date: Mon, 21 Jan 2019 01:03:44 +0100 Subject: xplist: Fix dtostr() inserting ':' to output string when fractional part overflows --- src/xplist.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/xplist.c b/src/xplist.c index c89b143..fc8fd61 100644 --- a/src/xplist.c +++ b/src/xplist.c @@ -107,9 +107,22 @@ static size_t dtostr(char *buf, size_t bufsize, double realval) int64_t v; size_t len; size_t p; + double CORR = 0.0000005; f = modf(f, &ip); - len = snprintf(buf, bufsize, "%s%"PRIi64, ((f < 0) && (ip >= 0)) ? "-" : "", (int64_t)ip); + v = (int64_t)ip; + if (f < 0) { + if (((int)((f - CORR) * -10.0f)) >= 10) { + v--; + f = 0; + } + } else { + if (((int)((f + CORR) * 10.0f)) >= 10) { + v++; + f = 0; + } + } + len = snprintf(buf, bufsize, "%s%"PRIi64, ((f < 0) && (ip >= 0)) ? "-" : "", v); if (len >= bufsize) { return 0; } @@ -117,7 +130,7 @@ static size_t dtostr(char *buf, size_t bufsize, double realval) if (f < 0) { f *= -1; } - f += 0.0000004; + f += CORR; p = len; buf[p++] = '.'; -- cgit v1.1-32-gdbae