[sprintf] Wrong formatting of nan/inf with leading 0

Open Beginner friendly
#28 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
70/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Stale
Tech stack
c
Domain
tooling

Research direction

The bug is in the float formatting paths of the single-header sprintf implementation, at the doexpfromg: and dofloatfromg: labels. Build a small test program that prints NaN and infinity values with %f and %06f, and compare the output with the C standard library. Done means that NaN and infinity print in lowercase, without a leading zero pad, and that the sign is handled as the issue describes.

Written by the indexing model from the issue text.

Description

In a footnote, the standard says:

When applied to infinite and NaN values, the -, +, and space flag characters have their usual meaning; the # and 0 flag characters have no effect.

STB vs standard-conforming implementation (notice also the wrong upper/lower case, already reported):

printf("%f", STB_POS_QNAN);   // "NaN"    vs "nan"
printf("%f", STB_NEG_QNAN);   // "-NaN"   vs "-nan"
printf("%f", STB_POS_SNAN);   // "NaN"    vs "nan"
printf("%f", STB_NEG_SNAN);   // "-NaN"   vs "-nan"
printf("%f", 0/0.0);          // "-NaN"   vs "-nan" or "nan" (the sign of nans is not guaranteed)
printf("%f", +1/0.0);         // "Inf"    vs "inf"
printf("%f", -1/0.0);         // "-Inf"   vs "-inf"
printf("%06f", STB_POS_QNAN); // "000NaN" vs "   nan"
printf("%06f", STB_NEG_QNAN); // "-00NaN" vs "  -nan"
printf("%06f", STB_POS_SNAN); // "000NaN" vs "   nan"
printf("%06f", STB_NEG_SNAN); // "-00NaN" vs "  -nan"
printf("%06f", 0/0.0);        // "-00NaN" vs "  -nan"
printf("%06f", +1/0.0);       // "000Inf" vs "   inf"
printf("%06f", -1/0.0);       // "-00Inf" vs "  -inf"

Reproducible nans for testing can be generated this way:

double make_nan(int sign, int signaling)
{
    unsigned long long v = ((sign ? 1ull : 0ull) << 63)
        | (0x7FFull << 52)
        | (signaling ? 1ull : (1ull << 51))
        ;
    double d;
    memcpy(&d, &v, 8);
    return d;
}
#define STB_POS_QNAN    make_nan(0, 0)
#define STB_NEG_QNAN    make_nan(1, 0)
#define STB_POS_SNAN    make_nan(0, 1)
#define STB_NEG_SNAN    make_nan(1, 1)

Fix: in both the doexpfromg: and dofloatfromg: code paths, add this:

if (dp == STBSP__SPECIAL) {
   fl &= ~STBSP__LEADINGZERO;
   s = (char *)sn;
   cs = 0;
   pr = 0;
   goto scopy;
}
Dominant language
C
Stars
18
Forks
5
Avg merge
1m
Merged PRs (30d)
3

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from nothings/stb_sprintf

All issues in nothings/stb_sprintf

Similar issues

More C issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.