Format of complex number in Python

cxxl picture cxxl · Nov 14, 2012 · Viewed 15.5k times · Source

I am wondering about the way Python (3.3.0) prints complex numbers. I am looking for an explanation, not a way to change the print.

Example:

>>> complex(1,1)-complex(1,1)
0j

Why doesn't it just print "0"? My guess is: to keep the output of type complex.

Next example:

>>> complex(0,1)*-1
(-0-1j)

Well, a simple "-1j" or "(-1j)" would have done. And why "-0"?? Isn't that the same as +0? It doesn't seem to be a rounding problem:

>>> (complex(0,1)*-1).real == 0.0
True

And when the imaginary part gets positive, the -0 vanishes:

>>> complex(0,1)
1j
>>> complex(0,1)*-1
(-0-1j)
>>> complex(0,1)*-1*-1
1j

Yet another example:

>>> complex(0,1)*complex(0,1)*-1
(1-0j)
>>> complex(0,1)*complex(0,1)*-1*-1
(-1+0j)
>>> (complex(0,1)*complex(0,1)*-1).imag
-0.0

Am I missing something here?

Answer

Thomas picture Thomas · Nov 14, 2012

It prints 0j to indicate that it's still a complex value. You can also type it back in that way:

>>> 0j
0j

The rest is probably the result of the magic of IEEE 754 floating point representation, which makes a distinction between 0 and -0, the so-called signed zero. Basically, there's a single bit that says whether the number is positive or negative, regardless of whether the number happens to be zero. This explains why 1j * -1 gives something with a negative zero real part: the positive zero got multiplied by -1.

-0 is required by the standard to compare equal to +0, which explains why (1j * -1).real == 0.0 still holds.

The reason that Python still decides to print the -0, is that in the complex world these make a difference for branch cuts, for instance in the phase function:

>>> phase(complex(-1.0, 0.0))
3.141592653589793
>>> phase(complex(-1.0, -0.0))
-3.141592653589793

This is about the imaginary part, not the real part, but it's easy to imagine situations where the sign of the real part would make a similar difference.