From b6ad6594f89637ffcee6e94065223dd04656a197 Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Mon, 21 May 2018 13:00:54 -0700 Subject: [PATCH] Use direct ops in Rem for Complex This avoids overflow issues with dividing by `norm_sqr`, and is probably faster too, just for being simpler. --- src/lib.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 55dbf3d..1a6f342 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -849,7 +849,7 @@ impl Rem for Complex { #[inline] fn rem(self, other: T) -> Complex { - self % Complex::new(other, T::zero()) + Complex::new(self.re % other.clone(), self.im % other) } } @@ -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]