This module implements complex numbers and basic mathematical operations on them.
Complex numbers are currently generic over 64-bit or 32-bit floats.
Example:
import std/complex from std/math import almostEqual, sqrt func almostEqual(a, b: Complex): bool = almostEqual(a.re, b.re) and almostEqual(a.im, b.im) let z1 = complex(1.0, 2.0) z2 = complex(3.0, -4.0) assert almostEqual(z1 + z2, complex(4.0, -2.0)) assert almostEqual(z1 - z2, complex(-2.0, 6.0)) assert almostEqual(z1 * z2, complex(11.0, 2.0)) assert almostEqual(z1 / z2, complex(-0.2, 0.4)) assert almostEqual(abs(z1), sqrt(5.0)) assert almostEqual(conjugate(z1), complex(1.0, -2.0)) let (r, phi) = z1.polar assert almostEqual(rect(r, phi), z1)
Procs
func `-`[T](x: Complex[T]; y: T): Complex[T]
- Subtracts a real number from a complex number. Source Edit
func rect[T](r, phi: T): Complex[T]
-
Returns the complex number with polar coordinates r and phi.
result.re = r * cos(phi)
result.im = r * sin(phi)See also:
- polar func for the inverse operation