--- ./Include/pymath.h_orig	2026-07-13 08:55:07.844625450 -0400
+++ ./Include/pymath.h	2026-07-13 08:58:12.148561099 -0400
@@ -44,7 +44,11 @@
 
 // Py_INFINITY: Value that evaluates to a positive double infinity.
 #ifndef Py_INFINITY
-#  define Py_INFINITY ((double)INFINITY)
+#  if defined(__GNUC__)
+#    define Py_INFINITY (__builtin_inf())
+#  else
+#    define Py_INFINITY ((double)INFINITY)
+#  endif
 #endif
 
 /* Py_HUGE_VAL should always be the same as Py_INFINITY.  But historically
@@ -58,8 +62,12 @@
 /* Py_NAN: Value that evaluates to a quiet Not-a-Number (NaN).  The sign is
  * undefined and normally not relevant, but e.g. fixed for float("nan").
  */
-#if !defined(Py_NAN)
+#ifndef Py_NAN
+#  if defined(__GNUC__)
+#    define Py_NAN (__builtin_nan(""))
+#  else
 #    define Py_NAN ((double)NAN)
+#  endif
 #endif
 
 #endif /* Py_PYMATH_H */
