Unexpected NANs in complex arithmetic
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
> py> -z
> So far, nothing unexpected has occurred. But:
> py> -1*z # should be the same as -z
> And even more strange:
> py> 1*z
> Is this the right behaviour? If so, what's the justification for it?
I've no idea, so I Googled StackExchange  and found 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.
More information about the Python-list