diff --git a/utilities/src/fixed_128.rs b/utilities/src/fixed_128.rs index 3974352cd1ea8586c8dd6991e8c5b466e7ffb23d..01feec9f52f2f2ef682d5fc8dd93370d410faf99 100644 --- a/utilities/src/fixed_128.rs +++ b/utilities/src/fixed_128.rs @@ -92,6 +92,10 @@ impl Fixed128 { if rhs.0.signum() == 0 { return None; } + if self.0 == 0 { + return Some(*self); + } + let signum = self.0.signum() / rhs.0.signum(); let mut lhs: i128 = self.0; if lhs.is_negative() { @@ -426,6 +430,14 @@ mod tests { assert_eq!(a.checked_div_int(&0i32), None); } + #[test] + fn checked_div_with_zero_dividend_should_be_zero() { + let a = Fixed128::zero(); + let b = Fixed128::from_parts(1); + + assert_eq!(a.checked_div(&b), Some(Fixed128::zero())); + } + #[test] fn under_flow_should_be_none() { let b = Fixed128::from_natural(1);