Format of complex number in Python

后端 未结 4 1631
清歌不尽
清歌不尽 2020-12-18 19:13

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:

>>         


        
4条回答
  •  余生分开走
    2020-12-18 19:54

    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.

提交回复
热议问题