Unexpected NANs in complex arithmetic

Chris Angelico rosuav at gmail.com
Tue Jun 21 22:57:55 EDT 2016


On Wed, Jun 22, 2016 at 12:48 PM, Steven D'Aprano <steve at pearwood.info> wrote:
> 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)
>
>
> And even more strange:
>
> py> 1*z
> (inf+nanj)
>
>
>
> Is this the right behaviour? If so, what's the justification for it?

I've no idea, so I Googled StackExchange [1] and found this:

http://math.stackexchange.com/questions/585766/what-is-infinity-in-complex-plane-and-what-are-operation-with-infinity-extended

Notably this:

"""
To some extent, +∞+∞ and −∞−∞ also play this role on the real axis:
they are not a destination, they are road signs that tell us to go in
a certain direction and never stop.
"""

So when your real part is float("inf"), what you're really saying is
"Go as far as you possibly can in the positive direction, then keep
going (because you haven't run out of numbers yet), and tell me, what
is 1*z tending towards?". Infinity isn't a number, and the imaginary
part of your complex number isn't really a factor in figuring out
where you're likely to end up with your multiplication. I guess that's
a justification for it coming out as NaN.

ChrisA

[1] http://www.theallium.com/engineering/computer-programming-to-be-officially-renamed-googling-stackoverflow/


More information about the Python-list mailing list