Change digits from isize to i8
This commit is contained in:
parent
2ec7e6c296
commit
ed886762d0
@ -15,7 +15,7 @@ pub enum Sign {
|
|||||||
|
|
||||||
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
|
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
|
||||||
pub struct Number {
|
pub struct Number {
|
||||||
pub digits: Vec<isize>,
|
pub digits: Vec<i8>,
|
||||||
pub sign: Sign,
|
pub sign: Sign,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -26,9 +26,9 @@ impl Number {
|
|||||||
(n % modulo) / divisor
|
(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
|
// 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 {
|
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 {
|
pub fn fact(self) -> Self {
|
||||||
let mut fact = Number::from(1);
|
let mut fact = Number::from(1);
|
||||||
if ((self.digits.len() * 8) as u32) < isize::BITS {
|
if ((self.digits.len() * 8) as u32) < isize::BITS {
|
||||||
@ -107,4 +114,28 @@ mod number_tests {
|
|||||||
assert_eq!(digit_4, 2);
|
assert_eq!(digit_4, 2);
|
||||||
assert_eq!(digit_5, 1);
|
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));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,7 +16,7 @@ impl Add for Number {
|
|||||||
let mut rhs_digits = rhs.digits.clone();
|
let mut rhs_digits = rhs.digits.clone();
|
||||||
if self_len != rhs_len {
|
if self_len != rhs_len {
|
||||||
let difference = (self_len).abs_diff(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 {
|
if min(self_len, rhs_len) == self_len {
|
||||||
self_digits = [self.digits, pad].concat();
|
self_digits = [self.digits, pad].concat();
|
||||||
} else {
|
} else {
|
||||||
|
@ -8,7 +8,7 @@ impl TryFrom<Number> for isize {
|
|||||||
fn try_from(value: Number) -> Result<Self, Self::Error> {
|
fn try_from(value: Number) -> Result<Self, Self::Error> {
|
||||||
let mut num = 0;
|
let mut num = 0;
|
||||||
for (pos, &digit) in value.digits.iter().enumerate() {
|
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() {
|
if mul.is_none() {
|
||||||
return Err(Box::from("Cannot convert Number to isize. Too big."));
|
return Err(Box::from("Cannot convert Number to isize. Too big."));
|
||||||
}
|
}
|
||||||
@ -62,7 +62,7 @@ impl From<isize> for Number {
|
|||||||
let num_len = (value as f64 + 1.0).log10().ceil() as usize;
|
let num_len = (value as f64 + 1.0).log10().ceil() as usize;
|
||||||
let mut digits = vec![];
|
let mut digits = vec![];
|
||||||
for digit_idx in 0..num_len {
|
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);
|
digits.push(digit);
|
||||||
}
|
}
|
||||||
let digits = digits.to_vec();
|
let digits = digits.to_vec();
|
||||||
|
@ -16,7 +16,7 @@ impl Sub for Number {
|
|||||||
let mut rhs_digits = rhs.digits.clone();
|
let mut rhs_digits = rhs.digits.clone();
|
||||||
if self_len != rhs_len {
|
if self_len != rhs_len {
|
||||||
let difference = (self_len).abs_diff(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 {
|
if min(self_len, rhs_len) == self_len {
|
||||||
self_digits = [self.digits, pad].concat();
|
self_digits = [self.digits, pad].concat();
|
||||||
} else {
|
} else {
|
||||||
|
Loading…
Reference in New Issue
Block a user