Golang Tutorials - Complex Types Numbers Guide With Examples

Learn about Complex Numbers types in the Go language with examples.

Golang Complex Type Numbers

Golang provides Inbuilt data types for numerics, strings, and other types. Numeric are int, uint, float, and complex numbers. Complex Numbers contain two parts - real numbers and imaginary numbers, for example, a complex number is 12.8i. In Golang, Complex numbers are represented in two types.

  • complex64 type: this type represent float32 real and imaginary data.
  • complex128 type : this type represent float64 real and imaginary data

How do we create a Complex number in Golang?

There are 2 ways to create a Complex number of ways in Golang. Using Inbuilt function -

Golang Provides an Inbuilt Complex function to create real and imaginary data.

Here is a complex function syntax

func complex(real, imaginary FloatType) ComplexType

It takes real and imaginary floating types - floats 32 or floats 64. Complex type returns Complex,complex64, and complex128 types These parameter types should be always the same.

If real and imaginary numbers of float32 are supplied, returned Complex32 type if real and imaginary types of type float64 are supplied, the function always returns Complex64 type

Using Shorthand assignment Syntax It is another way to create Complex numbers using shorthand assignment syntax.

complexData := 1 + 4i

Golang Complex Number Type example

In the below example, You will learn the following things.

  • Create a Complex number using the Inbuilt Complex function
  • Declare and assign Complex Number Variable with shorthand syntax
  • Printing Complex numbers using the Println function
  • Print Imaginary Data part using imag function
  • Print the real Data part using a real function

package main

import (
 "fmt"
)

func main() {
 complex1 := complex(10, 4)
 complex2 := 1 + 4i
 fmt.Println("complex Number 1", complex1)
 fmt.Println("complex Number 2:", complex2)
 fmt.Println("Real Number 1:", real(complex1))
 fmt.Println("Imaginary Number 1:", imag(complex1))
 fmt.Println("Real Number 1:", real(complex2))
 fmt.Println("Imaginary Number 2:", imag(complex2))
}

When the above program compiles, the Output is

complex Number 1 (10+4i)
complex Number 2: (1+4i)
Real Number 1: 10
Imaginary Number 1: 4
Real Number 1: 10
Imaginary Number 2: 4

Example program - Arithmetic Operations with Complex Numbers

It is an example of having arithmetic operations - add, subtract, divide, Multiplication on Complex Numbers When you are doing add operations with complex numbers, the Real part adds separately, Imaginary part adds separately. The same works for other operations. Below is an example of adding, subtract, multiply, and divide operations on complex numbers.

package main
import (
 "fmt"
)

func main() {
 complex1 := complex(10, 4)
 complex2 := 1 + 4i
 fmt.Println("Addition ", complex1+complex2)
 fmt.Println("Subtraction:", complex1-complex2)
 fmt.Println("Division ", complex1/complex2)
 fmt.Println("Multiplication:", complex1*complex2)
}

Output is

Addition  (11+8i)
Subtraction: (9+0i)
Division  (1.5294117647058822-2.1176470588235294i)
Multiplication: (-6+44i)