[sprintf] Wrong formatting of nan/inf with %a %A

Open
#27 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
58/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Stale
Tech stack
c
Domain
tooling

Research direction

The bug is in the case 'a' path of stb_sprintf.h, just after stbsp__real_to_parts() converts the double, so start there. The issue gives a suggested check on the unbiased exponent that routes NaN and infinity to the same output as %f. Build a small program using the make_nan macros and the 0/0.0, +1/0.0 and -1/0.0 cases, and compare stb's output with the C library's printf. Done means %a and %A print nan and inf with the sign and width behaviour the standard requires, including the padded cases.

Written by the indexing model from the issue text.

Description

Regarding %a %A, the standard says:

A double argument representing an infinity or NaN is converted in the style of an f or F
conversion specifier.

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

printf("%a", STB_POS_QNAN);    // "0x1.800000p+1024"     vs "nan"
printf("%a", STB_NEG_QNAN);    // "-0x1.800000p+1024"    vs "-nan"
printf("%a", STB_POS_SNAN);    // "0x1.000000p+1024""    vs "nan"
printf("%a", STB_NEG_SNAN);    // "-0x1.000000p+1024"    vs "-nan"
printf("%a", 0/0.0);           // "-0x1.800000p+1024"    vs "-nan"
printf("%a", +1/0.0);          // "0x1.000000p+1024"     vs "inf"
printf("%a", -1/0.0);          // "-0x1.000000p+1024"    vs "-inf"

printf("%020a", STB_POS_QNAN); // "0x00001.800000p+1024" vs "                 nan"
printf("%020a", STB_NEG_QNAN); // "-0x0001.800000p+1024" vs "                -nan"
printf("%020a", STB_POS_SNAN); // "0x00001.000000p+1024" vs "                 nan"
printf("%020a", STB_NEG_SNAN); // "-0x0001.000000p+1024" vs "                -nan"
printf("%020a", 0/0.0);        // "-0x0001.800000p+1024" vs "                -nan"
printf("%020a", +1/0.0);       // "0x00001.000000p+1024" vs "                 inf"
printf("%020a", -1/0.0);       // "-0x0001.000000p+1024" vs "                -inf"

printf("%20a", STB_POS_QNAN);  // "    0x1.800000p+1024" vs "                 nan"
printf("%20a", STB_NEG_QNAN);  // "   -0x1.800000p+1024" vs "                -nan"
printf("%20a", STB_POS_SNAN);  // "    0x1.000000p+1024" vs "                 nan"
printf("%20a", STB_NEG_SNAN);  // "   -0x1.000000p+1024" vs "                -nan"
printf("%20a", 0/0.0);         // "   -0x1.800000p+1024" vs "                -nan"
printf("%20a", +1/0.0);        // "    0x1.000000p+1024" vs "                 inf"
printf("%20a", -1/0.0);        // "   -0x1.000000p+1024" vs "                -inf"

Reproducible nans were 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 the case 'a' code path, right after the conversion using stbsp__real_to_parts(), add the code below.
This is around here:
https://github.com/nothings/stb/blob/f0569113c93ad095470c54bf34a17b36646bbbb5/stb_sprintf.h#L639-L651

The code to add:

   if (dp == 2047 - 1023) { // dp is the unbiased exponent; this checks if the number is nan or inf
      // read the double into a string
      if (stbsp__real_to_str(&sn, &ln, num, &dp, fv, 0, (f[0] == 'A')))
         fl |= STBSP__NEGATIVE;
      goto dofloatfromg; // nan/inf are output just like for %f
   }
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.