FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-156145: Don't rely on errno for float `pow` and `is_integer` by hpkfft · Pull Request #156551 · python/cpython · GitHub

Repository navigation

gh-156145: Don't rely on errno for float pow and is_integer - #156551

Open
hpkfft wants to merge 1 commit into
python:mainfrom
hpkfft:errno-floatobject
Open

hpkfft wants to merge 1 commit into
python:mainfrom
hpkfft:errno-floatobject

Conversation

hpkfft commented Aug 29, 2026 •
edited by bedevere-app Bot
Loading

Copy link
Copy Markdown
Contributor

This PR only addresses the Objects/floatobject.c portion of the linked issue.

Buy one get one free: I believe this PR allows closing #91290

Co-authored-by: Sergey B Kirpichev <skirpichev@gmail.com>

skirpichev left a comment •
edited
Loading

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

There is one extra case in this file: double_round(). We use errno here to check overflows in _Py_dg_dtoa(), but can use isinf() as well.

On one hand, dtoa.c code bundled with the CPython and we can trust errno value here. On another hand, as discussed in the issue thread, we can get some performance boost with skipping errno checks (gcc v14.2, default configure flags):

Benchmark ref patch
round(31.4) 1.09 us 978 ns: 1.12x faster
benchmark and patch
import pyperf

x = 31.4

runner = pyperf.Runner()
runner.bench_func(f"round({x})", round, x, -1)
diff --git a/Objects/floatobject.c b/Objects/floatobject.c
index 17e6a729dcd..8342dcbbd16 100644
--- a/Objects/floatobject.c
+++ b/Objects/floatobject.c
@@ -937,11 +936,10 @@ double_round(double x, int ndigits) {
                   buf, decpt - (int)buflen);
 
     /* and convert the resulting string back to a double */
-    errno = 0;
     _Py_SET_53BIT_PRECISION_START;
     rounded = _Py_dg_strtod(mybuf, NULL);
     _Py_SET_53BIT_PRECISION_END;
-    if (errno == ERANGE && fabs(rounded) >= 1.)
+    if (isinf(rounded))
         PyErr_SetString(PyExc_OverflowError,
                         "rounded value too large to represent");
     else

Copy link
Copy Markdown
Member

CC @picnixz

Copy link
Copy Markdown
Member

Another macro removed in #156694.

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants


Back | FazBrowse Home | New Git URL