Skip to content

Commit 5017d7b

Browse files
authored
Fix precision padding for float specifiers (#274)
1 parent 43828b5 commit 5017d7b

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

nanoprintf.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -977,7 +977,10 @@ int npf_vpprintf(npf_putc pc, void *pc_ctx, char const *format, va_list args) {
977977
if (fs.conv_spec != NPF_FMT_SPEC_CONV_STRING) {
978978
#if NANOPRINTF_USE_FLOAT_FORMAT_SPECIFIERS == 1
979979
// float precision is after the decimal point
980-
if (fs.conv_spec != NPF_FMT_SPEC_CONV_FLOAT_DEC)
980+
if ((fs.conv_spec != NPF_FMT_SPEC_CONV_FLOAT_DEC) &&
981+
(fs.conv_spec != NPF_FMT_SPEC_CONV_FLOAT_SCI) &&
982+
(fs.conv_spec != NPF_FMT_SPEC_CONV_FLOAT_SHORTEST) &&
983+
(fs.conv_spec != NPF_FMT_SPEC_CONV_FLOAT_HEX))
981984
#endif
982985
{ prec_pad = npf_max(0, fs.prec - cbuf_len); }
983986
}

tests/conformance.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,10 @@ TEST_CASE("conformance to system printf") {
471471
require_conform(" inf", "%4f", (double)INFINITY);
472472
#endif // NANOPRINTF_USE_FIELD_WIDTH_FORMAT_SPECIFIERS
473473
require_conform("inf", "%.100f", (double)INFINITY);
474+
require_conform("inf", "%.10f", (double)INFINITY);
475+
require_conform("inf", "%.10e", (double)INFINITY);
476+
require_conform("inf", "%.10g", (double)INFINITY);
477+
require_conform("inf", "%.10a", (double)INFINITY);
474478
require_conform("INF", "%F", (double)INFINITY);
475479
require_conform("0.000000", "%f", 0.0);
476480
require_conform("0.00", "%.2f", 0.0);

0 commit comments

Comments
 (0)