2015-03-02 07:47:44 -05: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 18:56:02 -04:00
|
|
|
print(-(1 + 2j))
|
2015-03-02 07:47:44 -05:00
|
|
|
|
|
|
|
# binary ops
|
2015-03-25 19:10:09 -04:00
|
|
|
print(1j + False)
|
|
|
|
print(1j + True)
|
2015-03-02 07:47:44 -05:00
|
|
|
print(1j + 2)
|
|
|
|
print(1j + 2j)
|
|
|
|
print(1j - 2)
|
|
|
|
print(1j - 2j)
|
|
|
|
print(1j * 2)
|
|
|
|
print(1j * 2j)
|
|
|
|
print(1j / 2)
|
2015-03-14 18:56:02 -04:00
|
|
|
print(1j / (1 + 2j))
|
|
|
|
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 12:21:10 -05:00
|
|
|
|
|
|
|
# builtin abs
|
|
|
|
print(abs(1j))
|
2015-03-14 18:56:02 -04:00
|
|
|
print("%.5g" % abs(1j + 2))
|
2015-03-25 19:10:09 -04: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")
|