From a231fbf12c543534c6b300648e8c3a8b467968cf Mon Sep 17 00:00:00 2001 From: Paul Mason Date: Tue, 8 Jun 2021 06:56:24 +1200 Subject: [PATCH] Fixes issue with remainder overflow --- src/ops/div.rs | 2 +- tests/decimal_tests.rs | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/ops/div.rs b/src/ops/div.rs index 948a0405..3b5ec577 100644 --- a/src/ops/div.rs +++ b/src/ops/div.rs @@ -196,7 +196,7 @@ impl Buf16 { if num < prod1 { // Detected carry. let tmp = remainder; - remainder += 1; + remainder = remainder.wrapping_add(1); if tmp < divisor_hi { break; } diff --git a/tests/decimal_tests.rs b/tests/decimal_tests.rs index 117f4aa6..6a838d5c 100644 --- a/tests/decimal_tests.rs +++ b/tests/decimal_tests.rs @@ -3989,4 +3989,13 @@ mod issues { // - 714606667614253123173036.85120 assert_eq!("-714606667614253123173036.85120", c.unwrap().to_string()); } + + #[test] + fn issue_392_overflow_during_remainder() { + let a = Decimal::from_str("-79228157791897.854723898738431").unwrap(); + let b = Decimal::from_str("184512476.73336922111").unwrap(); + let c = a.checked_div(b); + assert!(c.is_some()); + assert_eq!("-429391.87200000000002327170816", c.unwrap().to_string()) + } }