Fix 0 before decimal-point in hex float printf fns (#1297)

The C standard specifies that, upon handling the a conversion specifier,
the argument is converted to a string in which "there is one hexadecimal
digit (which is nonzero [...]) before the decimal-point character", this
being a requirement which cosmopolitan does not currently always handle,
sometimes printing numbers like "0x0.1p+5", where a correct output would
have been e.g. "0x1.0p+1" (despite both representing the same value, the
first one illegally has a '0' digit before the decimal-point character).
This commit is contained in:
Gabriel Ravier 2024-09-16 03:02:47 +02:00 committed by GitHub
parent 81bc8d0963
commit e260d90096
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 20 additions and 11 deletions

View file

@ -714,6 +714,7 @@ static int __fmt_bround(struct FPBits *b, int prec, int prec1) {
(b->fpi.rounding == FPI_Round_down && b->sign))
goto inc_true;
// Rounding to nearest, ties to even
if ((t = bits[k >> 3] >> (j = (k & 7) * 4)) & 8) {
if (t & 7)
goto inc_true;
@ -757,7 +758,12 @@ have_inc:
donothing;
if (j > k) {
onebit:
bits[0] = 1;
// We use 0x10 instead of 1 here to ensure that the digit before the
// decimal-point is non-0 (the C standard mandates this, i.e. considers
// that printing 0x0.1p+5 is illegal where 0x1.0p+1 is even though both
// evaluate to the same value because the first has 0 as the digit before
// the decimal-point character)
bits[0] = 0x10;
b->ex += 4 * prec;
return 1;
}