add angle func

pull/1054/head
hazohelet 2019-07-07 23:13:17 +09:00 committed by Alexander Medvednikov
parent b40ad7c83f
commit ccf733ac95
2 changed files with 22 additions and 2 deletions

View File

@ -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)
}
}

View File

@ -101,4 +101,19 @@ fn test_complex_equals() {
c1 = math.complex(-3,19)
c2 = math.complex(-3,19)
assert c1.equals(c2)
}
}
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
}