std/complex

March 14, 2026 ยท View on GitHub

The std/complex library provides the Complex struct and essential mathematical operations for working with complex numbers in Zen-C.

Overview

  • Value Type: Simple struct with real and imag components.
  • Operator support: Supports +, -, *, /, ==, and != via operator overloading.
  • Properties: Provides methods for calculating magnitude and phase.
  • Interpolation: Can be directly used in f-strings and print statements.

Usage

import "std/complex.zc"

fn main() {
    let c1 = Complex::new(3.0, 4.0);
    let c2 = Complex::new(1.0, 2.0);
    
    let sum = c1 + c2;
    let prod = c1 * c2;
    
    println "Sum: {sum}";       // Sum: 4.000000 + 6.000000i
    println "Magnitude: {c1.magnitude()}";
}

Struct Definition

struct Complex {
    real: double;
    imag: double;
}

Methods

Construction

MethodSignatureDescription
newComplex::new(r: double, i: double) -> ComplexCreates a new complex number with real component r and imaginary component i.

Access & Query

MethodSignatureDescription
magnitudemagnitude(self) -> doubleReturns the magnitude (absolute value) of the complex number.
phasephase(self) -> doubleReturns the phase (angle) in radians.

Operators

OperatorMethodDescription
+addAdds two complex numbers.
-subSubtracts one complex number from another.
*mulMultiplies two complex numbers.
/divDivides one complex number by another.
==eqChecks if two complex numbers are strictly equal.
!=neqChecks if two complex numbers are not equal.
{}to_stringEnables direct string interpolation.