Ctrl K
ComunidadGuías…

std/complex.zc

Rama main · réplica de lectura del repositorio original.

Archivo original ↗Preguntar a KiBot
import "./core.zc"
import "./math.zc"
import "./string.zc"

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

impl Complex {
    fn new(r: f64, i: f64) -> Complex {
        return Complex { real: r, imag: i };
    }

    // Arithmetic Operators
    fn add(self, other: Complex) -> Complex {
        return Complex {
            real: self.real + other.real,
            imag: self.imag + other.imag
        };
    }

    fn sub(self, other: Complex) -> Complex {
        return Complex {
            real: self.real - other.real,
            imag: self.imag - other.imag
        };
    }

    fn mul(self, other: Complex) -> Complex {
        return Complex {
            real: (self.real * other.real) - (self.imag * other.imag),
            imag: (self.real * other.imag) + (self.imag * other.real)
        };
    }

    fn div(self, other: Complex) -> Complex {
        let denominator = (other.real * other.real) + (other.imag * other.imag);
        return Complex {
            real: ((self.real * other.real) + (self.imag * other.imag)) / denominator,
            imag: ((self.imag * other.real) - (self.real * other.imag)) / denominator
        };
    }

    // Mathematical Properties
    fn magnitude(self) -> f64 {
        return Math::sqrt((self.real * self.real) + (self.imag * self.imag));
    }

    fn phase(self) -> f64 {
        return Math::atan2(self.imag, self.real);
    }

    // Equality Comparisons
    fn eq(self, other: Complex) -> bool {
        return (self.real == other.real) && (self.imag == other.imag);
    }

    fn neq(self, other: Complex) -> bool {
        return !self.eq(other);
    }

    // String Interpolation Support
    fn to_string(self) -> char* {
        let sign = "+";
        let im = self.imag;
        if (im < 0.0) {
            sign = "-";
            im = -im;
        }
        
        let s = String::from("");
        s.append_c("{self.real} ");
        s.append_c(sign);
        s.append_c(" {im}i");
        
        let out = s.c_str();
        s.forget();
        return out;
    }
}