From f8d2004c8f1885974f594d2d3fd391ad4d7eb2a4 Mon Sep 17 00:00:00 2001 From: Trevor Spiteri Date: Wed, 12 Feb 2020 16:58:21 +0100 Subject: [PATCH] fix Rem for Fixed https://gitlab.com/tspiteri/fixed/issues/13 --- src/arith.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/arith.rs b/src/arith.rs index b26c986..ca4d9fc 100644 --- a/src/arith.rs +++ b/src/arith.rs @@ -370,7 +370,13 @@ macro_rules! fixed_arith { type Output = $Fixed; #[inline] fn rem(self, rhs: $Inner) -> $Fixed { - Self::from_bits(self.to_bits().rem(rhs)) + // Any overflow in coverting rhs to $Fixed means that |rhs| > |self|, + // and consequently the remainder is self. + let fixed_rhs = match Self::checked_from_num(rhs) { + Some(s) => s, + None => return self, + }; + self.rem(fixed_rhs) } }