diff --git a/vlib/math/complex.v b/vlib/math/complex.v index 8925a7021a..87782410b9 100644 --- a/vlib/math/complex.v +++ b/vlib/math/complex.v @@ -26,6 +26,11 @@ pub fn (c Complex) str() string { return out } +// Complex Angle +pub fn (c Complex) angle() f64 { + return atan2(c.im, c.re) +} + // Complex Addition c1 + c2 pub fn (c1 Complex) + (c2 Complex) Complex { return Complex{c1.re+c2.re,c1.im+c2.im} @@ -90,4 +95,4 @@ pub fn (c1 Complex) conjugate() Complex{ // Complex Equals pub fn (c1 Complex) equals(c2 Complex) bool { return (c1.re == c2.re) && (c1.im == c2.im) -} \ No newline at end of file +} diff --git a/vlib/math/complex_test.v b/vlib/math/complex_test.v index 0407c16dc2..0f2c637d32 100644 --- a/vlib/math/complex_test.v +++ b/vlib/math/complex_test.v @@ -101,4 +101,19 @@ fn test_complex_equals() { c1 = math.complex(-3,19) c2 = math.complex(-3,19) assert c1.equals(c2) -} \ No newline at end of file +} + +fn test_complex_angle(){ + mut c := math.complex(1, 0) + assert c.angle() * 180 / math.Pi == 0 + c = math.complex(1, 1) + assert c.angle() * 180 / math.Pi == 45 + c = math.complex(0, 1) + assert c.angle() * 180 / math.Pi == 90 + c = math.complex(-1, 1) + assert c.angle() * 180 / math.Pi == 135 + c = math.complex(-1, -1) + assert c.angle() * 180 / math.Pi == -135 + mut cc := c.conjugate() + assert cc.angle() + c.angle() == 0 +}