vertical_multiplication/steps/
shift_add.rs

1use super::*;
2
3impl Display for ShiftAdd {
4    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
5        let tail = ".".repeat(self.tail_digits);
6        write!(f, "{}{}", self.result, tail)
7    }
8}
9
10impl ShiftAdd {
11    /// Create a new `ShiftAdd` step
12    pub fn new<R>(result: R, tail: usize) -> ShiftAdd
13    where
14        R: Into<BigInt>,
15    {
16        ShiftAdd { result: result.into(), tail_digits: tail }
17    }
18    /// Get the integer value of this step
19    pub fn as_integer(&self, base: u32) -> BigInt {
20        BigInt::from(self.tailing_power(base)).mul(&self.result)
21    }
22    /// Get the power of the tailing digits
23    pub fn tailing_power(&self, base: u32) -> BigInt {
24        BigInt::from(base).pow(self.tail_digits as u32)
25    }
26    /// Get the number of digits in this step
27    pub fn count_digits(&self, base: u32) -> usize {
28        self.result.to_str_radix(base).len() + self.tail_digits
29    }
30    /// Format this step with a given width
31    pub fn pretty_format(&self, width: usize, leading: &str, base: u32) -> String {
32        let space = " ".repeat(width - self.count_digits(base) - leading.len());
33        let number = self.result.to_str_radix(base);
34        let tail = ".".repeat(self.tail_digits);
35        format!("{}{}{}{}", leading, space, number, tail)
36    }
37}