malachite_float/float/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::Float;
10use malachite_base::num::arithmetic::traits::CanonicalUnitIPow;
11
12impl CanonicalUnitIPow for Float {
13 /// Finds the power of $i$ that brings a [`Float`] into canonical unit form. The canonical unit
14 /// form of a [`Float`] is its absolute value, so this is 2 for values with the sign bit set,
15 /// negative zero and negative infinity included, since $x i^2 = -x$, and 0 otherwise, NaN
16 /// included.
17 ///
18 /// # Worst-case complexity
19 /// Constant time and additional memory.
20 ///
21 /// # Examples
22 /// ```
23 /// use malachite_base::num::arithmetic::traits::CanonicalUnitIPow;
24 /// use malachite_base::num::basic::traits::{NaN, NegativeZero};
25 /// use malachite_float::Float;
26 ///
27 /// assert_eq!(Float::from(1.5).canonical_unit_i_pow(), 0);
28 /// assert_eq!(Float::from(-1.5).canonical_unit_i_pow(), 2);
29 /// assert_eq!(Float::NEGATIVE_ZERO.canonical_unit_i_pow(), 2);
30 /// assert_eq!(Float::NAN.canonical_unit_i_pow(), 0);
31 /// ```
32 #[inline]
33 fn canonical_unit_i_pow(&self) -> u64 {
34 if self.is_sign_negative() && !self.is_nan() {
35 2
36 } else {
37 0
38 }
39 }
40}