Complex Numbers

Initializing Complex Numbers

There are two complex types in XGo. The complex64 and the complex128. Initializing complex numbers is really easy. You can use the constructor or the initialization syntax as well.

c1 := complex(10, 11) // constructor init c2 := 10 + 11i // complex number init syntax println c1, c2

Parts of a Complex Number

There are two parts of a complex number. The real and imaginary part. We use functions to get those.

cc := complex(23, 31) realPart := real(cc) // gets real part imagPart := imag(cc) // gets imaginary part println realPart, imagPart

Operations on a Complex Number

A complex variable can do any operation like addition, subtraction, multiplication, and division.

Let’s see an example to perform mathematical operations on the complex numbers.

c11 := complex(10, 11) // constructor init c22 := 10 + 11i // complex number init syntax ccc := complex(2, 3) cc2 := 4 + 5i // complex initializer syntax a + ib cc3 := c11 + c22 // addition just like other variables println "Add: ", cc3 // prints "Add: (6+8i)" re := real(cc3) // get real part im := imag(cc3) // get imaginary part println ccc, cc2, re, im // prints 6 8

Next example: Booleans