Title: Exceptions from `round` implementation · Issue #322 · PythonCharmers/python-future · GitHub
Open Graph Title: Exceptions from `round` implementation · Issue #322 · PythonCharmers/python-future
X Title: Exceptions from `round` implementation · Issue #322 · PythonCharmers/python-future
Description: [Prompted by this StackOverflow question] There are a couple of issues with the current implementation of round from builtins: it doesn't support negative second argument: >>> from builtins import round >>> round(314, -2) Traceback (most...
Open Graph Description: [Prompted by this StackOverflow question] There are a couple of issues with the current implementation of round from builtins: it doesn't support negative second argument: >>> from builtins import ...
X Description: [Prompted by this StackOverflow question] There are a couple of issues with the current implementation of round from builtins: it doesn't support negative second argument: >>> from bui...
Opengraph URL: https://github.com/PythonCharmers/python-future/issues/322
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Exceptions from `round` implementation","articleBody":"[Prompted by [this StackOverflow question](https://stackoverflow.com/q/48360030/270986)]\r\n\r\nThere are a couple of issues with the current implementation of `round` from `builtins`:\r\n\r\n- it doesn't support negative second argument:\r\n\r\n```\r\n\u003e\u003e\u003e from builtins import round\r\n\u003e\u003e\u003e round(314, -2)\r\nTraceback (most recent call last):\r\n File \"\u003cstdin\u003e\", line 1, in \u003cmodule\u003e\r\n File \"/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/future/builtins/newround.py\", line 33, in newround\r\n raise NotImplementedError('negative ndigits not supported yet')\r\nNotImplementedError: negative ndigits not supported yet\r\n```\r\n\r\n- it can raise `decimal.InvalidOperation`:\r\n\r\n```\r\n\u003e\u003e\u003e round(1e50, 2)\r\nTraceback (most recent call last):\r\n File \"\u003cstdin\u003e\", line 1, in \u003cmodule\u003e\r\n File \"/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/future/builtins/newround.py\", line 43, in newround\r\n rounding=ROUND_HALF_EVEN)\r\n File \"/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/decimal.py\", line 2468, in quantize\r\n 'quantize result has too many digits for current context')\r\n File \"/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/decimal.py\", line 3872, in _raise_error\r\n raise error(explanation)\r\ndecimal.InvalidOperation: quantize result has too many digits for current context\r\n```\r\n\r\nThe first restriction seems unnecessary. The second can be overcome by using a dedicated `decimal` context with sufficient precision. For rounding a float `x` to `n` decimal places (with `n` possibly negative), I'd suggest something like the following:\r\n\r\n```\r\nfrom decimal import Context, Decimal\r\nc = Context(prec=800) # sufficient precision that the `quantize` result is representable\r\nquantum = Decimal(\"1e{}\".format(-n)) # should work for both n positive and negative\r\nrounded = float(c.quantize(Decimal(x), quantum))\r\n```\r\n\r\n(The bound of 800 here is a bit crude. The maximum number of significant digits needed to represent any IEEE 754 binary64 float exactly in decimal is 767.)","author":{"url":"https://github.com/mdickinson","@type":"Person","name":"mdickinson"},"datePublished":"2018-01-20T19:46:04.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":0},"url":"https://github.com/322/python-future/issues/322"}
| 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:68ff4353-863c-25aa-db1b-eff3b5b3f464 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | CAA8:31CE75:31144C:407B0A:69697B82 |
| html-safe-nonce | 08fdb2fed23fa34526dd1f534475fe726243dfbee92d7d85b99a7ddee094713a |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDQUE4OjMxQ0U3NTozMTE0NEM6NDA3QjBBOjY5Njk3QjgyIiwidmlzaXRvcl9pZCI6Ijg2MDU2MjUwNzEzNzM0ODI4ODIiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 593c237f73c1b9ab113ca88f65370a6e2cc743177c99a79087df0b6839a536da |
| hovercard-subject-tag | issue:290217971 |
| 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/PythonCharmers/python-future/322/issue_layout |
| twitter:image | https://opengraph.githubassets.com/d6169614eb605ccfc2ee8e4e87d5ba6d6c38ae7b3a650feec5afd7a8e3b1516f/PythonCharmers/python-future/issues/322 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/d6169614eb605ccfc2ee8e4e87d5ba6d6c38ae7b3a650feec5afd7a8e3b1516f/PythonCharmers/python-future/issues/322 |
| og:image:alt | [Prompted by this StackOverflow question] There are a couple of issues with the current implementation of round from builtins: it doesn't support negative second argument: >>> from builtins import ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | mdickinson |
| hostname | github.com |
| expected-hostname | github.com |
| None | f33e4b94c8824ab2b434d82a94139432fb5ebee9df4b75304140ad22508c4a77 |
| turbo-cache-control | no-preview |
| go-import | github.com/PythonCharmers/python-future git https://github.com/PythonCharmers/python-future.git |
| octolytics-dimension-user_id | 3365815 |
| octolytics-dimension-user_login | PythonCharmers |
| octolytics-dimension-repository_id | 11403699 |
| octolytics-dimension-repository_nwo | PythonCharmers/python-future |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 11403699 |
| octolytics-dimension-repository_network_root_nwo | PythonCharmers/python-future |
| 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 | 48f380098b30acbb700b04f1724481ca10d574fc |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width