Title: Improve accuracy of builtin sum() for float inputs · Issue #100425 · python/cpython · GitHub
Open Graph Title: Improve accuracy of builtin sum() for float inputs · Issue #100425 · python/cpython
X Title: Improve accuracy of builtin sum() for float inputs · Issue #100425 · python/cpython
Description: Currently sum() makes no efforts to improve accuracy over a simple running total. We do have math.fsum() that makes extreme efforts to be almost perfect; however, that function isn't well known, it runs 10x slower than regular sum(), and...
Open Graph Description: Currently sum() makes no efforts to improve accuracy over a simple running total. We do have math.fsum() that makes extreme efforts to be almost perfect; however, that function isn't well known, it...
X Description: Currently sum() makes no efforts to improve accuracy over a simple running total. We do have math.fsum() that makes extreme efforts to be almost perfect; however, that function isn't well known...
Opengraph URL: https://github.com/python/cpython/issues/100425
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Improve accuracy of builtin sum() for float inputs","articleBody":"Currently `sum()` makes no efforts to improve accuracy over a simple running total. We do have `math.fsum()` that makes extreme efforts to be almost perfect; however, that function isn't well known, it runs 10x slower than regular `sum()`, and it always coerces to a float.\r\n\r\nI suggest switching the builtin `sum()` handling of float inputs to Arnold Neumaier's improved variation of compensated summation. Per his [paper](https://www.mat.univie.ac.at/~neum/scan/01.pdf), this algorithm has excellent error bounds (though not as perfect as `fsum()`:\r\n\r\n```\r\n|s - š| ≤ ε|s| + ε²(3/4n² + n)·Σ|aᵢ| (IV,12)\r\n|s - š| ≤ ε|s| + ε²(1/4n³ + 5/2n² + n)·Max|aᵢ| (IV,13)\r\n```\r\n\r\nThe compensation tracking runs in parallel to the main accumulation. And except for pathological cases, the branch is predictable, making the test essentially free. Thanks to the magic of instruction level parallelism and branch prediction, this improvement has zero cost on my Apple M1 build. Timed with:\r\n\r\n`./python.exe -m timeit -s 'from random import expovariate as r' -s 'd=[r(1.0) for i in range(10_000)]' 'sum(d)'`\r\n\r\nN.B. Numpy switched from a simple running total to [partial pairwise summation](https://numpy.org/doc/stable/reference/generated/numpy.sum.html). That isn't as accurate as what is being proposed here, but it made more sense for them because the extra work of Neumaier summation isn't masked by the overhead of fetching values from an iterator as we do here. Also with an iterator, we can't do pairwise summation without using auxiliary memory.\r\n\r\n\n\n\u003c!-- gh-linked-prs --\u003e\n### Linked PRs\n* gh-100426\n* gh-100860\n* gh-101854\n* gh-107785\n* gh-107787\n\u003c!-- /gh-linked-prs --\u003e\n","author":{"url":"https://github.com/rhettinger","@type":"Person","name":"rhettinger"},"datePublished":"2022-12-22T06:48:17.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":24},"url":"https://github.com/100425/cpython/issues/100425"}
| 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:726f7906-5388-8fb7-1db2-4bbe85832879 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | CBCC:BE21E:9B490B:DCDCBA:696901E3 |
| html-safe-nonce | 7b802bfd1155136b5d9518ad6736d0886e941fffc6bea9315cb52e5cf2694e93 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDQkNDOkJFMjFFOjlCNDkwQjpEQ0RDQkE6Njk2OTAxRTMiLCJ2aXNpdG9yX2lkIjoiMzY3ODc5MjExNjc1MDMyMDA5OSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 4e91fc1d9c1dc2c1736783fc243111caa57dcff02252f51f19a0cd50d87c1b2d |
| hovercard-subject-tag | issue:1507355192 |
| 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/100425/issue_layout |
| twitter:image | https://opengraph.githubassets.com/4389b31276ce504bdb0ca1cd15adedf51c0c565fa22bd89f741dff18e5cff792/python/cpython/issues/100425 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/4389b31276ce504bdb0ca1cd15adedf51c0c565fa22bd89f741dff18e5cff792/python/cpython/issues/100425 |
| og:image:alt | Currently sum() makes no efforts to improve accuracy over a simple running total. We do have math.fsum() that makes extreme efforts to be almost perfect; however, that function isn't well known, it... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | rhettinger |
| hostname | github.com |
| expected-hostname | github.com |
| None | e6156bd4ef9f2dc8dadf4c49a8f7ed8532186388cef72eda3ccb9f0ab3b8cfca |
| 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 | fd1938215b152e2c6a29cf56fec07fd9f91f1203 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width