Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

complex64 is a built-in numeric data type in Go that represents a complex number using 64 bits of memory. It is composed of two 32-bit floating-point numbers (float32): one representing the real component and the other representing the imaginary component. Both underlying components adhere to the IEEE 754 standard for floating-point arithmetic.

Memory Layout

  • Total Size: 8 bytes (64 bits)
  • Real Part: 4 bytes (float32)
  • Imaginary Part: 4 bytes (float32)

Declaration and Initialization

You can initialize a complex64 variable using the built-in complex() function or by using complex literal syntax with the i suffix. Because Go’s untyped complex literals default to complex128, you must explicitly declare the type when creating a complex64 using shorthand syntax.
// Method 1: Using the built-in complex() function.
// The untyped floating-point constants yield an untyped complex constant,
// which is implicitly converted to complex64 by the variable assignment context.
var c1 complex64 = complex(2.5, 3.1)

// Method 2: Using literal notation with explicit type declaration.
var c2 complex64 = 2.5 + 3.1i

// Method 3: Using short variable declaration with type conversion.
c3 := complex64(2.5 + 3.1i)

Component Extraction

Go provides two built-in functions, real() and imag(), to extract the respective components of a complex number. When applied to a complex64 value, both functions return a float32.
var c complex64 = 4.2 - 1.5i

// Extracting the real component
var r float32 = real(c) // r == 4.2

// Extracting the imaginary component
var im float32 = imag(c) // im == -1.5

Arithmetic and Comparison Operations

complex64 supports standard arithmetic operators. Operations are performed on both the real and imaginary parts simultaneously according to the mathematical rules of complex arithmetic.
a := complex64(1.0 + 2.0i)
b := complex64(3.0 + 4.0i)

sum := a + b        // 4.0 + 6.0i
difference := a - b // -2.0 - 2.0i
product := a * b    // -5.0 + 10.0i
quotient := a / b   // 0.44 + 0.08i
You can compare complex64 values using equality operators (== and !=). Two complex64 values are strictly equal if and only if both their real and imaginary float32 components are exactly equal. Inequality operators (<, <=, >, >=) are not defined for complex types in Go.

Standard Library Compatibility

The Go standard library’s math/cmplx package exclusively operates on complex128 values. To perform advanced mathematical operations (such as calculating the absolute value or square root) on a complex64 variable, you must explicitly convert it to complex128 before passing it to the function.
package main

import (
    "fmt"
    "math/cmplx"
)

func main() {
    var c complex64 = 3.0 + 4.0i

    // Explicit conversion to complex128 is required for math/cmplx functions.
    // The result is cast back to float32 to match the precision of complex64.
    magnitude := float32(cmplx.Abs(complex128(c))) 
    
    fmt.Println(magnitude) // Output: 5
}
Master Go with Deep Grasping Methodology!Learn More