Title: `round(number, digits)` is incorrect for non-zero `digits` · Issue #1554 · RustPython/RustPython · GitHub
Open Graph Title: `round(number, digits)` is incorrect for non-zero `digits` · Issue #1554 · RustPython/RustPython
X Title: `round(number, digits)` is incorrect for non-zero `digits` · Issue #1554 · RustPython/RustPython
Description: round with non-zero digits is notoriously hard to implement correctly, and I think #1521 underestimates the difficulty. The following script produces a lot of counterexamples in RustPython but none in Python 3 (at least after 10M trials)...
Open Graph Description: round with non-zero digits is notoriously hard to implement correctly, and I think #1521 underestimates the difficulty. The following script produces a lot of counterexamples in RustPython but none...
X Description: round with non-zero digits is notoriously hard to implement correctly, and I think #1521 underestimates the difficulty. The following script produces a lot of counterexamples in RustPython but none...
Opengraph URL: https://github.com/RustPython/RustPython/issues/1554
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"`round(number, digits)` is incorrect for non-zero `digits`","articleBody":"`round` with non-zero digits is [notoriously hard to implement correctly](https://news.ycombinator.com/item?id=20816827), and I think #1521 underestimates the difficulty. The following script produces a lot of counterexamples in RustPython but none in Python 3 (at least after 10M trials):\r\n\r\n```python\r\nfrom random import random\r\n\r\ntrial = 0\r\nwhile True:\r\n trial += 1\r\n if trial % 10000 == 0: print(trial)\r\n\r\n num = random()\r\n\r\n # produces a single integral digit, `.` and 52+ fractional digits.\r\n # unlike str, `%f` retains fractional digits of the true value.\r\n #\r\n # note: while not specified, the implementation of `random.random`\r\n # ensures that those fractional digits contain all significant digits.\r\n # this is pretty important for determining a midpoint for Banker's rounding;\r\n # if it's not the case, simply change the number of frac digits to 1100+.\r\n num_str = '%.60f' % num\r\n\r\n # TODO: RustPython doesn't yet have random.randint\r\n digits = int(1 + random() * 16)\r\n\r\n # same to `int(num_str * 10**digits)`, but exact\r\n scaled = int(num_str[0] + num_str[2:2+digits], 10)\r\n\r\n # same to `round(num_str * 10**digits)`, but exact\r\n scaled_and_rounded = scaled\r\n if num_str[2+digits] >= '5':\r\n # Banker's rounding: when `num` is equally close to both rounded values, prefer even\r\n is_midpoint = all(c == '0' for c in num_str[3+digits:])\r\n if not is_midpoint or scaled % 2 != 0: scaled_and_rounded += 1\r\n\r\n # simulate `round(num, digits)`\r\n scaled_and_rounded_str = ('%0' + str(1+digits) + 'd') % scaled_and_rounded\r\n rounded = float(scaled_and_rounded_str[0] + '.' + scaled_and_rounded_str[1:])\r\n\r\n rounded_impl = round(num, digits)\r\n if rounded != rounded_impl:\r\n print(f'failed: round({num_str[:2+digits]}_{num_str[2+digits:5+digits]}..., {digits}) = expected {rounded}, got {rounded_impl}')\r\n```\r\n\r\nExample output in RustPython:\r\n\r\n```\r\nfailed: round(0.3965324291450832_427..., 16) = expected 0.3965324291450832, got 0.3965324291450833\r\nfailed: round(0.289032886419551_493..., 15) = expected 0.289032886419551, got 0.289032886419552\r\nfailed: round(0.3364255023393837_307..., 16) = expected 0.3364255023393837, got 0.3364255023393838\r\nfailed: round(0.1182629781749734_432..., 16) = expected 0.1182629781749734, got 0.1182629781749735\r\nfailed: round(0.2254041491428512_289..., 16) = expected 0.2254041491428512, got 0.2254041491428513\r\nfailed: round(0.781000838713878_486..., 15) = expected 0.781000838713878, got 0.781000838713879\r\n```\r\n\r\nIt should be noted that the string representation of `round(num, digits)` doesn't need to be correctly rounded. In fact, it might not be possible at all. Python's implementation only tries to return *a* floating point number closest to the true rounded value and probably it is the best possible result when you have to return floating point numbers.\r\n\r\nImplementation-wise I think there is no way easier than converting a number to the stringy value and back, as Python does. Note that the stringy value doesn't need to be an actual string, it just has to be a value that can be easily converted to decimal digits. (Rust has an unstable interface to those stringy values, but they are exposed just for tests.) As we have BigInts as a given, we probably should make use of them.","author":{"url":"https://github.com/lifthrasiir","@type":"Person","name":"lifthrasiir"},"datePublished":"2019-10-21T13:07:50Z","url":"https://github.com/RustPython/RustPython/issues/1554"}
| 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:4cf72fbe-c5b8-4050-b5fa-d3f6ad6d1585 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C1C2:2F9718:2FA4DAA:3FE59C7:6B01F308 |
| html-safe-nonce | b24ebe1b67a2acda09573a08979727094da8961d6159a73615e9e9de0fcbbf26 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDMUMyOjJGOTcxODoyRkE0REFBOjNGRTU5Qzc6NkIwMUYzMDgiLCJ2aXNpdG9yX2lkIjoiNzI5OTI5NTg5MDU1NTEzODgyNCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 609060795375157ea4a2379ee6938c89a4e360b83a91d3b117cd2c601486a692 |
| hovercard-subject-tag | issue:509950710 |
| 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/RustPython/RustPython/1554/issue_layout |
| twitter:image | https://opengraph.githubassets.com/89643c2d23a911a4c0fcd729a2639bc2f85e6e85318e191c818909dfd1a2ed3e/RustPython/RustPython/issues/1554 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/89643c2d23a911a4c0fcd729a2639bc2f85e6e85318e191c818909dfd1a2ed3e/RustPython/RustPython/issues/1554 |
| og:image:alt | round with non-zero digits is notoriously hard to implement correctly, and I think #1521 underestimates the difficulty. The following script produces a lot of counterexamples in RustPython but none... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | lifthrasiir |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| turbo-cache-control | no-preview |
| 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 |
| disable-turbo | false |
| 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 |
| react-app-name | issues |
Links:
Viewport: width=device-width