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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
macro_rules! define_none_prec_common {
() => {
/// The zero value.
pub const ZERO: Self = Self(I::ZERO);
/// The largest value, (2<sup>b</sup> - 1) / 10<sup>P</sup>, where `b`
/// is the bits of `I`.
pub const MAX: Self = Self(I::MAX);
/// The smallest value, -(2<sup>b</sup> / 10<sup>P</sup>), where `b`
/// is the bits of `I`.
pub const MIN: Self = Self(I::MIN);
/// The smallest positive value, 10<sup>-P</sup> .
pub const MIN_POSITIVE: Self = Self(I::ONE);
/// The largest powers of 10.
pub const MAX_POWERS: Self = Self(I::MAX_POWERS);
/// Computes the absolute value of self.
///
/// # Overflow behavior
///
/// The absolute value of `MIN` cannot be represented as this type,
/// and attempting to calculate it will cause an overflow. This means that
/// code in debug mode will trigger a panic on this case and optimized code
/// will return `MIN` without a panic.
pub fn abs(self) -> Self {
Self(self.0.abs())
}
/// Checked absolute value. Computes `self.abs()`, returning `None` if `self == MIN`.
pub fn checked_abs(self) -> Option<Self> {
if self == Self::MIN {
None
} else {
Some(self.abs())
}
}
/// Checked addition. Computes `self + rhs`, returning `None` if overflow occurred.
///
/// The right operand must have the same precision with self.
pub fn checked_add(self, rhs: Self) -> Option<Self> {
self.0.checked_add(&rhs.0).map(Self)
}
/// Checked subtraction. Computes `self - rhs`, returning `None` if overflow occurred.
///
/// The right operand must have the same precision with self.
pub fn checked_sub(self, rhs: Self) -> Option<Self> {
self.0.checked_sub(&rhs.0).map(Self)
}
/// Checked multiplication with integer. Computes `self * n`, returning
/// `None` if overflow occurred.
pub fn checked_mul_int(self, n: I) -> Option<Self> {
self.0.checked_mul(&n).map(Self)
}
/// Checked division by integer, with `Rounding::Round`.
///
/// Computes `self / n`, returning `None` if `n == 0` or overflow occurres.
pub fn checked_div_int(self, n: I) -> Option<Self> {
self.checked_div_int_ext(n, Rounding::Round, None)
}
/// Checked division by integer with rounding and cumulative-error.
///
/// Computes `self / n`, returning `None` if `n == 0` or overflow occurres.
///
/// See the [cumulative error section](index.html#cumulative-error)
/// for more information and examples.
pub fn checked_div_int_ext(
self,
n: I,
rounding: Rounding,
cum_error: Option<&mut I>,
) -> Option<Self> {
checked_divide(self.0, n, rounding, cum_error).map(Self)
}
/// Return if negative.
pub fn is_neg(&self) -> bool {
self.0.is_negative()
}
/// Return if positive.
pub fn is_pos(&self) -> bool {
self.0.is_positive()
}
/// Return if zero.
pub fn is_zero(&self) -> bool {
self.0.is_zero()
}
}
}
pub(crate) use define_none_prec_common;