mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-05-25 06:42:27 +00:00
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:
parent
81bc8d0963
commit
e260d90096
2 changed files with 20 additions and 11 deletions
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue