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
/*
Copyright 2024 Owain Davies
SPDX-License-Identifier: Apache-2.0 OR MIT
*/
use crate::Arbi;
impl Arbi {
/// Negates the integer, in-place.
///
/// # Examples
/// ```
/// use arbi::Arbi;
/// let mut pos = Arbi::from(123456789);
/// pos = pos.negate();
/// assert_eq!(pos, -123456789);
/// pos = pos.negate();
/// assert_eq!(pos, 123456789);
/// ```
///
/// # Complexity
/// \\( O(1) \\)
#[inline(always)]
pub fn negate(mut self) -> Self {
self.negate_mut();
self
}
/// Negates the integer, in-place.
///
/// # Examples
/// ```
/// use arbi::Arbi;
/// let mut pos = Arbi::from(123456789);
/// pos.negate_mut();
/// assert_eq!(pos, -123456789);
/// pos.negate_mut();
/// assert_eq!(pos, 123456789);
/// ```
///
/// # Complexity
/// \\( O(1) \\)
#[inline(always)]
pub fn negate_mut(&mut self) {
if self.size() > 0 {
self.neg = !self.neg;
}
}
/// Return a new integer representing this integer negated.
///
/// # Examples
/// ```
/// use arbi::Arbi;
/// let pos = Arbi::from(123456789);
/// assert_eq!(pos.negate_ref(), -123456789);
/// let neg = Arbi::from(-123456789);
/// assert_eq!(neg.negate_ref(), 123456789);
/// ```
///
/// # Complexity
/// \\( O(n) \\)
#[inline(always)]
pub fn negate_ref(&self) -> Self {
let ret = self.clone();
ret.negate()
}
}
#[cfg(test)]
mod tests {
use crate::Arbi;
#[test]
fn test_negate_mut() {
let mut a = Arbi::from(123);
let mut z = Arbi::zero();
// Zero
z.negate_mut();
assert_eq!(z, 0);
// Positive
a.negate_mut();
assert_eq!(a, -123);
// Negative
a.negate_mut();
assert_eq!(a, 123);
a.negate_mut();
a.negate_mut();
assert_eq!(a, 123);
}
}