From: gloit042 Date: Sun, 20 Dec 2020 16:35:21 +0000 (+0800) Subject: num2str: fix precision loss bug when the fractional part is close to 1 X-Git-Tag: fio-3.26~66^2 X-Git-Url: https://git.kernel.dk/?a=commitdiff_plain;h=c736b361190e1133b4d407a6c7615bf8c4d9c262;p=fio.git num2str: fix precision loss bug when the fractional part is close to 1 example: The result of num2str(11999999999999, 4, 1, 0, N2S_NONE) should be "12.0G", but current result is "11.0G". Signed-off-by: Jiahao Li --- diff --git a/lib/num2str.c b/lib/num2str.c index 726f1c44..423d97b2 100644 --- a/lib/num2str.c +++ b/lib/num2str.c @@ -112,6 +112,9 @@ done: sprintf(tmp, "%.*f", (int)(maxlen - strlen(tmp) - 1), (double)modulo / (double)thousand); + if (tmp[0] == '1') + num++; + if (asprintf(&buf, "%llu.%s%s%s", (unsigned long long) num, &tmp[2], unitprefix[post_index], unitstr[units]) < 0) buf = NULL;