malachite_base/num/arithmetic/overflowing_abs.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::num::arithmetic::traits::{OverflowingAbs, OverflowingAbsAssign};
10
11macro_rules! impl_overflowing_abs {
12 ($t:ident) => {
13 impl OverflowingAbs for $t {
14 type Output = $t;
15
16 /// This is a wrapper over the `overflowing_abs` functions in the standard library, for
17 /// example [this one](i32::overflowing_abs).
18 #[inline]
19 fn overflowing_abs(self) -> ($t, bool) {
20 $t::overflowing_abs(self)
21 }
22 }
23
24 impl OverflowingAbsAssign for $t {
25 /// Replaces a number with its absolute value.
26 ///
27 /// Returns a boolean indicating whether an arithmetic overflow occurred. If an overflow
28 /// occurred, then the wrapped value is assigned.
29 ///
30 /// # Worst-case complexity
31 /// Constant time and additional memory.
32 ///
33 /// # Examples
34 /// See [here](super::overflowing_abs#overflowing_abs_assign).
35 #[inline]
36 fn overflowing_abs_assign(&mut self) -> bool {
37 let overflow;
38 (*self, overflow) = self.overflowing_abs();
39 overflow
40 }
41 }
42 };
43}
44apply_to_signeds!(impl_overflowing_abs);