complex: add saturating_add/sub

This commit is contained in:
Robert Jördens 2021-02-22 16:36:56 +01:00
parent d02241fcf9
commit f671e0c942
1 changed files with 12 additions and 1 deletions

View File

@ -8,6 +8,8 @@ pub trait ComplexExt<T, U> {
fn abs_sqr(&self) -> U; fn abs_sqr(&self) -> U;
fn log2(&self) -> T; fn log2(&self) -> T;
fn arg(&self) -> T; fn arg(&self) -> T;
fn saturating_add(&self, other: Self) -> Self;
fn saturating_sub(&self, other: Self) -> Self;
} }
impl ComplexExt<i32, u32> for Complex<i32> { impl ComplexExt<i32, u32> for Complex<i32> {
@ -23,7 +25,7 @@ impl ComplexExt<i32, u32> for Complex<i32> {
/// ``` /// ```
fn from_angle(angle: i32) -> Self { fn from_angle(angle: i32) -> Self {
let (c, s) = cossin(angle); let (c, s) = cossin(angle);
Self { re: c, im: s } Self::new(c, s)
} }
/// Return the absolute square (the squared magnitude). /// Return the absolute square (the squared magnitude).
@ -85,6 +87,15 @@ impl ComplexExt<i32, u32> for Complex<i32> {
fn arg(&self) -> i32 { fn arg(&self) -> i32 {
atan2(self.im, self.re) atan2(self.im, self.re)
} }
fn saturating_add(&self, other: Self) -> Self {
Self::new(self.re.saturating_add(other.re), self.im.saturating_add(other.im))
}
fn saturating_sub(&self, other: Self) -> Self {
Self::new(self.re.saturating_sub(other.re), self.im.saturating_sub(other.im))
}
} }
/// Full scale fixed point multiplication. /// Full scale fixed point multiplication.