From 87b96f505ec335307c71ca82e626e84a3c381832 Mon Sep 17 00:00:00 2001 From: Okarss <104319900+Okarss@users.noreply.github.com> Date: Sat, 31 Aug 2024 13:09:21 +0300 Subject: [PATCH] Fix precision padding for float specifiers --- nanoprintf.h | 5 ++++- tests/conformance.cc | 4 ++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/nanoprintf.h b/nanoprintf.h index 95d9fd5..b6fff5c 100644 --- a/nanoprintf.h +++ b/nanoprintf.h @@ -977,7 +977,10 @@ int npf_vpprintf(npf_putc pc, void *pc_ctx, char const *format, va_list args) { if (fs.conv_spec != NPF_FMT_SPEC_CONV_STRING) { #if NANOPRINTF_USE_FLOAT_FORMAT_SPECIFIERS == 1 // float precision is after the decimal point - if (fs.conv_spec != NPF_FMT_SPEC_CONV_FLOAT_DEC) + if ((fs.conv_spec != NPF_FMT_SPEC_CONV_FLOAT_DEC) && + (fs.conv_spec != NPF_FMT_SPEC_CONV_FLOAT_SCI) && + (fs.conv_spec != NPF_FMT_SPEC_CONV_FLOAT_SHORTEST) && + (fs.conv_spec != NPF_FMT_SPEC_CONV_FLOAT_HEX)) #endif { prec_pad = npf_max(0, fs.prec - cbuf_len); } } diff --git a/tests/conformance.cc b/tests/conformance.cc index 35e9188..34b87f6 100644 --- a/tests/conformance.cc +++ b/tests/conformance.cc @@ -471,6 +471,10 @@ TEST_CASE("conformance to system printf") { require_conform(" inf", "%4f", (double)INFINITY); #endif // NANOPRINTF_USE_FIELD_WIDTH_FORMAT_SPECIFIERS require_conform("inf", "%.100f", (double)INFINITY); + require_conform("inf", "%.10f", (double)INFINITY); + require_conform("inf", "%.10e", (double)INFINITY); + require_conform("inf", "%.10g", (double)INFINITY); + require_conform("inf", "%.10a", (double)INFINITY); require_conform("INF", "%F", (double)INFINITY); require_conform("0.000000", "%f", 0.0); require_conform("0.00", "%.2f", 0.0);