Unexpected NANs in complex arithmetic

Dan Sommers dan at tombstonezero.net
Tue Jun 21 23:54:27 EDT 2016

On Wed, 22 Jun 2016 12:57:55 +1000, Chris Angelico wrote:

> 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)


>> 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.

By the time Python returns a result for inf+3j, you're already in
trouble (or perhaps Python is already in trouble).  A complex number has
a real part and an imaginary part, and inf isn't real (i.e., it's not an
element of ℝ).

More information about the Python-list mailing list