Golang ComplexType
last modified May 8, 2025
This tutorial explains how to use the ComplexType
built-in type in Go.
We'll cover complex number basics with practical examples of complex operations.
The ComplexType in Go represents complex numbers with float components.
Go provides two complex types: complex64
and complex128
.
These correspond to 32-bit and 64-bit floating-point components respectively.
Complex numbers are useful in scientific computing, signal processing, and graphics. Go provides built-in functions for creating and manipulating complex numbers. The real and imaginary parts can be accessed separately when needed.
Basic complex number creation
The simplest way to create complex numbers uses the complex
function.
This example demonstrates basic complex number creation and component access.
Note: Complex literals use the format (a+bi)
.
package main import "fmt" func main() { // Create complex numbers a := complex(2, 3) // 2 + 3i b := 4 + 5i // Literal syntax // Access components realA := real(a) imagA := imag(a) fmt.Println("a =", a) fmt.Println("b =", b) fmt.Printf("Real part of a: %.1f\n", realA) fmt.Printf("Imaginary part of a: %.1f\n", imagA) }
The complex
function creates a complex number from two floats.
The real and imaginary parts can be accessed with real
and imag
.
Complex literals provide a convenient syntax for initialization.
Complex arithmetic operations
Complex numbers support standard arithmetic operations like addition and multiplication. This example shows basic arithmetic with complex numbers.
package main import "fmt" func main() { a := 3 + 4i b := 1 + 2i // Arithmetic operations sum := a + b difference := a - b product := a * b quotient := a / b fmt.Println("a + b =", sum) fmt.Println("a - b =", difference) fmt.Println("a * b =", product) fmt.Println("a / b =", quotient) }
Complex arithmetic follows standard mathematical rules for complex numbers. Multiplication and division use the formula (a+bi)(c+di) = (ac-bd)+(ad+bc)i. The results are automatically promoted to the larger type if mixed.
Complex number comparison
Complex numbers can be compared for equality but not ordered. This example demonstrates comparison operations with complex numbers.
package main import "fmt" func main() { a := 3.0 + 4i b := 3.0 + 4i c := 3.1 + 4i // Equality comparison fmt.Println("a == b:", a == b) fmt.Println("a == c:", a == c) // Note: No ordering operators (<, >, etc.) // fmt.Println("a < c:", a < c) // Compile error }
Complex numbers are equal if both real and imaginary parts are equal. Go doesn't support ordering operators (<, >) for complex numbers. This matches mathematical conventions for complex number comparison.
Complex math functions
The math/cmplx
package provides advanced complex number functions.
This example demonstrates common complex math operations.
package main import ( "fmt" "math/cmplx" ) func main() { z := 3 + 4i // Complex math operations conjugate := cmplx.Conj(z) absolute := cmplx.Abs(z) phase := cmplx.Phase(z) sqrt := cmplx.Sqrt(z) fmt.Println("z =", z) fmt.Println("Conjugate:", conjugate) fmt.Printf("Absolute value: %.2f\n", absolute) fmt.Printf("Phase angle: %.2f radians\n", phase) fmt.Println("Square root:", sqrt) }
The math/cmplx
package provides functions like Conj
,
Abs
, and Sqrt
. These implement standard complex number
operations from mathematics. The package includes trigonometric, exponential,
and logarithmic functions too.
Practical complex number application
Complex numbers are useful for solving quadratic equations. This example demonstrates using complex numbers for equation roots.
package main import ( "fmt" "math/cmplx" ) func quadraticRoots(a, b, c float64) (complex128, complex128) { discriminant := b*b - 4*a*c sqrtDiscriminant := cmplx.Sqrt(complex(discriminant, 0)) root1 := (-complex(b, 0) + sqrtDiscriminant) / complex(2*a, 0) root2 := (-complex(b, 0) - sqrtDiscriminant) / complex(2*a, 0) return root1, root2 } func main() { // Real roots r1, r2 := quadraticRoots(1, -3, 2) fmt.Println("Roots of x² - 3x + 2 = 0:") fmt.Println("Root 1:", r1) fmt.Println("Root 2:", r2) // Complex roots r1, r2 = quadraticRoots(1, 0, 1) fmt.Println("\nRoots of x² + 1 = 0:") fmt.Println("Root 1:", r1) fmt.Println("Root 2:", r2) }
Complex numbers allow solving quadratic equations with negative discriminants. The same function works for both real and complex roots. This demonstrates the practical value of complex numbers in computations.
Source
This tutorial covered the ComplexType
in Go with practical
examples of complex number creation, operations, and applications.
Author
List all Golang tutorials.