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.

complex128 is a built-in numeric type in Go that represents a complex number using 128 bits of memory. It is composed of two 64-bit IEEE 754 floating-point numbers (float64): one representing the real component and the other representing the imaginary component.

Memory Layout and Precision

  • Total Size: 16 bytes (128 bits).
  • Real Part: 8 bytes (float64).
  • Imaginary Part: 8 bytes (float64).
  • Zero Value: 0 + 0i (or simply 0).

Declaration and Initialization

You can initialize a complex128 variable using either the built-in complex() function or the imaginary literal suffix i.
package main

import "fmt"

func main() {
    // Method 1: Using the built-in complex() function
    // Signature: func complex(r, i FloatType) ComplexType
    var a complex128 = complex(5.5, 3.2)

    // Method 2: Using literal syntax with the 'i' suffix
    var b complex128 = 5.5 + 3.2i

    // Method 3: Type inference (Go defaults to complex128 for complex literals)
    c := 5.5 + 3.2i

    fmt.Printf("a: %v\n", a)
    fmt.Printf("b: %v\n", b)
    fmt.Printf("Type: %T, Value: %v\n", c, c) 
    // Output: Type: complex128, Value: (5.5+3.2i)
}

Component Extraction

Go provides built-in functions to extract the real and imaginary float64 components from a complex128 value.
package main

import "fmt"

func main() {
    c := 7.1 + 4.9i

    // Extract the real part
    // Signature: func real(c ComplexType) FloatType
    r := real(c) // Type: float64, Value: 7.1

    // Extract the imaginary part
    // Signature: func imag(c ComplexType) FloatType
    i := imag(c) // Type: float64, Value: 4.9

    fmt.Printf("Real: %f, Imaginary: %f\n", r, i)
}

Arithmetic Operations

complex128 supports standard arithmetic operators. Operations are performed on both the real and imaginary planes according to standard complex arithmetic rules.
package main

import "fmt"

func main() {
    c1 := 2.0 + 3.0i
    c2 := 1.0 + 2.0i

    add := c1 + c2 // (3.0+5.0i)
    sub := c1 - c2 // (1.0+1.0i)
    mul := c1 * c2 // (-4.0+7.0i)
    div := c1 / c2 // (1.6-0.2i)

    fmt.Printf("Addition: %v\n", add)
    fmt.Printf("Subtraction: %v\n", sub)
    fmt.Printf("Multiplication: %v\n", mul)
    fmt.Printf("Division: %v\n", div)
}

The math/cmplx Package

For advanced mathematical operations, Go provides the math/cmplx standard library package. This package is specifically designed to operate exclusively on complex128 values (it does not support complex64).
package main

import (
    "fmt"
    "math/cmplx"
)

func main() {
    c := 2.0 + 3.0i
    
    // Returns the modulus/magnitude (float64)
    abs := cmplx.Abs(c)   
    
    // Returns the complex conjugate (complex128)
    conj := cmplx.Conj(c) 
    
    // Returns the phase/argument (float64)
    phase := cmplx.Phase(c) 

    fmt.Printf("Absolute value: %f\n", abs)
    fmt.Printf("Conjugate: %v\n", conj)
    fmt.Printf("Phase: %f\n", phase)
}
Master Go with Deep Grasping Methodology!Learn More