ZetCode

Golang complex64 Type

last modified May 8, 2025

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

The complex64 type represents complex numbers with float32 real and imaginary parts. It's one of Go's built-in numeric types for complex arithmetic.

In Go, complex64 provides efficient storage for complex numbers. Operations on complex64 values use 32-bit floating-point precision for both components.

Basic complex64 creation

The simplest way to create complex64 values uses the complex function. This example demonstrates basic complex number creation.
Note: The imaginary part is denoted with 'i' suffix.

basic_complex.go
package main

import "fmt"

func main() {

    // Create complex numbers
    a := complex(3.0, 4.0) // 3 + 4i
    b := complex(1.5, -2.5) // 1.5 - 2.5i
    
    fmt.Printf("a = %v (type %T)\n", a, a)
    fmt.Printf("b = %v (type %T)\n", b, b)
    
    // Access real and imaginary parts
    fmt.Println("Real part of a:", real(a))
    fmt.Println("Imaginary part of a:", imag(a))
}

The complex function creates complex64 values when given float32 arguments. The real and imag functions extract parts.

Complex arithmetic operations

Complex64 values support standard arithmetic operations. This example shows basic arithmetic with complex numbers.

complex_arithmetic.go
package main

import "fmt"

func main() {

    x := complex(2.0, 3.0)
    y := complex(1.0, -1.0)
    
    // Addition
    sum := x + y
    fmt.Println("Sum:", sum) // (3+2i)
    
    // Subtraction
    diff := x - y
    fmt.Println("Difference:", diff) // (1+4i)
    
    // Multiplication
    product := x * y
    fmt.Println("Product:", product) // (5+1i)
    
    // Division
    quotient := x / y
    fmt.Println("Quotient:", quotient) // (-0.5+2.5i)
}

Complex arithmetic follows standard mathematical rules. The operations work component-wise while respecting the imaginary unit properties.

Complex conjugate and magnitude

Common complex number operations include conjugates and magnitudes. This example shows how to compute these properties.

complex_properties.go
package main

import (
    "fmt"
    "math"
)

func magnitude(c complex64) float32 {
    r := real(c)
    i := imag(c)
    return float32(math.Sqrt(float64(r*r + i*i)))
}

func main() {

    z := complex(4.0, 3.0)
    
    // Complex conjugate
    conjugate := complex(real(z), -imag(z))
    fmt.Println("Conjugate:", conjugate) // (4-3i)
    
    // Magnitude (absolute value)
    fmt.Println("Magnitude:", magnitude(z)) // 5
    
    // Built-in complex64 operations
    fmt.Println("Built-in magnitude:", cmplx.Abs(z))
}

The conjugate changes the sign of the imaginary part. The magnitude is computed using the Pythagorean theorem. Go's math/cmplx package provides these operations.

Using complex64 in functions

Complex64 values can be passed to and returned from functions. This example demonstrates function usage with complex numbers.

complex_functions.go
package main

import (
    "fmt"
    "math/cmplx"
)

func rotate(c complex64, angle float32) complex64 {
    // Convert angle to radians
    rad := complex(0, float32(cmplx.Pi)*angle/180)
    return c * cmplx.Exp(rad)
}

func main() {

    point := complex(1.0, 0.0)
    
    // Rotate 90 degrees
    rotated := rotate(point, 90)
    fmt.Println("After 90° rotation:", rotated) // ~(0+1i)
    
    // Rotate another 90 degrees
    rotated = rotate(rotated, 90)
    fmt.Println("After 180° rotation:", rotated) // ~(-1+0i)
}

The rotate function rotates a complex number by a given angle. Complex numbers naturally represent 2D points and rotations.

Complex64 in signal processing

Complex numbers are fundamental in signal processing. This example shows a basic Fourier transform simulation.

signal_processing.go
package main

import (
    "fmt"
    "math"
)

func dft(signal []complex64) []complex64 {
    N := len(signal)
    spectrum := make([]complex64, N)
    
    for k := 0; k < N; k++ {
        var sum complex64
        for n := 0; n < N; n++ {
            angle := -2 * math.Pi * float64(k*n) / float64(N)
            c := complex(float32(math.Cos(angle)), float32(math.Sin(angle)))
            sum += signal[n] * c
        }
        spectrum[k] = sum
    }
    return spectrum
}

func main() {

    // Create a simple signal (sine wave)
    signal := make([]complex64, 8)
    for i := range signal {
        signal[i] = complex(float32(math.Sin(2*math.Pi*float64(i)/8)), 0)
    }
    
    // Compute DFT
    spectrum := dft(signal)
    
    fmt.Println("Signal:", signal)
    fmt.Println("Spectrum:", spectrum)
}

This discrete Fourier transform implementation uses complex64 for efficient storage. Each frequency component is represented as a complex number.

Source

Go language specification

This tutorial covered the complex64 type in Go with practical examples of complex number operations and applications.

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.