add abs for complex, add tests (#1043)
parent
c4fcfcec88
commit
385f47c0cd
|
@ -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)
|
||||||
|
|
|
@ -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
|
||||||
|
|
Loading…
Reference in New Issue