Skip to content

Commit 54842e4

Browse files
authored
bpo-46640: Py_NAN now uses the C99 NAN constant (GH-31134)
Building Python now requires a C99 <math.h> header file providing a NAN constant, or the __builtin_nan() built-in function. If a platform does not support Not-a-Number (NaN), the Py_NO_NAN macro can be defined in the pyconfig.h file.
1 parent f1e29ce commit 54842e4

File tree

3 files changed

+20
-21
lines changed

3 files changed

+20
-21
lines changed

Doc/whatsnew/3.11.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,12 @@ Build Changes
604604
``isinf()``, ``isnan()``, ``round()``.
605605
(Contributed by Victor Stinner in :issue:`45440`.)
606606

607+
* Building Python now requires a C99 ``<math.h>`` header file providing
608+
a ``NAN`` constant, or the ``__builtin_nan()`` built-in function. If a
609+
platform does not support Not-a-Number (NaN), the ``Py_NO_NAN`` macro can be
610+
defined in the ``pyconfig.h`` file.
611+
(Contributed by Victor Stinner in :issue:`46640`.)
612+
607613
* Freelists for object structs can now be disabled. A new :program:`configure`
608614
option :option:`!--without-freelists` can be used to disable all freelists
609615
except empty tuple singleton.

Include/pymath.h

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -51,29 +51,18 @@
5151
#endif
5252

5353
/* Py_NAN
54-
* A value that evaluates to a NaN. On IEEE 754 platforms INF*0 or
55-
* INF/INF works. Define Py_NO_NAN in pyconfig.h if your platform
56-
* doesn't support NaNs.
54+
* A value that evaluates to a quiet Not-a-Number (NaN).
55+
* Define Py_NO_NAN in pyconfig.h if your platform doesn't support NaNs.
5756
*/
5857
#if !defined(Py_NAN) && !defined(Py_NO_NAN)
59-
# if !defined(__INTEL_COMPILER)
60-
# define Py_NAN (Py_HUGE_VAL * 0.)
61-
# else /* __INTEL_COMPILER */
62-
# if defined(ICC_NAN_STRICT)
63-
#pragma float_control(push)
64-
#pragma float_control(precise, on)
65-
#pragma float_control(except, on)
66-
Py_NO_INLINE static double __icc_nan()
67-
{
68-
return sqrt(-1.0);
69-
}
70-
#pragma float_control (pop)
71-
# define Py_NAN __icc_nan()
72-
# else /* ICC_NAN_RELAXED as default for Intel Compiler */
73-
static const union { unsigned char buf[8]; double __icc_nan; } __nan_store = {0,0,0,0,0,0,0xf8,0x7f};
74-
# define Py_NAN (__nan_store.__icc_nan)
75-
# endif /* ICC_NAN_STRICT */
76-
# endif /* __INTEL_COMPILER */
58+
# if _Py__has_builtin(__builtin_nan)
59+
// Built-in implementation of the ISO C99 function nan(): quiet NaN.
60+
# define Py_NAN (__builtin_nan(""))
61+
#else
62+
// Use C99 NAN constant: quiet Not-A-Number.
63+
// NAN is a float, Py_NAN is a double: cast to double.
64+
# define Py_NAN ((double)NAN)
65+
# endif
7766
#endif
7867

7968
#endif /* Py_PYMATH_H */
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Building Python now requires a C99 ``<math.h>`` header file providing a ``NAN``
2+
constant, or the ``__builtin_nan()`` built-in function. If a platform does not
3+
support Not-a-Number (NaN), the ``Py_NO_NAN`` macro can be defined in the
4+
``pyconfig.h`` file. Patch by Victor Stinner.

0 commit comments

Comments
 (0)