1# test basic complex number functionality
2
3# constructor
4print(complex(1))
5print(complex(1.2))
6print(complex(1.2j))
7print(complex("1"))
8print(complex("1.2"))
9print(complex("1.2j"))
10print(complex(1, 2))
11print(complex(1j, 2j))
12
13# unary ops
14print(bool(1j))
15print(+(1j))
16print(-(1 + 2j))
17
18# binary ops
19print(1j + False)
20print(1j + True)
21print(1j + 2)
22print(1j + 2j)
23print(1j - 2)
24print(1j - 2j)
25print(1j * 2)
26print(1j * 2j)
27print(1j / 2)
28print((1j / 2j).real)
29print(1j / (1 + 2j))
30ans = 0j ** 0
31print("%.5g %.5g" % (ans.real, ans.imag))
32ans = 0j ** 1
33print("%.5g %.5g" % (ans.real, ans.imag))
34ans = 0j ** 0j
35print("%.5g %.5g" % (ans.real, ans.imag))
36ans = 1j ** 2.5
37print("%.5g %.5g" % (ans.real, ans.imag))
38ans = 1j ** 2.5j
39print("%.5g %.5g" % (ans.real, ans.imag))
40
41# comparison
42print(1j == 1)
43print(1j == 1j)
44print(0 + 0j == False, 1 + 0j == True)
45print(False == 0 + 0j, True == 1 + 0j)
46
47# comparison of nan is special
48nan = float("nan") * 1j
49print(nan == 1j)
50print(nan == nan)
51
52# builtin abs
53print(abs(1j))
54print("%.5g" % abs(1j + 2))
55
56# builtin hash
57print(hash(1 + 0j))
58print(type(hash(1j)))
59
60# float on lhs should delegate to complex
61print(1.2 + 3j)
62
63# negative base and fractional power should create a complex
64ans = (-1) ** 2.3
65print("%.5g %.5g" % (ans.real, ans.imag))
66ans = (-1.2) ** -3.4
67print("%.5g %.5g" % (ans.real, ans.imag))
68
69# check printing of inf/nan
70print(float("nan") * 1j)
71print(float("-nan") * 1j)
72print(float("inf") * (1 + 1j))
73print(float("-inf") * (1 + 1j))
74
75# can't assign to attributes
76try:
77    (1j).imag = 0
78except AttributeError:
79    print("AttributeError")
80
81# can't convert rhs to complex
82try:
83    1j + []
84except TypeError:
85    print("TypeError")
86
87# unsupported unary op
88try:
89    ~(1j)
90except TypeError:
91    print("TypeError")
92
93# unsupported binary op
94try:
95    1j // 2
96except TypeError:
97    print("TypeError")
98
99# unsupported binary op
100try:
101    1j < 2j
102except TypeError:
103    print("TypeError")
104
105# small int on LHS, complex on RHS, unsupported op
106try:
107    print(1 | 1j)
108except TypeError:
109    print("TypeError")
110
111# zero division
112try:
113    1j / 0
114except ZeroDivisionError:
115    print("ZeroDivisionError")
116
117# zero division via power
118try:
119    0j ** -1
120except ZeroDivisionError:
121    print("ZeroDivisionError")
122try:
123    0j ** 1j
124except ZeroDivisionError:
125    print("ZeroDivisionError")
126