Exercise: imaginary numbers - complex numbers


Create a class that will represent imaginary numbers (x, y*i) and has methods to add and multiply two imaginary numbers.


The math:

z1 = (x1 + y1*i)
z2 = (x2 + y2*i)
z1+z2 = (x1 + x2  + (y1 + y2)*i)

z1*z2 = x1*y1 + x2*y2*i*i + x1*y2*i + x2*y1*i

Add operator overloading so we can really write code like:


z1 = Z(2, 3)
z2 = Z(4, 7)

zz = z1*z2


examples/advanced/complex_numbers.py
z = complex(2, 3)
print(z)
print(z.real)
print(z.imag)

imag = (-1) ** 0.5
print(imag)

i = complex(0, 1)
print(i)
print(i ** 2)

(2+3j)
2.0
3.0
(6.123233995736766e-17+1j)
1j
(-1+0j)