python复数怎么打,Python中复数的格式

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?

解决方案

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值