Add C2X _PRINTF_NAN_LEN_MAX

Message ID alpine.DEB.2.22.394.2109302008500.896631@digraph.polyomino.org.uk
State New
Headers show
Series
  • Add C2X _PRINTF_NAN_LEN_MAX
Related show

Commit Message

Joseph Myers Sept. 30, 2021, 8:09 p.m.
C2X adds a macro _PRINTF_NAN_LEN_MAX to <stdio.h>, giving the maximum
length of printf output for a NaN.  glibc never includes an
n-char-sequence in its printf output for NaNs, so the correct value
for glibc is 4 ("-nan" or "-NAN"); define the macro accordingly.

This patch makes the macro definition conditional on __GLIBC_USE
(ISOC2X), as is generally done with features from new standard
versions.  The name is in the implementation namespace for older
standards, so it would also be possible to define it unconditionally.

Tested for x86_64.

---

Any comments on this patch, especially the choice of whether to define
the macro only for C2X or unconditionally?


-- 
Joseph S. Myers
joseph@codesourcery.com

Comments

Florian Weimer via Libc-alpha Sept. 30, 2021, 8:51 p.m. | #1
* Joseph Myers:

> C2X adds a macro _PRINTF_NAN_LEN_MAX to <stdio.h>, giving the maximum

> length of printf output for a NaN.  glibc never includes an

> n-char-sequence in its printf output for NaNs, so the correct value

> for glibc is 4 ("-nan" or "-NAN"); define the macro accordingly.

>

> This patch makes the macro definition conditional on __GLIBC_USE

> (ISOC2X), as is generally done with features from new standard

> versions.  The name is in the implementation namespace for older

> standards, so it would also be possible to define it unconditionally.


Patch looks okay, but I don't have a way to check against ISO
publications.

Thanks,
Florian

Patch

diff --git a/NEWS b/NEWS
index 6bc98873ab..da1e3a5cd3 100644
--- a/NEWS
+++ b/NEWS
@@ -42,6 +42,8 @@  Major new features:
 * The __STDC_IEC_60559_BFP__ and __STDC_IEC_60559_COMPLEX__ macros are
   predefined as specified in TS 18661-1:2014.
 
+* The ISO C2X macro _PRINTF_NAN_LEN_MAX has been added to <stdio.h>.
+
 Deprecated and removed features, and other changes affecting compatibility:
 
 * The r_version update in the debugger interface makes the glibc binary
diff --git a/libio/stdio.h b/libio/stdio.h
index 497da016ff..0a5c76b552 100644
--- a/libio/stdio.h
+++ b/libio/stdio.h
@@ -133,6 +133,12 @@  typedef __fpos64_t fpos64_t;
 #include <bits/stdio_lim.h>
 
 
+#if __GLIBC_USE (ISOC2X)
+/* Maximum length of printf output for a NaN.  */
+# define _PRINTF_NAN_LEN_MAX 4
+#endif
+
+
 /* Standard streams.  */
 extern FILE *stdin;		/* Standard input stream.  */
 extern FILE *stdout;		/* Standard output stream.  */
diff --git a/manual/stdio.texi b/manual/stdio.texi
index fd7ed0cedc..1d235be68d 100644
--- a/manual/stdio.texi
+++ b/manual/stdio.texi
@@ -2108,7 +2108,10 @@  If the value to be printed represents infinity or a NaN, the output is
 @w{[@code{-}]@code{inf}} or @code{nan} respectively if the conversion
 specifier is @samp{%a}, @samp{%e}, @samp{%f}, or @samp{%g} and it is
 @w{[@code{-}]@code{INF}} or @code{NAN} respectively if the conversion is
-@samp{%A}, @samp{%E}, or @samp{%G}.
+@samp{%A}, @samp{%E}, or @samp{%G}.  On some implementations, a NaN
+may result in longer output with information about the payload of the
+NaN; ISO C2X defines a macro @code{_PRINTF_NAN_LEN_MAX} giving the
+maximum length of such output.
 
 The following flags can be used to modify the behavior: