From b063f597af49ef52834940ff560ac1db8e6d2e91 Mon Sep 17 00:00:00 2001 From: Peter Bierma Date: Tue, 4 Nov 2025 16:52:50 -0500 Subject: [PATCH 1/4] Document Py_RETURN_NAN and Py_RETURN_INF --- Doc/c-api/float.rst | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Doc/c-api/float.rst b/Doc/c-api/float.rst index 489676caa3a16a..61ac3f4b21681d 100644 --- a/Doc/c-api/float.rst +++ b/Doc/c-api/float.rst @@ -78,6 +78,16 @@ Floating-Point Objects Return the minimum normalized positive float *DBL_MIN* as C :c:expr:`double`. +.. c:macro:: Py_RETURN_NAN + + Return :data:`math.nan` from a function. + + +.. c:macro:: Py_RETURN_INF + + Return :data:`math.inf` from a function. + + Pack and Unpack functions ------------------------- From 2679d3530408621163bf9b5f6a60c5bf9846b0ae Mon Sep 17 00:00:00 2001 From: Peter Bierma Date: Tue, 4 Nov 2025 17:12:57 -0500 Subject: [PATCH 2/4] Apparently, Py_RETURN_INF() is a function-like macro. --- Doc/c-api/float.rst | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Doc/c-api/float.rst b/Doc/c-api/float.rst index 61ac3f4b21681d..b3bc34221817aa 100644 --- a/Doc/c-api/float.rst +++ b/Doc/c-api/float.rst @@ -83,9 +83,10 @@ Floating-Point Objects Return :data:`math.nan` from a function. -.. c:macro:: Py_RETURN_INF +.. c:macro:: Py_RETURN_INF(sign) - Return :data:`math.inf` from a function. + Return :data:`math.inf` or :data:`-math.inf ` from a function, + depending on the sign of *sign*. Pack and Unpack functions From 86b35f16032e6bbd2ad804e52b03c6fc6731a1e1 Mon Sep 17 00:00:00 2001 From: Peter Bierma Date: Tue, 4 Nov 2025 21:33:29 -0500 Subject: [PATCH 3/4] Document the expansion of the macros. --- Doc/c-api/float.rst | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/Doc/c-api/float.rst b/Doc/c-api/float.rst index b3bc34221817aa..c34ca861791ed2 100644 --- a/Doc/c-api/float.rst +++ b/Doc/c-api/float.rst @@ -82,12 +82,23 @@ Floating-Point Objects Return :data:`math.nan` from a function. + On most platforms, this is equivalent to ``return PyFloat_FromDouble(NAN)``. + .. c:macro:: Py_RETURN_INF(sign) Return :data:`math.inf` or :data:`-math.inf ` from a function, depending on the sign of *sign*. + On most platforms, this is equivalent to the following:: + + if (copysign(1., sign) == 1.) { + return PyFloat_FromDouble(INFINITY); + } + else { + return PyFloat_FromDouble(-INFINITY); + } + Pack and Unpack functions ------------------------- From 29d549e819ef5e7c6e7248e52cfef6ce05460014 Mon Sep 17 00:00:00 2001 From: Peter Bierma Date: Wed, 5 Nov 2025 09:18:47 -0500 Subject: [PATCH 4/4] Update Doc/c-api/float.rst Co-authored-by: Sergey B Kirpichev --- Doc/c-api/float.rst | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/Doc/c-api/float.rst b/Doc/c-api/float.rst index c34ca861791ed2..1085c32a537071 100644 --- a/Doc/c-api/float.rst +++ b/Doc/c-api/float.rst @@ -92,12 +92,7 @@ Floating-Point Objects On most platforms, this is equivalent to the following:: - if (copysign(1., sign) == 1.) { - return PyFloat_FromDouble(INFINITY); - } - else { - return PyFloat_FromDouble(-INFINITY); - } + return PyFloat_FromDouble(copysign(INFINITY, sign)); Pack and Unpack functions