ZetCode

Golang complex function

last modified May 8, 2025

This tutorial explains how to use the complex built-in function in Go. We'll cover complex number basics with practical examples of complex operations.

The complex function constructs a complex value from two floating-point numbers. In Go, complex numbers are a first-class type with built-in support.

Go provides two complex number types: complex64 and complex128. These correspond to 32-bit and 64-bit floating-point components respectively.

Basic complex number creation

The simplest use of complex creates a complex number from two floats. This example demonstrates basic complex number construction.
Note: Both arguments must be of the same floating-point type.

basic_complex.go
package main

import "fmt"

func main() {
    // Create complex128 numbers
    a := complex(3.5, 2.1)
    b := complex(1.2, 4.7)
    
    fmt.Println("a =", a)
    fmt.Println("b =", b)
    
    // Perform basic arithmetic
    sum := a + b
    product := a * b
    
    fmt.Println("Sum:", sum)
    fmt.Println("Product:", product)
}

The complex function combines two floats into a complex number. Go supports arithmetic operations directly on complex numbers.

Extracting real and imaginary parts

We can extract the real and imaginary components using real and imag functions. This example shows component extraction.

components.go
package main

import (
    "fmt"
    "math"
)

func main() {
    c := complex(4.2, 5.3)
    
    r := real(c)
    i := imag(c)
    
    fmt.Printf("Complex: %.2f\n", c)
    fmt.Printf("Real part: %.2f\n", r)
    fmt.Printf("Imaginary part: %.2f\n", i)
    
    // Calculate magnitude
    magnitude := math.Hypot(r, i)
    fmt.Printf("Magnitude: %.2f\n", magnitude)
}

The real and imag functions extract components. math.Hypot calculates the magnitude of the complex number.

Complex64 vs complex128

Go provides two complex number types with different precision levels. This example demonstrates the difference between them.

precision.go
package main

import "fmt"

func main() {
    // complex64 uses float32 components
    c64 := complex(float32(1.23456789), float32(9.87654321))
    
    // complex128 uses float64 components
    c128 := complex(1.23456789, 9.87654321)
    
    fmt.Println("complex64:", c64)
    fmt.Println("complex128:", c128)
    
    // Precision difference
    fmt.Printf("complex64 real part: %.15f\n", real(c64))
    fmt.Printf("complex128 real part: %.15f\n", real(c128))
}

complex64 has less precision than complex128. The output shows the difference in floating-point component precision.

Complex number operations

Go supports various mathematical operations with complex numbers. This example demonstrates common complex number operations.

operations.go
package main

import (
    "fmt"
    "math/cmplx"
)

func main() {
    a := complex(3, 4)
    b := complex(1, 2)
    
    // Basic arithmetic
    fmt.Println("a + b =", a+b)
    fmt.Println("a - b =", a-b)
    fmt.Println("a * b =", a*b)
    fmt.Println("a / b =", a/b)
    
    // Math functions
    fmt.Println("Conjugate of a:", cmplx.Conj(a))
    fmt.Println("Phase (angle) of a:", cmplx.Phase(a))
    fmt.Println("Square root of a:", cmplx.Sqrt(a))
}

The cmplx package provides advanced complex number functions. Basic arithmetic operations work directly with complex numbers.

Practical application: FFT example

Complex numbers are essential in signal processing. This example shows a simple Fast Fourier Transform (FFT) using complex numbers.

fft_example.go
package main

import (
    "fmt"
    "math"
    "math/cmplx"
)

func simpleFFT(input []float64) []complex128 {
    n := len(input)
    output := make([]complex128, n)
    
    for k := 0; k < n; k++ {
        var sum complex128
        for t := 0; t < n; t++ {
            angle := -2 * math.Pi * float64(k) * float64(t) / float64(n)
            sum += complex(input[t]*math.Cos(angle), input[t]*math.Sin(angle))
        }
        output[k] = sum
    }
    return output
}

func main() {
    signal := []float64{1, 2, 1, 2, 1, 2, 1, 2}
    spectrum := simpleFFT(signal)
    
    fmt.Println("Input signal:", signal)
    fmt.Println("FFT result:")
    for i, val := range spectrum {
        fmt.Printf("Bin %d: %.2f (magnitude %.2f)\n", 
            i, val, cmplx.Abs(val))
    }
}

This simplified FFT implementation demonstrates complex number usage. The result shows frequency components as complex numbers with magnitudes.

Source

Go language specification

This tutorial covered the complex function in Go with practical examples of complex number creation and operations.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all Golang tutorials.