malachite_nz/integer/arithmetic/sub.rs
1// Copyright © 2025 Mikhail Hogrefe
2//
3// This file is part of Malachite.
4//
5// Malachite is free software: you can redistribute it and/or modify it under the terms of the GNU
6// Lesser General Public License (LGPL) as published by the Free Software Foundation; either version
7// 3 of the License, or (at your option) any later version. See <https://www.gnu.org/licenses/>.
8
9use crate::integer::Integer;
10use crate::natural::Natural;
11use core::mem::swap;
12use core::ops::{Sub, SubAssign};
13use malachite_base::num::arithmetic::traits::NegAssign;
14use malachite_base::num::basic::traits::Zero;
15use malachite_base::num::logic::traits::NotAssign;
16
17impl Sub<Self> for Integer {
18 type Output = Self;
19
20 /// Subtracts an [`Integer`] by another [`Integer`], taking both by value.
21 ///
22 /// $$
23 /// f(x, y) = x - y.
24 /// $$
25 ///
26 /// # Worst-case complexity
27 /// $T(n) = O(n)$
28 ///
29 /// $M(n) = O(n)$ (only if the underlying [`Vec`] needs to reallocate)
30 ///
31 /// where $T$ is time, $M$ is additional memory, and $n$ is `min(self.significant_bits(),
32 /// other.significant_bits())`.
33 ///
34 /// # Examples
35 /// ```
36 /// use malachite_base::num::arithmetic::traits::Pow;
37 /// use malachite_base::num::basic::traits::Zero;
38 /// use malachite_nz::integer::Integer;
39 ///
40 /// assert_eq!(Integer::ZERO - Integer::from(123), -123);
41 /// assert_eq!(Integer::from(123) - Integer::ZERO, 123);
42 /// assert_eq!(Integer::from(456) - Integer::from(-123), 579);
43 /// assert_eq!(
44 /// -Integer::from(10u32).pow(12) - -Integer::from(10u32).pow(12) * Integer::from(2u32),
45 /// 1000000000000u64
46 /// );
47 /// ```
48 #[inline]
49 fn sub(mut self, other: Self) -> Self {
50 self -= other;
51 self
52 }
53}
54
55impl Sub<&Self> for Integer {
56 type Output = Self;
57
58 /// Subtracts an [`Integer`] by another [`Integer`], taking the first by value and the second by
59 /// reference.
60 ///
61 /// $$
62 /// f(x, y) = x - y.
63 /// $$
64 ///
65 /// # Worst-case complexity
66 /// $T(n) = O(n)$
67 ///
68 /// $M(n) = O(n)$
69 ///
70 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
71 /// other.significant_bits())`.
72 ///
73 /// # Examples
74 /// ```
75 /// use malachite_base::num::arithmetic::traits::Pow;
76 /// use malachite_base::num::basic::traits::Zero;
77 /// use malachite_nz::integer::Integer;
78 ///
79 /// assert_eq!(Integer::ZERO - &Integer::from(123), -123);
80 /// assert_eq!(Integer::from(123) - &Integer::ZERO, 123);
81 /// assert_eq!(Integer::from(456) - &Integer::from(-123), 579);
82 /// assert_eq!(
83 /// -Integer::from(10u32).pow(12) - &(-Integer::from(10u32).pow(12) * Integer::from(2u32)),
84 /// 1000000000000u64
85 /// );
86 /// ```
87 #[inline]
88 fn sub(mut self, other: &Self) -> Self {
89 self -= other;
90 self
91 }
92}
93
94impl Sub<Integer> for &Integer {
95 type Output = Integer;
96
97 /// Subtracts an [`Integer`] by another [`Integer`], taking the first by reference and the
98 /// second by value.
99 ///
100 /// $$
101 /// f(x, y) = x - y.
102 /// $$
103 ///
104 /// # Worst-case complexity
105 /// $T(n) = O(n)$
106 ///
107 /// $M(n) = O(n)$
108 ///
109 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
110 /// other.significant_bits())`.
111 ///
112 /// # Examples
113 /// ```
114 /// use malachite_base::num::arithmetic::traits::Pow;
115 /// use malachite_base::num::basic::traits::Zero;
116 /// use malachite_nz::integer::Integer;
117 ///
118 /// assert_eq!(&Integer::ZERO - Integer::from(123), -123);
119 /// assert_eq!(&Integer::from(123) - Integer::ZERO, 123);
120 /// assert_eq!(&Integer::from(456) - Integer::from(-123), 579);
121 /// assert_eq!(
122 /// &-Integer::from(10u32).pow(12) - -Integer::from(10u32).pow(12) * Integer::from(2u32),
123 /// 1000000000000u64
124 /// );
125 /// ```
126 fn sub(self, mut other: Integer) -> Integer {
127 other -= self;
128 -other
129 }
130}
131
132impl Sub<&Integer> for &Integer {
133 type Output = Integer;
134
135 /// Subtracts an [`Integer`] by another [`Integer`], taking both by reference.
136 ///
137 /// $$
138 /// f(x, y) = x - y.
139 /// $$
140 ///
141 /// # Worst-case complexity
142 /// $T(n) = O(n)$
143 ///
144 /// $M(n) = O(n)$
145 ///
146 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
147 /// other.significant_bits())`.
148 ///
149 /// # Examples
150 /// ```
151 /// use malachite_base::num::arithmetic::traits::Pow;
152 /// use malachite_base::num::basic::traits::Zero;
153 /// use malachite_nz::integer::Integer;
154 ///
155 /// assert_eq!(&Integer::ZERO - &Integer::from(123), -123);
156 /// assert_eq!(&Integer::from(123) - &Integer::ZERO, 123);
157 /// assert_eq!(&Integer::from(456) - &Integer::from(-123), 579);
158 /// assert_eq!(
159 /// &-Integer::from(10u32).pow(12) - &(-Integer::from(10u32).pow(12) * Integer::from(2u32)),
160 /// 1000000000000u64
161 /// );
162 /// ```
163 fn sub(self, other: &Integer) -> Integer {
164 match (self, other) {
165 (x, y) if core::ptr::eq(x, y) => Integer::ZERO,
166 (integer_zero!(), y) => -y.clone(),
167 (x, &integer_zero!()) => x.clone(),
168 // e.g. 10 - -5 or -10 - 5; sign of result is sign of self
169 (
170 &Integer {
171 sign: sx,
172 abs: ref ax,
173 },
174 &Integer {
175 sign: sy,
176 abs: ref ay,
177 },
178 ) if sx == (!sy && *ay != 0) => Integer {
179 sign: sx,
180 abs: ax + ay,
181 },
182 // e.g. 10 - 5, -10 - -5, or 5 - 5; sign of result is sign of self
183 (
184 &Integer {
185 sign: sx,
186 abs: ref ax,
187 },
188 Integer { abs: ay, .. },
189 ) if sx && *ax == *ay || *ax > *ay => Integer {
190 sign: sx,
191 abs: ax - ay,
192 },
193 // e.g. 5 - 10, -5 - -10, or -5 - -5; sign of result is opposite of sign of other
194 (
195 Integer { abs: ax, .. },
196 &Integer {
197 sign: sy,
198 abs: ref ay,
199 },
200 ) => Integer {
201 sign: !sy,
202 abs: ay - ax,
203 },
204 }
205 }
206}
207
208impl SubAssign<Self> for Integer {
209 /// Subtracts an [`Integer`] by another [`Integer`] in place, taking the [`Integer`] on the
210 /// right-hand side by value.
211 ///
212 /// $$
213 /// x \gets x - y.
214 /// $$
215 ///
216 /// # Worst-case complexity
217 /// $T(n) = O(n)$
218 ///
219 /// $M(n) = O(n)$ (only if the underlying [`Vec`] needs to reallocate)
220 ///
221 /// # Examples
222 /// ```
223 /// use malachite_base::num::arithmetic::traits::Pow;
224 /// use malachite_base::num::basic::traits::Zero;
225 /// use malachite_nz::integer::Integer;
226 ///
227 /// let mut x = Integer::ZERO;
228 /// x -= -Integer::from(10u32).pow(12);
229 /// x -= Integer::from(10u32).pow(12) * Integer::from(2u32);
230 /// x -= -Integer::from(10u32).pow(12) * Integer::from(3u32);
231 /// x -= Integer::from(10u32).pow(12) * Integer::from(4u32);
232 /// assert_eq!(x, -2000000000000i64);
233 /// ```
234 fn sub_assign(&mut self, mut other: Self) {
235 match (&mut *self, &other) {
236 (_, &integer_zero!()) => {}
237 (&mut integer_zero!(), _) => {
238 *self = other;
239 self.neg_assign();
240 }
241 // e.g. 10 - -5 or -10 - 5; sign of self is unchanged
242 (
243 &mut Self {
244 sign: sx,
245 abs: ref mut ax,
246 },
247 &Self {
248 sign: sy,
249 abs: ref ay,
250 },
251 ) if sx == (!sy && *ay != 0) => *ax += ay,
252 // e.g. 10 - 5, -10 - -5, or 5 - 5; sign of self is unchanged
253 (
254 &mut Self {
255 sign: sx,
256 abs: ref mut ax,
257 },
258 Self { abs: ay, .. },
259 ) if sx && *ax == *ay || *ax > *ay => *ax -= ay,
260 // e.g. 5 - 10, -5 - -10, or -5 - -5; sign of self is flipped
261 _ => {
262 swap(self, &mut other);
263 self.abs -= other.abs;
264 self.sign.not_assign();
265 }
266 }
267 }
268}
269
270impl SubAssign<&Self> for Integer {
271 /// Subtracts an [`Integer`] by another [`Integer`] in place, taking the [`Integer`] on the
272 /// right-hand side by reference.
273 ///
274 /// $$
275 /// x \gets x - y.
276 /// $$
277 ///
278 /// # Worst-case complexity
279 /// $T(n) = O(n)$
280 ///
281 /// $M(n) = O(n)$
282 ///
283 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
284 /// other.significant_bits())`.
285 ///
286 /// # Examples
287 /// ```
288 /// use malachite_base::num::arithmetic::traits::Pow;
289 /// use malachite_base::num::basic::traits::Zero;
290 /// use malachite_nz::integer::Integer;
291 ///
292 /// let mut x = Integer::ZERO;
293 /// x -= &(-Integer::from(10u32).pow(12));
294 /// x -= &(Integer::from(10u32).pow(12) * Integer::from(2u32));
295 /// x -= &(-Integer::from(10u32).pow(12) * Integer::from(3u32));
296 /// x -= &(Integer::from(10u32).pow(12) * Integer::from(4u32));
297 /// assert_eq!(x, -2000000000000i64);
298 /// ```
299 fn sub_assign(&mut self, other: &Self) {
300 match (&mut *self, other) {
301 (_, &integer_zero!()) => {}
302 (&mut integer_zero!(), y) => *self = -y.clone(),
303 // e.g. 10 - -5 or -10 - 5; sign of self is unchanged
304 (
305 &mut Self {
306 sign: sx,
307 abs: ref mut ax,
308 },
309 &Self {
310 sign: sy,
311 abs: ref ay,
312 },
313 ) if sx == (!sy && *ay != 0) => *ax += ay,
314 // e.g. 10 - 5, -10 - -5, or 5 - 5; sign of self is unchanged
315 (
316 &mut Self {
317 sign: sx,
318 abs: ref mut ax,
319 },
320 Self { abs: ay, .. },
321 ) if sx && *ax == *ay || *ax > *ay => *ax -= ay,
322 (
323 &mut Self {
324 sign: ref mut sx,
325 abs: ref mut ax,
326 },
327 &Self {
328 sign: sy,
329 abs: ref ay,
330 },
331 ) => {
332 *sx = !sy;
333 *ax = ay - &*ax;
334 }
335 }
336 }
337}