Hi,
Kevin Ushey wrote:
> On native Windows with the UCRT (mingw-w64, GCC 14.2), in a testdir
> created with
>
> gnulib-tool --create-testdir --dir=testdir \
> vasnprintf-posix vasnwprintf-posix
>
> at commit eedf47b2d43288f528db62ccd1cd2ce55eda92d4, where configure
> defines NEED_PRINTF_DOUBLE, vasnprintf prints the exponent of a double
> with three digits, and that of a long double with two:
>
> vasnprintf (NULL, &len, "%e", 1e-5) => "1.000000e-005"
> vasnprintf (NULL, &len, "%g", 1e-5) => "1e-005"
> vasnprintf (NULL, &len, "%Le", 1e-5L) => "1.000000e-05"
>
> The UCRT's own printf and mingw-w64's both give "1.000000e-05" and
> "1e-05" for the first two.
Thanks for the report. I did not know that this part of Windows behaviour
had changed from MSVCRT to UCRT.
> I understand this is deliberate: lib/vasnprintf.c uses "%+.3d" when
> _WIN32 is defined and __CYGWIN__ is not, to "produce the same number
> of exponent digits as the native printf implementation", and
> tests/test-vasnprintf-posix2.c and tests/test-vasnwprintf-posix2.c
> expect "3,1416e+009" there ("Native Windows uses 3 digits for the
> exponent"). That holds for msvcrt.dll, but not for the UCRT, which
> prints two digits. The code for long double uses "%+.2d"
> unconditionally.
Yes, you have interpreted the intention behind the code correctly.
> Would it make sense to exclude the UCRT there? I tried adding
> "&& ! defined _UCRT" to the five such conditions in the code for
> double (four choose the format string, one adds a third "0" to the
> exponent of a zero). The output then has two digits throughout, and
> "make check" in the testdir fails only in the two tests named above,
> at the assertions which expect three digits
> (test-vasnprintf-posix2.c:365, test-vasnwprintf-posix2.c:299); they
> would need the same condition.
Yes, it makes sense, except that the actual condition is more complicated.
Done as follows:
2026-12-03 Bruno Haible <[email protected]>
vasnprintf-posix: Use 3 exponent digits only if the native printf does.
Reported by Kevin Ushey <[email protected]> in
<https://lists.gnu.org/archive/html/bug-gnulib/2026-12/msg00018.html>.
* lib/vasnprintf.c (THREE_EXPONENT_DIGITS): New macro.
(VASNPRINTF): Use it.
* tests/test-vasnprintf-posix2.c (main): On native Windows, allow either
3 or 2 digits for the exponent.
* tests/test-vasnwprintf-posix2.c (main): Likewise.
diff --git a/lib/vasnprintf.c b/lib/vasnprintf.c
index 0a2854ec8d..0fe3490a22 100644
--- a/lib/vasnprintf.c
+++ b/lib/vasnprintf.c
@@ -648,6 +648,20 @@ num_thousands_separators (const signed char *grouping,
size_t ndigits)
# endif
#endif
+/* Whether the native printf has a minimum of 3 exponent digits.
+ (Most implementations have a minimum of 2 exponent digits.)
+ This is the case only on native Windows:
+ - With mingw __USE_MINGW_ANSI_STDIO=0 or MSVC it depends on the runtime
+ library: MSVCRT -> 3 digits, UCRT -> 2 digits.
+ - With mingw __USE_MINGW_ANSI_STDIO=1, it was changed on 2018-05-06:
+ mingw < 6.0.0 -> 3 digits, mingw >= 6.0.0 -> 2 digits. */
+#if defined _WIN32 && !defined __CYGWIN__ \
+ && (defined __MINGW32__ && __USE_MINGW_ANSI_STDIO \
+ ? __MINGW64_VERSION_MAJOR < 6 \
+ : !defined _UCRT)
+# define THREE_EXPONENT_DIGITS 1
+#endif
+
#if NEED_PRINTF_INFINITE_DOUBLE && !NEED_PRINTF_DOUBLE
/* Equivalent to !isfinite(x) || x == 0, but does not require libm. */
@@ -5913,7 +5927,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
static const wchar_t decimal_format[] =
/* Produce the same number of exponent
digits
as the native printf implementation. */
-# if defined _WIN32 && ! defined __CYGWIN__
+# if THREE_EXPONENT_DIGITS
{ '%', '+', '.', '3', 'd', '\0' };
# else
{ '%', '+', '.', '2', 'd', '\0' };
@@ -5927,7 +5941,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
static const char decimal_format[] =
/* Produce the same number of exponent
digits
as the native printf implementation. */
-# if defined _WIN32 && ! defined __CYGWIN__
+# if THREE_EXPONENT_DIGITS
"%+.3d";
# else
"%+.2d";
@@ -6175,7 +6189,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
static const wchar_t
decimal_format[] =
/* Produce the same number of
exponent digits
as the native printf
implementation. */
-# if defined _WIN32 && ! defined __CYGWIN__
+# if THREE_EXPONENT_DIGITS
{ '%', '+', '.', '3', 'd', '\0' };
# else
{ '%', '+', '.', '2', 'd', '\0' };
@@ -6189,7 +6203,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
static const char decimal_format[] =
/* Produce the same number of
exponent digits
as the native printf
implementation. */
-# if defined _WIN32 && ! defined __CYGWIN__
+# if THREE_EXPONENT_DIGITS
"%+.3d";
# else
"%+.2d";
@@ -6246,7 +6260,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
*p++ = '+';
/* Produce the same number of exponent digits
as
the native printf implementation. */
-# if defined _WIN32 && ! defined __CYGWIN__
+# if THREE_EXPONENT_DIGITS
*p++ = '0';
# endif
*p++ = '0';
diff --git a/tests/test-vasnprintf-posix2.c b/tests/test-vasnprintf-posix2.c
index b9db6ea542..c98702740e 100644
--- a/tests/test-vasnprintf-posix2.c
+++ b/tests/test-vasnprintf-posix2.c
@@ -361,9 +361,9 @@ main (int argc, char *argv[])
char *result = asnprintf (NULL, &length, "%'.5g", 3141592653.5897932386);
ASSERT (result != NULL);
#if defined _WIN32 && !defined __CYGWIN__
- /* Native Windows uses 3 digits for the exponent. */
- ASSERT (length == 11);
- ASSERT (streq (result, "3,1416e+009"));
+ /* Native Windows may use 3 digits for the exponent. */
+ ASSERT ((length == 11 && streq (result, "3,1416e+009"))
+ || (length == 10 && streq (result, "3,1416e+09")));
#else
ASSERT (length == 10);
ASSERT (streq (result, "3,1416e+09"));
@@ -375,9 +375,9 @@ main (int argc, char *argv[])
char *result = asnprintf (NULL, &length, "%'.5g",
-3141592653.5897932386);
ASSERT (result != NULL);
#if defined _WIN32 && !defined __CYGWIN__
- /* Native Windows uses 3 digits for the exponent. */
- ASSERT (length == 12);
- ASSERT (streq (result, "-3,1416e+009"));
+ /* Native Windows may use 3 digits for the exponent. */
+ ASSERT ((length == 12 && streq (result, "-3,1416e+009"))
+ || (length == 11 && streq (result, "-3,1416e+09")));
#else
ASSERT (length == 11);
ASSERT (streq (result, "-3,1416e+09"));
diff --git a/tests/test-vasnwprintf-posix2.c b/tests/test-vasnwprintf-posix2.c
index f74b46ac17..015cbebfe7 100644
--- a/tests/test-vasnwprintf-posix2.c
+++ b/tests/test-vasnwprintf-posix2.c
@@ -295,9 +295,9 @@ main (int argc, char *argv[])
wchar_t *result = asnwprintf (NULL, &length, L"%'.5g",
3141592653.5897932386);
ASSERT (result != NULL);
#if defined _WIN32 && !defined __CYGWIN__
- /* Native Windows uses 3 digits for the exponent. */
- ASSERT (length == 11);
- ASSERT (wcscmp (result, L"3,1416e+009") == 0);
+ /* Native Windows may use 3 digits for the exponent. */
+ ASSERT ((length == 11 && wcscmp (result, L"3,1416e+009") == 0)
+ || (length == 10 && wcscmp (result, L"3,1416e+09") == 0));
#else
ASSERT (length == 10);
ASSERT (wcscmp (result, L"3,1416e+09") == 0);
@@ -309,9 +309,9 @@ main (int argc, char *argv[])
wchar_t *result = asnwprintf (NULL, &length, L"%'.5g",
-3141592653.5897932386);
ASSERT (result != NULL);
#if defined _WIN32 && !defined __CYGWIN__
- /* Native Windows uses 3 digits for the exponent. */
- ASSERT (length == 12);
- ASSERT (wcscmp (result, L"-3,1416e+009") == 0);
+ /* Native Windows may use 3 digits for the exponent. */
+ ASSERT ((length == 12 && wcscmp (result, L"-3,1416e+009") == 0)
+ || (length == 11 && wcscmp (result, L"-3,1416e+09") == 0));
#else
ASSERT (length == 11);
ASSERT (wcscmp (result, L"-3,1416e+09") == 0);