1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

add abs for complex, add tests (#1043)

This commit is contained in:
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
}
// Complex Absolute value
pub fn (c Complex) abs() f64 {
return C.hypot(c.re,c.im)
}
// Complex Angle
pub fn (c Complex) angle() f64 {
return atan2(c.im, c.re)

View File

@ -103,6 +103,16 @@ fn test_complex_equals() {
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(){
mut c := math.complex(1, 0)
assert c.angle() * 180 / math.Pi == 0