Title: Avoid a temporary float in `PyFloat_AsDouble()` for exact ints · Issue #162477 · python/cpython · GitHub
Open Graph Title: Avoid a temporary float in `PyFloat_AsDouble()` for exact ints · Issue #162477 · python/cpython
X Title: Avoid a temporary float in `PyFloat_AsDouble()` for exact ints · Issue #162477 · python/cpython
Description: Feature or enhancement When a caller such as math.sqrt(42) asks for a C double, PyFloat_AsDouble() calls the integer's nb_float slot. That slot calls PyLong_AsDouble() and wraps the result in a Python float. The caller then reads the sam...
Open Graph Description: Feature or enhancement When a caller such as math.sqrt(42) asks for a C double, PyFloat_AsDouble() calls the integer's nb_float slot. That slot calls PyLong_AsDouble() and wraps the result in a Pyt...
X Description: Feature or enhancement When a caller such as math.sqrt(42) asks for a C double, PyFloat_AsDouble() calls the integer's nb_float slot. That slot calls PyLong_AsDouble() and wraps the result in a...
Opengraph URL: https://github.com/python/cpython/issues/162477
X: @github
Domain: github.com
IPv4 address: 140.82.112.3
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Avoid a temporary float in `PyFloat_AsDouble()` for exact ints","articleBody":"### Feature or enhancement\n\nWhen a caller such as `math.sqrt(42)` asks for a C `double`, [PyFloat_AsDouble()](https://github.com/python/cpython/blob/22691a32b122cff3ad17460c068f70e806cd7e84/Objects/floatobject.c#L279-L303) calls the integer's `nb_float` slot. That slot calls `PyLong_AsDouble()` and wraps the result in a Python float. The caller then reads the same double and releases the temporary float.\n\nhttps://github.com/python/cpython/blob/22691a32b122cff3ad17460c068f70e806cd7e84/Objects/longobject.c#L5974-L5981\n\nFor an exact `int`, `PyFloat_AsDouble()` could return `PyLong_AsDouble()` directly...\n\nmacOS M4 PGO+LTO:\n\n| Operation | Baseline | Patched | Baseline / patched |\n| --- | ---: | ---: | ---: |\n| `sqrt(42)` | 16.40 ns | 13.06 ns | 1.256x |\n| `pack(1, 2, 3)` | 40.08 ns | 30.64 ns | 1.308x |\n| `array('d', ints)` | 10.31 µs | 7.19 µs | 1.433x |\n| `array('d', floats)` (control) | 6.65 µs | 6.16 µs | 1.080x |\n\n### Has this already been discussed elsewhere?\n\nNo response given\n\n### Links to previous discussion of this feature:\n\n_No response_\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-162478\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/maurycy","@type":"Person","name":"maurycy"},"datePublished":"2026-11-23T08:14:04Z","url":"https://github.com/python/cpython/issues/162477"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:b980576f-3c68-8f93-ffd3-710a1b6d8a28 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9B22:113BA5:D7F9CC:123393A:6B064BFF |
| html-safe-nonce | 22c25cb214c8394e6431696762a41af8e17158f8dd14d52a6594f5d7c21f5494 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5QjIyOjExM0JBNTpEN0Y5Q0M6MTIzMzkzQTo2QjA2NEJGRiIsInZpc2l0b3JfaWQiOiI3NzU2NDg3NjIxNTQ3MDE5MjYzIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | e644c6e3b5f1d37b4d08ffeda2d954f939b216498656ff31801c38013f047d7c |
| hovercard-subject-tag | issue:6555117019 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/162477/issue_layout |
| twitter:image | https://opengraph.githubassets.com/2e441c44f5a6040500c01b309f5a1e8069b6222865195f0cf98d8a0a77a5059c/python/cpython/issues/162477 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/2e441c44f5a6040500c01b309f5a1e8069b6222865195f0cf98d8a0a77a5059c/python/cpython/issues/162477 |
| og:image:alt | Feature or enhancement When a caller such as math.sqrt(42) asks for a C double, PyFloat_AsDouble() calls the integer's nb_float slot. That slot calls PyLong_AsDouble() and wraps the result in a Pyt... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | maurycy |
| hostname | github.com |
| expected-hostname | github.com |
| None | 772b6997ca4daa0428dd49a25d76052e765555ae560fe9c88517eb828bbd861e |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | f1f9a08f7c769a37f1dfc18ad43d463c222a7eea |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | max-age=0, private, must-revalidate |
Viewport: width=device-width