# test basic complex number functionality# constructorprint(complex(1))print(complex(1.2))print(complex(1.2j))print(complex("1"))print(complex("1.2"))print(complex("1.2j"))print(complex(1, 2))print(complex(1j, 2j))# unary opsprint(bool(1j))print(+(1j))print(-(1 + 2j))# binary opsprint(1j + False)print(1j + True)print(1j + 2)print(1j + 2j)print(1j - 2)print(1j - 2j)print(1j * 2)print(1j * 2j)print(1j / 2)print((1j / 2j).real)print(1j / (1 + 2j))ans = 0j ** 0; print("%.5g %.5g" % (ans.real, ans.imag))ans = 0j ** 1; print("%.5g %.5g" % (ans.real, ans.imag))ans = 0j ** 0j; print("%.5g %.5g" % (ans.real, ans.imag))ans = 1j ** 2.5; print("%.5g %.5g" % (ans.real, ans.imag))ans = 1j ** 2.5j; print("%.5g %.5g" % (ans.real, ans.imag))# comparisonprint(1j == 1)print(1j == 1j)# comparison of nan is specialnan = float('nan') * 1jprint(nan == 1j)print(nan == nan)# builtin absprint(abs(1j))print("%.5g" % abs(1j + 2))# builtin hashprint(hash(1 + 0j))print(type(hash(1j)))# float on lhs should delegate to complexprint(1.2 + 3j)# negative base and fractional power should create a complexans = (-1) ** 2.3; print("%.5g %.5g" % (ans.real, ans.imag))ans = (-1.2) ** -3.4; print("%.5g %.5g" % (ans.real, ans.imag))# check printing of inf/nanprint(float('nan') * 1j)print(float('-nan') * 1j)print(float('inf') * (1 + 1j))print(float('-inf') * (1 + 1j))# can't assign to attributestry:(1j).imag = 0except AttributeError:print('AttributeError')# can't convert rhs to complextry:1j + []except TypeError:print("TypeError")# unsupported unary optry:~(1j)except TypeError:print("TypeError")# unsupported binary optry:1j // 2except TypeError:print("TypeError")# unsupported binary optry:1j < 2jexcept TypeError:print("TypeError")#small int on LHS, complex on RHS, unsupported optry:print(1 | 1j)except TypeError:print('TypeError')# zero divisiontry:1j / 0except ZeroDivisionError:print("ZeroDivisionError")# zero division via powertry:0j ** -1except ZeroDivisionError:print("ZeroDivisionError")try:0j ** 1jexcept ZeroDivisionError:print("ZeroDivisionError")
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。