complex128 in Go



In this tutorial, we will learn about the complex128 data type in Go. We will cover the basics of defining and using complex numbers, including how to perform arithmetic operations and access the real and imaginary parts.


Understanding the Complex128 Data Type in Go

The complex128 data type in Go is used to represent complex numbers, which have both real and imaginary parts. Each part is a float64 value.


Defining a Complex Number

Complex numbers can be defined using the complex function or by directly assigning values to the real and imaginary parts.

var c1 complex128 = complex(3, 4)
c2 := 2 + 3i

Accessing Real and Imaginary Parts

The real and imaginary parts of a complex number can be accessed using the real and imag functions, respectively.

r := real(c1)
i := imag(c1)

Performing Arithmetic Operations

Arithmetic operations such as addition, subtraction, multiplication, and division can be performed on complex numbers.

sum := c1 + c2
difference := c1 - c2
product := c1 * c2
quotient := c1 / c2


Example 1: Defining and using Complex Numbers

We can define and use complex numbers in Go to represent values with real and imaginary parts.

For example,

  1. Define a complex number named c1 using the complex function.
  2. Define another complex number named c2 using a direct assignment.
  3. Print both complex numbers to the console.

Go Program

package main
import "fmt"
func main() {
    var c1 complex128 = complex(3, 4)
    c2 := 2 + 3i
    fmt.Println(c1)
    fmt.Println(c2)
}

Output

(3+4i)
(2+3i)


Example 2: Accessing Real and Imaginary Parts

We can access the real and imaginary parts of a complex number in Go using the real and imag functions.

For example,

  1. Define a complex number named c.
  2. Use the real and imag functions to extract the real and imaginary parts.
  3. Print the real and imaginary parts to the console.

Go Program

package main
import "fmt"
func main() {
    c := 3 + 4i
    r := real(c)
    i := imag(c)
    fmt.Printf("Real part: %f, Imaginary part: %f\n", r, i)
}

Output

Real part: 3.000000, Imaginary part: 4.000000


Example 3: Performing Arithmetic Operations

We can perform arithmetic operations on complex numbers in Go.

For example,

  1. Define two complex numbers named c1 and c2.
  2. Perform addition, subtraction, multiplication, and division on these complex numbers.
  3. Print the results of these operations to the console.

Go Program

package main
import "fmt"
func main() {
    c1 := 3 + 4i
    c2 := 1 + 2i
    sum := c1 + c2
    difference := c1 - c2
    product := c1 * c2
    quotient := c1 / c2
    fmt.Println("Sum:", sum)
    fmt.Println("Difference:", difference)
    fmt.Println("Product:", product)
    fmt.Println("Quotient:", quotient)
}

Output

Sum: (4+6i)
Difference: (2+2i)
Product: (-5+10i)
Quotient: (2.2-0.4i)


Example 4: Using Complex Numbers in Functions

We can pass complex numbers to functions and return complex numbers from functions in Go.

For example,

  1. Define a function named conjugate that takes a complex number and returns its conjugate.
  2. Call the function with a complex number and print the result to the console.

Go Program

package main
import "fmt"
func conjugate(c complex128) complex128 {
    return complex(real(c), -imag(c))
}
func main() {
    c := 3 + 4i
    fmt.Println("Original:", c)
    fmt.Println("Conjugate:", conjugate(c))
}

Output

Original: (3+4i)
Conjugate: (3-4i)