malachite_nz/integer/arithmetic/canonical_unit_i_pow.rs
1// Copyright © 2026 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 malachite_base::num::arithmetic::traits::CanonicalUnitIPow;
11
12impl CanonicalUnitIPow for Integer {
13 /// Finds the power of $i$ that brings a [`Integer`] into canonical unit form. The canonical
14 /// unit form of an [`Integer`] is its absolute value, so this is 2 for negative values, since
15 /// $x i^2 = -x$, and 0 otherwise.
16 ///
17 /// # Worst-case complexity
18 /// Constant time and additional memory.
19 ///
20 /// # Examples
21 /// ```
22 /// use malachite_base::num::arithmetic::traits::CanonicalUnitIPow;
23 /// use malachite_nz::integer::Integer;
24 ///
25 /// assert_eq!(Integer::from(123).canonical_unit_i_pow(), 0);
26 /// assert_eq!(Integer::from(-123).canonical_unit_i_pow(), 2);
27 /// ```
28 #[inline]
29 fn canonical_unit_i_pow(&self) -> u64 {
30 if *self < 0u32 { 2 } else { 0 }
31 }
32}