15. Complex Numbers

Complex Type

complex types are used to work with complex numbers. Go has Go two types of complex numbers, complex64 and complex128

To create complex number, we use build in complex function.

Declaring Complex Number

Complex numbers can be declared using any of the below way

main.go
var c1 complex128 = complex(2, 3)
var c2 complex64 = complex(3, 4)
var c1 = complex(2, 3)
c1 := complex(2, 3)

When type is not specified, complex number defaults to complex128

Since complex numbers must be created using complex() function, these do not have a corresponding zero value.

Go provides two sizes of complex numbers, complex64 and complex128 . These are used to represent complex numbers.

Declaring Complex Type

We need to use in-build complex function to create complex type.

main.go
var x = complex(2, 2)
var x complex128 = complex(2,2)
var y complex64 = complex(3,4)

If no type is specified, it defaults to complex128