From 2e054ce90cc33399bff74741fb12f4304fb8e7e5 Mon Sep 17 00:00:00 2001
From: Shaopeng Wang <spxwang@gmail.com>
Date: Wed, 18 Mar 2020 13:20:01 +1300
Subject: [PATCH] Fixed128: fix dividend zero checked_div panic. (#125)

---
 utilities/src/fixed_128.rs | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/utilities/src/fixed_128.rs b/utilities/src/fixed_128.rs
index 3974352..01feec9 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);
-- 
GitLab