ZetCode

Golang imag function

last modified May 8, 2025

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

The imag function is used to extract the imaginary part of a complex number in Go. It works with both complex64 and complex128 types. The function returns a floating-point value.

In Go, imag is one of two built-in functions for complex numbers. The other is real. Together they provide access to complex number components.

Basic imag function example

The simplest use of imag extracts the imaginary part from a complex number. This example demonstrates basic imaginary part extraction.
Note: The result type matches the complex number's component type.

basic_imag.go
package main

import "fmt"

func main() {
    c1 := complex(3, 4) // 3 + 4i
    c2 := complex(1.5, -2.5) // 1.5 - 2.5i
    
    fmt.Println("Imaginary part of c1:", imag(c1))
    fmt.Println("Imaginary part of c2:", imag(c2))
    
    // Type demonstration
    fmt.Printf("Type of imag(c1): %T\n", imag(c1))
    fmt.Printf("Type of imag(c2): %T\n", imag(c2))
}

The program creates two complex numbers and extracts their imaginary parts. The output shows the floating-point values and their types.

Using imag with complex64

The imag function works with both complex64 and complex128 types. This example shows usage with the smaller complex64 type.

complex64_imag.go
package main

import "fmt"

func main() {
    var c1 complex64 = complex(1.2, 3.4)
    var c2 complex64 = complex(-5.6, 7.8)
    
    im1 := imag(c1)
    im2 := imag(c2)
    
    fmt.Println("Imaginary parts:", im1, im2)
    fmt.Printf("Types: %T, %T\n", im1, im2)
    
    sum := im1 + im2
    fmt.Println("Sum of imaginary parts:", sum)
}

The example demonstrates that imag returns float32 for complex64. We can perform arithmetic operations on the extracted imaginary parts.

Imaginary part in calculations

Extracted imaginary parts can be used in mathematical calculations. This example shows practical use of imag in computations.

calculation_imag.go
package main

import (
    "fmt"
    "math"
)

func main() {
    c := complex(3.0, 4.0) // 3 + 4i
    
    // Calculate magnitude using real and imag parts
    magnitude := math.Sqrt(math.Pow(real(c), 2) + math.Pow(imag(c), 2))
    
    // Calculate phase angle (in radians)
    phase := math.Atan2(imag(c), real(c))
    
    fmt.Printf("Complex number: %v\n", c)
    fmt.Printf("Magnitude: %.2f\n", magnitude)
    fmt.Printf("Phase angle: %.2f radians\n", phase)
}

The program calculates the magnitude and phase angle of a complex number. It uses both real and imag functions for the calculations.

Imaginary part in function returns

The imag function can be used directly in return statements. This example shows a function that returns the imaginary part of a complex number.

function_imag.go
package main

import "fmt"

func getImaginaryPart(c complex128) float64 {
    return imag(c)
}

func main() {
    numbers := []complex128{
        complex(1, 2),
        complex(0, -3),
        complex(4.5, 6.7),
    }
    
    for _, num := range numbers {
        im := getImaginaryPart(num)
        fmt.Printf("Number: %v, Imaginary part: %.1f\n", num, im)
    }
}

The getImaginaryPart function encapsulates the imag call. This makes the code more readable and reusable for complex number operations.

Comparing imaginary parts

Extracted imaginary parts can be compared like regular floating-point numbers. This example demonstrates comparison operations with imaginary parts.

compare_imag.go
package main

import (
    "fmt"
    "math"
)

func main() {
    c1 := complex(3, 4)
    c2 := complex(1, 5)
    c3 := complex(0, -4)
    
    // Compare imaginary parts
    fmt.Println("c1 imaginary > c2 imaginary:", imag(c1) > imag(c2))
    fmt.Println("c2 imaginary == 5:", imag(c2) == 5)
    fmt.Println("c3 imaginary is negative:", imag(c3) < 0)
    
    // Floating-point comparison with tolerance
    c4 := complex(2, math.Pi)
    fmt.Println("c4 imaginary ≈ π:", math.Abs(imag(c4)-math.Pi) < 0.0001)
}

The example shows various comparison operations with imaginary parts. It includes exact comparisons and floating-point comparisons with tolerance.

Source

Go language specification

This tutorial covered the imag function in Go with practical examples of complex number manipulation and imaginary part extraction.

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.