Title: Specialize complex power for integer/real exponents · Issue #137746 · python/cpython · GitHub
Open Graph Title: Specialize complex power for integer/real exponents · Issue #137746 · python/cpython
X Title: Specialize complex power for integer/real exponents · Issue #137746 · python/cpython
Description: Feature or enhancement Proposal: With #69639 complex arithmetic in Python was extended to support C-like mixed-more rules, where one operand is a float or an int. However, this doesn't affect exponentiation. Currently we have, for exampl...
Open Graph Description: Feature or enhancement Proposal: With #69639 complex arithmetic in Python was extended to support C-like mixed-more rules, where one operand is a float or an int. However, this doesn't affect expon...
X Description: Feature or enhancement Proposal: With #69639 complex arithmetic in Python was extended to support C-like mixed-more rules, where one operand is a float or an int. However, this doesn't affect e...
Opengraph URL: https://github.com/python/cpython/issues/137746
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Specialize complex power for integer/real exponents","articleBody":"# Feature or enhancement\n\n### Proposal:\n\nWith #69639 complex arithmetic in Python was extended to support C-like mixed-more rules, where one operand is a float or an int. However, this doesn't affect exponentiation.\n\nCurrently we have, for example:\n```pycon\n\u003e\u003e\u003e import cmath\n\u003e\u003e\u003e z = complex('inf')\n\u003e\u003e\u003e z**0.5\nTraceback (most recent call last):\n File \"\u003cpython-input-2\u003e\", line 1, in \u003cmodule\u003e\n z**0.5\n ~^^~~~\nOverflowError: complex exponentiation\n\u003e\u003e\u003e cmath.sqrt(z)\n(inf+0j)\n\u003e\u003e\u003e z**2\nTraceback (most recent call last):\n File \"\u003cpython-input-4\u003e\", line 1, in \u003cmodule\u003e\n z**2\n ~^^~\nOverflowError: complex exponentiation\n```\nwhich is just meaningless. Note also, that plain multiplication is not too much better in the last case:\n```pycon\n\u003e\u003e\u003e z*z\n(inf+nanj)\n```\nWhat we actually want here is ``exp(log(z)*real_exponent)``:\n```pycon\n\u003e\u003e\u003e import cmath, math\n\u003e\u003e\u003e def mypow(b, e):\n... if isinstance(b, (float, int)) and b \u003e 0:\n... if isinstance(e, (float, int)):\n... return math.exp(e*math.log(b))\n... return cmath.exp(e*math.log(b))\n... return cmath.exp(e*cmath.log(b))\n... \n\u003e\u003e\u003e mypow(z, 2)\n(inf+0j)\n\u003e\u003e\u003e mypow(z+123j, 2)\n(inf+0j)\n```\nThis works as expected for real exponents and/or positive bases due to special mixed-mode rules in complex arithmetic (this time - in multiplication).\n\nSpecial rules for exponentiation will be useful for implementation of complex functions, using known analytic identities. For example:\n```pycon\n\u003e\u003e\u003e cmath.asinh(z)\n(inf+0j)\n\u003e\u003e\u003e myasinh_bad = lambda z: cmath.log(z + cmath.sqrt(1 + z**2))\n\u003e\u003e\u003e myasinh_bad(z)\nTraceback (most recent call last):\n File \"\u003cpython-input-11\u003e\", line 1, in \u003cmodule\u003e\n myasinh_bad(z)\n ~~~~~~~~~~~^^^\n File \"\u003cpython-input-10\u003e\", line 1, in \u003clambda\u003e\n myasinh_bad = lambda z: cmath.log(z + cmath.sqrt(1 + z**2))\n ~^^~\nOverflowError: complex exponentiation\n\u003e\u003e\u003e myasinh_bad2 = lambda z: cmath.log(z + cmath.sqrt(1 + z*z))\n\u003e\u003e\u003e myasinh_bad2(z)\n(inf+nanj)\n\u003e\u003e\u003e myasinh_good = lambda z: cmath.log(z + cmath.sqrt(1 + mypow(z, 2)))\n\u003e\u003e\u003e myasinh_good(z)\n(inf+0j)\n```\n\n### Has this already been discussed elsewhere?\n\nNo response given\n\n### Links to previous discussion of this feature:\n\n_No response_","author":{"url":"https://github.com/skirpichev","@type":"Person","name":"skirpichev"},"datePublished":"2025-08-14T03:50:57.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":2},"url":"https://github.com/137746/cpython/issues/137746"}
| 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:86d1d178-7d0b-6f28-58e9-abd2ba756d8d |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C90A:305A31:2D76CE:3ACF1E:696B3B0E |
| html-safe-nonce | eb1a6a4fd19be3521f438feabda5459ca8684fcd2ac7cc27aba5cca6d711396e |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDOTBBOjMwNUEzMToyRDc2Q0U6M0FDRjFFOjY5NkIzQjBFIiwidmlzaXRvcl9pZCI6IjEzMDgyNjc2NzMxNTMxMjUxMzQiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 53f60df41cd9e3035655652c7b5801a857b55cc5b1c33b25d4c8dc71e1ffcb21 |
| hovercard-subject-tag | issue:3320632474 |
| 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/137746/issue_layout |
| twitter:image | https://opengraph.githubassets.com/242398b499255a7946d00b830c14ec9cafb404a08d1ba13bcd971d6521501209/python/cpython/issues/137746 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/242398b499255a7946d00b830c14ec9cafb404a08d1ba13bcd971d6521501209/python/cpython/issues/137746 |
| og:image:alt | Feature or enhancement Proposal: With #69639 complex arithmetic in Python was extended to support C-like mixed-more rules, where one operand is a float or an int. However, this doesn't affect expon... |
| 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 | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width