浏览代码

fix printf rounding with %g for some corner case midpoints

the subsequent rounding code assumes the end pointer (z) accurately
reflects the end of significance in the decimal expansion, but for
certain large integers, spurious trailing zero slots were left behind
when applying the binary exponent.

issue reported by Morten Welinder; the analysis of the cause was
performed by nsz, who also proposed this change.

(cherry picked from commit e94d0692864ecf9522fd6a97610a47a2f718d3de)
Rich Felker 11 年之前
父节点
当前提交
a65824a74f
共有 1 个文件被更改,包括 1 次插入1 次删除
  1. 1 1
      src/stdio/vfprintf.c

+ 1 - 1
src/stdio/vfprintf.c

@@ -308,8 +308,8 @@ static int fmt_fp(FILE *f, long double y, int w, int p, int fl, int t)
 			*d = x % 1000000000;
 			carry = x / 1000000000;
 		}
-		if (!z[-1] && z>a) z--;
 		if (carry) *--a = carry;
+		while (z>a && !z[-1]) z--;
 		e2-=sh;
 	}
 	while (e2<0) {