Skip to content

Commit

Permalink
Merge #25
Browse files Browse the repository at this point in the history
25: Use direct ops in Rem<T> for Complex<T> r=cuviper a=cuviper

This avoids overflow issues with dividing by `norm_sqr`, and is
probably faster too, just for being simpler.

Co-authored-by: Josh Stone <cuviper@gmail.com>
  • Loading branch information
bors[bot] and cuviper committed May 21, 2018
2 parents 6087ddf + b6ad659 commit 11ac7d6
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 11ac7d6

Please sign in to comment.