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 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 almostEqual[T: SomeFloat](x, y: Complex[T]; unitsInLastPlace: Natural = 4): bool
-
Checks if two complex values are almost equal, using the machine epsilon.
Two complex values are considered almost equal if their real and imaginary components are almost equal.
unitsInLastPlace is the max number of units in the last place difference tolerated when comparing two numbers. The larger the value, the more error is allowed. A 0 value means that two numbers must be exactly the same to be considered equal.
The machine epsilon has to be scaled to the magnitude of the values used and multiplied by the desired precision in ULPs unless the difference is subnormal.
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