Title: Invalid corner cases (resulting in nan+nanj) in complex division · Issue #119372 · python/cpython · GitHub
Open Graph Title: Invalid corner cases (resulting in nan+nanj) in complex division · Issue #119372 · python/cpython
X Title: Invalid corner cases (resulting in nan+nanj) in complex division · Issue #119372 · python/cpython
Description: Bug report Bug description: Reproducer: >>> (1+1j)/(cmath.inf+0j) # ok 0j >>> (1+1j)/cmath.infj # ok -0j >>> (1+1j)/(cmath.inf+cmath.infj) # should be 0j, isn't? (nan+nanj) c.f. MPC: >>> gmpy2.mpc('(1+1j)')/gmpy2.mpc('(inf+infj)') mpc('0...
Open Graph Description: Bug report Bug description: Reproducer: >>> (1+1j)/(cmath.inf+0j) # ok 0j >>> (1+1j)/cmath.infj # ok -0j >>> (1+1j)/(cmath.inf+cmath.infj) # should be 0j, isn't? (nan+nanj) c.f. MPC: >>> gmpy2.mpc(...
X Description: Bug report Bug description: Reproducer: >>> (1+1j)/(cmath.inf+0j) # ok 0j >>> (1+1j)/cmath.infj # ok -0j >>> (1+1j)/(cmath.inf+cmath.infj) # should be 0j, isn't? (nan...
Opengraph URL: https://github.com/python/cpython/issues/119372
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Invalid corner cases (resulting in nan+nanj) in complex division","articleBody":"# Bug report\r\n\r\n### Bug description:\r\n\r\nReproducer:\r\n```pycon\r\n\u003e\u003e\u003e (1+1j)/(cmath.inf+0j) # ok\r\n0j\r\n\u003e\u003e\u003e (1+1j)/cmath.infj # ok\r\n-0j\r\n\u003e\u003e\u003e (1+1j)/(cmath.inf+cmath.infj) # should be 0j, isn't?\r\n(nan+nanj)\r\n```\r\nc.f. MPC:\r\n```pycon\r\n\u003e\u003e\u003e gmpy2.mpc('(1+1j)')/gmpy2.mpc('(inf+infj)')\r\nmpc('0.0+0.0j')\r\n```\r\n\r\nIt seems, there are not so much such cases (i.e. when the numerator is finite and the denominator has infinite and infinite/nan components, or if the numerator has infinite components and the denominator has zeros). Following patch (mostly literally taken from the C11 Annex G.5.1 ``_Cdivd()``'s example, except for the ``denom == 0.0`` case) should fix the issue:\r\n```diff\r\ndiff --git a/Objects/complexobject.c b/Objects/complexobject.c\r\nindex b3d57b8337..9041cbb8ae 100644\r\n--- a/Objects/complexobject.c\r\n+++ b/Objects/complexobject.c\r\n@@ -122,6 +122,27 @@ _Py_c_quot(Py_complex a, Py_complex b)\r\n /* At least one of b.real or b.imag is a NaN */\r\n r.real = r.imag = Py_NAN;\r\n }\r\n+\r\n+ /* Recover infinities and zeros that computed as nan+nanj */\r\n+ if (isnan(r.real) \u0026\u0026 isnan(r.imag)) {\r\n+ if ((isinf(a.real) || isinf(a.imag))\r\n+ \u0026\u0026 isfinite(b.real) \u0026\u0026 isfinite(b.imag))\r\n+ {\r\n+ const double x = copysign(isinf(a.real) ? 1.0 : 0.0, a.real);\r\n+ const double y = copysign(isinf(a.imag) ? 1.0 : 0.0, a.imag);\r\n+ r.real = Py_INFINITY * (x*b.real + y*b.imag);\r\n+ r.imag = Py_INFINITY * (y*b.real - x*b.imag);\r\n+ }\r\n+ else if ((fmax(abs_breal, abs_bimag) == Py_INFINITY)\r\n+ \u0026\u0026 isfinite(a.real) \u0026\u0026 isfinite(a.imag))\r\n+ {\r\n+ const double x = copysign(isinf(b.real) ? 1.0 : 0.0, b.real);\r\n+ const double y = copysign(isinf(b.imag) ? 1.0 : 0.0, b.imag);\r\n+ r.real = 0.0 * (a.real*x + a.imag*y);\r\n+ r.imag = 0.0 * (a.imag*x - a.real*y);\r\n+ }\r\n+ }\r\n+\r\n return r;\r\n }\r\n \r\n```\r\n\r\nPerhaps, we could also clear ending remark in ``_Py_c_quot()`` comment:\r\nhttps://github.com/python/cpython/blob/d065edfb66470bbf06367b3570661d0346aa6707/Objects/complexobject.c#L91-L92\r\n\r\nI'll prepare a PR, if this issue will be accepted.\r\n\r\n### CPython versions tested on:\r\n\r\nCPython main branch\r\n\r\n### Operating systems tested on:\r\n\r\n_No response_\n\n\u003c!-- gh-linked-prs --\u003e\n### Linked PRs\n* gh-119457\n\u003c!-- /gh-linked-prs --\u003e\n","author":{"url":"https://github.com/skirpichev","@type":"Person","name":"skirpichev"},"datePublished":"2024-05-22T02:07:17.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":4},"url":"https://github.com/119372/cpython/issues/119372"}
| 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:b9383c05-0911-297c-c42a-5e7bec7bccac |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8B94:1A425C:163B855:1D369E0:696B847B |
| html-safe-nonce | a18e3b850f38598d494aa67b37bb86705566f9ea9a61865f0fc674f59a7117bb |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4Qjk0OjFBNDI1QzoxNjNCODU1OjFEMzY5RTA6Njk2Qjg0N0IiLCJ2aXNpdG9yX2lkIjoiNzQ3NDQ2NjczNzI4NjI1MTY0MyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 7938750ff943ab77f8648d2df5b260e458eafad4141d35682a9e46ece30933d4 |
| hovercard-subject-tag | issue:2309423649 |
| 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/119372/issue_layout |
| twitter:image | https://opengraph.githubassets.com/2d211603a9d2f49fc9c488db844b48171bb7be95deff810eb71c61275ae1b7a2/python/cpython/issues/119372 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/2d211603a9d2f49fc9c488db844b48171bb7be95deff810eb71c61275ae1b7a2/python/cpython/issues/119372 |
| og:image:alt | Bug report Bug description: Reproducer: >>> (1+1j)/(cmath.inf+0j) # ok 0j >>> (1+1j)/cmath.infj # ok -0j >>> (1+1j)/(cmath.inf+cmath.infj) # should be 0j, isn't? (nan+nanj) c.f. MPC: >>> gmpy2.mpc(... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | skirpichev |
| hostname | github.com |
| expected-hostname | github.com |
| None | 5f99f7c1d70f01da5b93e5ca90303359738944d8ab470e396496262c66e60b8d |
| 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 | 82560a55c6b2054555076f46e683151ee28a19bc |
| ui-target | canary-2 |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width