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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
use std::cmp::Ordering;
pub use parser::{dec_repr_from_str, ParseDecimalError};
pub use powers_of_ten::{checked_mul_pow_ten, mul_pow_ten, ten_pow};
mod parser;
mod powers_of_ten;
pub const MAX_N_FRAC_DIGITS: u8 = 38;
#[doc(hidden)]
#[inline]
pub fn adjust_coeffs(x: i128, p: u8, y: i128, q: u8) -> (i128, i128) {
match p.cmp(&q) {
Ordering::Equal => (x, y),
Ordering::Greater => (x, mul_pow_ten(y, p - q)),
Ordering::Less => (mul_pow_ten(x, q - p), y),
}
}
#[doc(hidden)]
#[inline]
pub fn checked_adjust_coeffs(
x: i128,
p: u8,
y: i128,
q: u8,
) -> (Option<i128>, Option<i128>) {
match p.cmp(&q) {
Ordering::Equal => (Some(x), Some(y)),
Ordering::Greater => (Some(x), checked_mul_pow_ten(y, p - q)),
Ordering::Less => (checked_mul_pow_ten(x, q - p), Some(y)),
}
}
#[doc(hidden)]
#[inline]
pub fn div_mod_floor(x: i128, y: i128) -> (i128, i128) {
let (q, r) = (x / y, x % y);
if (r > 0 && y < 0) || (r < 0 && y > 0) {
(q - 1, r + y)
} else {
(q, r)
}
}
#[doc(hidden)]
#[inline]
pub fn magnitude(i: i128) -> u8 {
(i.abs() as f64).log10().trunc() as u8
}