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