Title: Round float at the decimal level to match CPython's _Py_dg_dtoa by changjoon-park · Pull Request #7761 · RustPython/RustPython · GitHub
Open Graph Title: Round float at the decimal level to match CPython's _Py_dg_dtoa by changjoon-park · Pull Request #7761 · RustPython/RustPython
X Title: Round float at the decimal level to match CPython's _Py_dg_dtoa by changjoon-park · Pull Request #7761 · RustPython/RustPython
Description: Summary CPython's float.__round__ (Objects/floatobject.c) routes through _Py_dg_dtoa and rounds at the decimal level. The previous round_float_digits multiplied by 10**ndigits and rounded at the IEEE 754 binary level, which diverges for values that aren't exactly representable. >>> round(2.675, 2) # CPython 3.14.4: 2.67 # RustPython main: 2.68 ❌ 2.675 stores as 2.67499...; dtoa correctly rounds it down to 2.67, but (2.675 * 100.0).round() / 100.0 lands on 2.68 because the multiplication produces a phantom 267.5 tie that round-half-to-even snaps up. Rust's {:.*} float formatting uses dtoa-style algorithms (Grisu3 + Dragon4 fallback) and matches CPython's _Py_dg_dtoa byte-for-byte. Replacing the multiply-then-round step with format!("{:.*}", ndigits, x).parse() removes the entire phantom-tie class for ndigits >= 0. For ndigits < 0 the existing divide-then-round path is preserved because dividing typical inputs by 10**|ndigits| produces genuine half-integer ties rather than synthesizing them. Verification (CPython 3.14.4) Probe set Cases Match Targeted half-tie probes (positive ndigits) 34 34/34 Negative ndigits regression 17 17/17 Special values (NaN, ±Inf, ±0.0, NDIGITS bounds) 12 12/12 No-arg round(x) (different code path) 10 10/10 Subnormal / denormal 6 6/6 Random fuzz (mantissa × ndigits ∈ {-5..50}) 4332 4332/4332 Tests unmasked Lib/test/test_float.py::RoundTestCase: test_matches_float_format — rounds against float(format(x, '.{n}f')) across many values; this is exactly what the fix does internally. test_previous_round_bugs — explicit "round-half-even" cases (e.g. round(25.0, -1) == 20.0). Test runs cargo run --release -- -m test test_float test_builtin test_decimal \ test_fractions test_complex test_math test_format test_long test_int \ test_statistics test_re 1573 tests pass, 0 regressions. All 188 extra_tests/snippets/*.py pass under the CI feature set (stdlib,importlib,stdio,encodings,sqlite,ssl-rustls,host_env). A regression block is added to extra_tests/snippets/builtin_round.py. Summary by CodeRabbit Bug Fixes Improved rounding to more closely match CPython’s behavior: better handling of halfway ties (banker’s rounding), negative ndigits, signed zeros, and non-finite inputs. Tests Expanded rounding tests to cover banker's rounding, negative ndigits, exact-half cases, NaN/infinity behavior, signed zero, and out-of-range ndigits.
Open Graph Description: Summary CPython's float.__round__ (Objects/floatobject.c) routes through _Py_dg_dtoa and rounds at the decimal level. The previous round_float_digits multiplied by 10**ndigits and rounded at th...
X Description: Summary CPython's float.__round__ (Objects/floatobject.c) routes through _Py_dg_dtoa and rounds at the decimal level. The previous round_float_digits multiplied by 10**ndigits and rounded a...
Opengraph URL: https://github.com/RustPython/RustPython/pull/7761
X: @github
Domain: github.com
| 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 | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:943ba585-3b4a-cac2-7f67-12a276f88ad6 |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | AC16:19AA62:38DE8A8:4831032:6B01F3BD |
| html-safe-nonce | 2baa28e6990d77308ab0f7cfd9ec4d9618634cab32c9c8519dc1bb61adfda7b7 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBQzE2OjE5QUE2MjozOERFOEE4OjQ4MzEwMzI6NkIwMUYzQkQiLCJ2aXNpdG9yX2lkIjoiODg1MjUwNjQwNTgyMzk2ODE4OSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | ea717c834a49515428d612ba793c046f2fe803cb9a994d5d520502306451e0a4 |
| hovercard-subject-tag | pull_request:3617637430 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,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/RustPython/RustPython/pull/7761/files |
| twitter:image | https://avatars.githubusercontent.com/u/131537123?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/131537123?s=400&v=4 |
| og:image:alt | Summary CPython's float.__round__ (Objects/floatobject.c) routes through _Py_dg_dtoa and rounds at the decimal level. The previous round_float_digits multiplied by 10**ndigits and rounded at th... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| turbo-cache-control | no-preview |
| diff-view | unified |
| go-import | github.com/RustPython/RustPython git https://github.com/RustPython/RustPython.git |
| octolytics-dimension-user_id | 39710557 |
| octolytics-dimension-user_login | RustPython |
| octolytics-dimension-repository_id | 135201145 |
| octolytics-dimension-repository_nwo | RustPython/RustPython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 135201145 |
| octolytics-dimension-repository_network_root_nwo | RustPython/RustPython |
| turbo-body-classes | logged-out env-production page-responsive full-width |
| disable-turbo | true |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 423dbb54c5e7bfecc838eb387c1435eb3d0e760d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width