Skip to content

Commit

Permalink
Use direct ops in Rem<T> for Complex<T>
Browse files Browse the repository at this point in the history
This avoids overflow issues with dividing by `norm_sqr`, and is
probably faster too, just for being simpler.
  • Loading branch information
cuviper committed May 21, 2018
1 parent 6087ddf commit b6ad659
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -849,7 +849,7 @@ impl<T: Clone + Num> Rem<T> for Complex<T> {

#[inline]
fn rem(self, other: T) -> Complex<T> {
self % Complex::new(other, T::zero())
Complex::new(self.re % other.clone(), self.im % other)
}
}

Expand Down Expand Up @@ -1821,6 +1821,14 @@ mod test {
assert_eq!(_neg1_1i % 2.0, _neg1_1i);
assert_eq!(-_4_2i % 3.0, Complex::new(-1.0, -2.0));
}

#[test]
fn test_div_rem_gaussian() {
// These would overflow with `norm_sqr` division.
let max = Complex::new(255u8, 255u8);
assert_eq!(max / 200, Complex::new(1, 1));
assert_eq!(max % 200, Complex::new(55, 55));
}
}

#[test]
Expand Down

0 comments on commit b6ad659

Please sign in to comment.