Unexpected NANs in complex arithmetic

Antoon Pardon antoon.pardon at rece.vub.ac.be
Wed Jun 22 03:14:34 EDT 2016

Op 22-06-16 om 04:48 schreef Steven D'Aprano:
> I'm doing some arithmetic on complex numbers involving INFs, and getting
> unexpected NANs.
> py> INF = float('inf')
> py> z = INF + 3j
> py> z
> (inf+3j)
> py> -z
> (-inf-3j)
> So far, nothing unexpected has occurred. But:
> py> -1*z  # should be the same as -z
> (-inf+nanj)

What I remember from complex numbers is that a multiplication
with a number that has |z| = 1, is equivallent with a rotation.

So you should be able to get the polar representation of this
"number", add in the angle of -1, being π, and convert back to
the cartesian representation.

I think seen this way, the nan part makes perfect sense.

Also the multiplication of a+bj with c+dj is (ac-bd)+(ad+bc)j
With your "numbers" this gives.

  (inf*(-1) - 3*0) + (inf*0 + 3*(-1))j

Again the nan part makes perfect sense.


More information about the Python-list mailing list