add abs for complex, add tests (#1043)

pull/1056/head
Koustav Chowdhury 2019-07-10 00:42:52 +05:30 committed by Alexander Medvednikov
parent c4fcfcec88
commit 385f47c0cd
2 changed files with 15 additions and 0 deletions

View File

@ -26,6 +26,11 @@ pub fn (c Complex) str() string {
return out return out
} }
// Complex Absolute value
pub fn (c Complex) abs() f64 {
return C.hypot(c.re,c.im)
}
// Complex Angle // Complex Angle
pub fn (c Complex) angle() f64 { pub fn (c Complex) angle() f64 {
return atan2(c.im, c.re) return atan2(c.im, c.re)

View File

@ -103,6 +103,16 @@ fn test_complex_equals() {
assert c1.equals(c2) assert c1.equals(c2)
} }
fn test_complex_abs() {
mut c1 := math.complex(3,4)
assert c1.abs() == 5
c1 = math.complex(1,2)
assert c1.abs() == math.sqrt(5)
assert c1.abs() == c1.conjugate().abs()
c1 = math.complex(7,0)
assert c1.abs() == 7
}
fn test_complex_angle(){ fn test_complex_angle(){
mut c := math.complex(1, 0) mut c := math.complex(1, 0)
assert c.angle() * 180 / math.Pi == 0 assert c.angle() * 180 / math.Pi == 0