From cad2d2b91024850137dd02d30668f8e17da4ec6d Mon Sep 17 00:00:00 2001 From: Fabian Schmidt Date: Tue, 26 Nov 2024 15:58:57 +0100 Subject: [PATCH] Some minimal progress on div --- src/number/div.rs | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/src/number/div.rs b/src/number/div.rs index e4c9db0..1729792 100644 --- a/src/number/div.rs +++ b/src/number/div.rs @@ -16,6 +16,10 @@ impl Number { if n2_len > n1_len { return Ok((Number::from(0), n2)); } + let sign = match (n1.sign, n2.sign) { + (Sign::Positif, Sign::Positif) | (Sign::Negatif, Sign::Negatif) => Sign::Positif, + (Sign::Positif, Sign::Negatif) | (Sign::Negatif, Sign::Positif) => Sign::Negatif, + }; let dividend = n1.digits[..n2_len].to_vec(); let mut quotient = vec![]; let mut remainder = Number { @@ -31,8 +35,13 @@ impl Number { quotient.push(factor); break; } - remainder = temp_remainder; factor += 1; + if temp_remainder == 0.into() { + remainder = temp_remainder; + quotient.push(factor); + break; + } + remainder = temp_remainder; } if n1_len == n2_len + iteration - 1 { break; @@ -40,21 +49,25 @@ impl Number { remainder.digits.push(n1.digits[n2_len + iteration - 1]); iteration += 1; } - let mut res = Number { - digits: quotient, - sign: Sign::Positif, - }; - for digit in res.clone().digits { - if digit != 0 || res.digits.len() == 1 { + println!("{quotient:?}"); + for digit in quotient.clone() { + if digit != 0 || quotient.len() == 1 { break; } - res.digits.remove(0); + quotient.remove(0); } - for digit in remainder.clone().digits { + quotient.reverse(); + let res = Number { + digits: quotient, + sign, + }; + let mut rem_digits = remainder.clone().digits; + rem_digits.reverse(); + for digit in rem_digits { if digit != 0 || remainder.digits.len() == 1 { break; } - remainder.digits.remove(0); + remainder.digits.pop(); } Ok((res, remainder)) //if n2 == 0.into() {