2015-03-02 12:47:44 +00:00
|
|
|
# test basic complex number functionality
|
|
|
|
|
|
|
|
# constructor
|
|
|
|
print(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 ops
|
|
|
|
print(bool(1j))
|
|
|
|
print(+(1j))
|
2015-03-14 22:56:02 +00:00
|
|
|
print(-(1 + 2j))
|
2015-03-02 12:47:44 +00:00
|
|
|
|
|
|
|
# binary ops
|
2015-03-25 23:10:09 +00:00
|
|
|
print(1j + False)
|
|
|
|
print(1j + True)
|
2015-03-02 12:47:44 +00:00
|
|
|
print(1j + 2)
|
|
|
|
print(1j + 2j)
|
|
|
|
print(1j - 2)
|
|
|
|
print(1j - 2j)
|
|
|
|
print(1j * 2)
|
|
|
|
print(1j * 2j)
|
|
|
|
print(1j / 2)
|
2015-04-04 23:16:22 +01:00
|
|
|
print((1j / 2j).real)
|
2015-03-14 22:56:02 +00:00
|
|
|
print(1j / (1 + 2j))
|
2015-04-04 23:16:22 +01:00
|
|
|
ans = 0j ** 0; print("%.5g %.5g" % (ans.real, ans.imag))
|
|
|
|
ans = 0j ** 0j; print("%.5g %.5g" % (ans.real, ans.imag))
|
2015-03-14 22:56:02 +00:00
|
|
|
ans = 1j ** 2.5; print("%.5g %.5g" % (ans.real, ans.imag))
|
|
|
|
ans = 1j ** 2.5j; print("%.5g %.5g" % (ans.real, ans.imag))
|
2015-03-02 17:21:10 +00:00
|
|
|
|
2015-04-04 23:16:22 +01:00
|
|
|
# comparison
|
|
|
|
print(1j == 1)
|
|
|
|
print(1j == 1j)
|
|
|
|
|
2015-03-02 17:21:10 +00:00
|
|
|
# builtin abs
|
|
|
|
print(abs(1j))
|
2015-03-14 22:56:02 +00:00
|
|
|
print("%.5g" % abs(1j + 2))
|
2015-03-25 23:10:09 +00:00
|
|
|
|
2015-04-04 22:05:30 +01:00
|
|
|
# float on lhs should delegate to complex
|
|
|
|
print(1.2 + 3j)
|
|
|
|
|
2015-05-13 20:35:58 +01:00
|
|
|
# check printing of inf/nan
|
|
|
|
print(float('nan') * 1j)
|
|
|
|
print(float('inf') * (1 + 1j))
|
|
|
|
print(float('-inf') * (1 + 1j))
|
|
|
|
|
2015-03-25 23:10:09 +00:00
|
|
|
# convert bignum to complex on rhs
|
|
|
|
ans = 1j + (1 << 70); print("%.5g %.5g" % (ans.real, ans.imag))
|
|
|
|
|
|
|
|
# can't convert rhs to complex
|
|
|
|
try:
|
|
|
|
1j + []
|
|
|
|
except TypeError:
|
|
|
|
print("TypeError")
|
2015-04-04 23:16:22 +01:00
|
|
|
|
|
|
|
# unsupported unary op
|
|
|
|
try:
|
|
|
|
~(1j)
|
|
|
|
except TypeError:
|
|
|
|
print("TypeError")
|
|
|
|
|
|
|
|
# unsupported binary op
|
|
|
|
try:
|
|
|
|
1j // 2
|
|
|
|
except TypeError:
|
|
|
|
print("TypeError")
|
|
|
|
|
|
|
|
# unsupported binary op
|
|
|
|
try:
|
|
|
|
1j < 2j
|
|
|
|
except TypeError:
|
|
|
|
print("TypeError")
|
|
|
|
|
|
|
|
# zero division
|
|
|
|
try:
|
|
|
|
1j / 0
|
|
|
|
except ZeroDivisionError:
|
|
|
|
print("ZeroDivisionError")
|
|
|
|
|
|
|
|
# zero division via power
|
|
|
|
try:
|
|
|
|
0j ** 1j
|
|
|
|
except ZeroDivisionError:
|
|
|
|
print("ZeroDivisionError")
|