Skip to main content

malachite_float/float/arithmetic/
is_unit.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::IsUnit;
11
12impl IsUnit for Float {
13    /// Determines whether a [`Float`] is a unit: whether it is finite and nonzero, so that it has a
14    /// multiplicative inverse; NaN and the infinities are not units.
15    ///
16    /// # Worst-case complexity
17    /// Constant time and additional memory.
18    ///
19    /// # Examples
20    /// ```
21    /// use malachite_base::num::arithmetic::traits::IsUnit;
22    /// use malachite_base::num::basic::traits::{Infinity, NaN, One, Zero};
23    /// use malachite_float::Float;
24    ///
25    /// assert_eq!(Float::ONE.is_unit(), true);
26    /// assert_eq!(Float::from(-1.5).is_unit(), true);
27    /// assert_eq!(Float::ZERO.is_unit(), false);
28    /// assert_eq!(Float::NAN.is_unit(), false);
29    /// assert_eq!(Float::INFINITY.is_unit(), false);
30    /// ```
31    #[inline]
32    fn is_unit(&self) -> bool {
33        self.is_finite() && *self != 0u32
34    }
35}