gh-117999: Fix small integer powers of complex numbers by serhiy-storchaka · Pull Request #124243 · python/cpython (original) (raw)

the result ((inf+nanj)) is still incorrect -- it should be (inf+0j), as for smaller exponents.

Not necessary, it's correct in C (no mixed-mode rules for cpow()). BTW, for small exponents we also got (inf+nanj) with this patch (even with n=2).

You are right, (inf+0j) seems to be better. But only iff we will use mixed-mode arithmetic rules for exponentiation, i.e. z**real == cmath.exp(real*cmath.log(z)) (where multiplication taken component-wise):

cmath.log(complex('inf')) (inf+0j) cmath.exp(cmath.log(complex('inf'))*2) (inf+0j)

Unfortunately, it's not easy to fix integer algorithm in such case.

I think the better road would be along #123283. We have two variants.

  1. Drop algorithm for integer exponents. That makes complex math in Python more C-compatible.
  2. Add a version for z**real, that will also handle cases when z has special components if we kept also integer algorithm.