diff --git a/src/number.rs b/src/number.rs index 21fc589..d919e97 100644 --- a/src/number.rs +++ b/src/number.rs @@ -15,7 +15,7 @@ pub enum Sign { #[derive(Clone, Debug, Eq, PartialEq, Hash)] pub struct Number { - pub digits: Vec, + pub digits: Vec, pub sign: Sign, } @@ -26,9 +26,9 @@ impl Number { (n % modulo) / divisor } - pub const fn byte_to_digit(b: u8) -> isize { + pub const fn byte_to_digit(b: u8) -> i8 { // wrapping_sub('0' as u32) same as - 48 but less magical - (b as isize).wrapping_sub('0' as isize) + (b as i8).wrapping_sub('0' as i8) } pub fn pow(self, n: u32) -> Self { @@ -48,6 +48,13 @@ impl Number { } } + pub fn ten_pow(self, n: u32) -> Self { + Number { + digits: [vec![0; n as usize], self.digits].concat(), + sign: self.sign, + } + } + pub fn fact(self) -> Self { let mut fact = Number::from(1); if ((self.digits.len() * 8) as u32) < isize::BITS { @@ -107,4 +114,28 @@ mod number_tests { assert_eq!(digit_4, 2); assert_eq!(digit_5, 1); } + + #[test] + fn test_pow() { + let num = Number::from(2); + assert_eq!(Number::from(16), num.pow(4)); + } + + #[test] + fn test_ten_pow_zero() { + let num = Number::from(2); + assert_eq!(Number::from(2), num.ten_pow(0)); + } + + #[test] + fn test_ten_pow_one() { + let num = Number::from(2); + assert_eq!(Number::from(20), num.ten_pow(1)); + } + + #[test] + fn test_ten_pow_two() { + let num = Number::from(2); + assert_eq!(Number::from(200), num.ten_pow(2)); + } } diff --git a/src/number/add.rs b/src/number/add.rs index 0f4d0ea..3cfe093 100644 --- a/src/number/add.rs +++ b/src/number/add.rs @@ -16,7 +16,7 @@ impl Add for Number { let mut rhs_digits = rhs.digits.clone(); if self_len != rhs_len { let difference = (self_len).abs_diff(rhs_len); - let pad = vec![0isize; difference]; + let pad = vec![0i8; difference]; if min(self_len, rhs_len) == self_len { self_digits = [self.digits, pad].concat(); } else { diff --git a/src/number/from.rs b/src/number/from.rs index 39ac26a..510ecb6 100644 --- a/src/number/from.rs +++ b/src/number/from.rs @@ -8,7 +8,7 @@ impl TryFrom for isize { fn try_from(value: Number) -> Result { let mut num = 0; for (pos, &digit) in value.digits.iter().enumerate() { - let mul = digit.checked_mul(10isize.pow(pos as u32)); + let mul = (digit as isize).checked_mul(10isize.pow(pos as u32)); if mul.is_none() { return Err(Box::from("Cannot convert Number to isize. Too big.")); } @@ -62,7 +62,7 @@ impl From for Number { let num_len = (value as f64 + 1.0).log10().ceil() as usize; let mut digits = vec![]; for digit_idx in 0..num_len { - let digit = Self::get_digit(value, digit_idx); + let digit = Self::get_digit(value, digit_idx) as i8; digits.push(digit); } let digits = digits.to_vec(); diff --git a/src/number/sub.rs b/src/number/sub.rs index 86e02de..f3dd189 100644 --- a/src/number/sub.rs +++ b/src/number/sub.rs @@ -16,7 +16,7 @@ impl Sub for Number { let mut rhs_digits = rhs.digits.clone(); if self_len != rhs_len { let difference = (self_len).abs_diff(rhs_len); - let pad = vec![0isize; difference]; + let pad = vec![0i8; difference]; if min(self_len, rhs_len) == self_len { self_digits = [self.digits, pad].concat(); } else {