ComplexType
type in GolangThe ComplexType
type in Golang documentation is a placeholder for two complex number data types:
complex64
complex128
Both these data types take 2 floating point literals, one for the real part and one for the imaginary part. complex64
takes 2 float32
literals, whereas complex128
takes 2 float64
literals.
ComplexType
typeThe following code demonstrates the use of ComplexType
:
package mainimport "fmt"func main() {var a complex64 = complex(1, 2)var b complex64 = 2 + 4ivar c complex64 = b - afmt.Println(a)fmt.Println(b)fmt.Println(c)}
In the example above, the variables a
, b
, and c
are of complex64
type (which is a ComplexType
). The variable a
is created using a built-in complex
function which has the following syntax:
func complex(real, imaginary FloatType) ComplexType
The above function takes in 2 floating point numbers (real and imaginary parts) and returns the complex number.
The variable b
is created by writing 2 + 4i
, just like how you write complex numbers in math. The variable c
is created by subtracting a
from b
, which results in 1 + 2i
.
Note: Two variables, one with
complex64
type and the other withcomplex128
type, cannot be arithmetically compared with each other.
Free Resources