fmt icon indicating copy to clipboard operation
fmt copied to clipboard

Hexfloat ('a'/'A') zero padding is placed before the sign and "0x" prefix

Open davidscottpope-gif opened this issue 2 months ago • 0 comments

With the 'a'/'A' presentation and the '0' flag, {fmt} puts the zero padding before the sign and the "0x" prefix:

fmt::format("{:012a}", 1.0) -> 0000000x1p+0 fmt::format("{:+012a}", 1.5) -> +0000x1.8p+0 fmt::format("{:012a}", -1.5) -> -0000x1.8p+0 printf("%012a", 1.0) -> 0x0000001p+0

Expected: 0x0000001p+0, +0x0001.8p+0, -0x0001.8p+0. C11 7.21.6.1 para 6 says leading zeros follow any sign or base indication, and std::format does the same. The integer path already does this right: fmt::format("{:#012x}", 0x1abc) gives 0x0000001abc. fmt::sprintf("%012a", ...) is affected too.

It looks like the hexfloat branch of the float write() in format.h builds sign + "0x" + digits and emits it with write_bytes at forced align::right, so the fill lands in front of the whole string.

fmt main @ 174569c, g++ 11.4.

I used AI tools to find and reproduce this; I reviewed it myself.

davidscottpope-gif avatar Oct 04 '26 19:10 davidscottpope-gif