malachite_float/conversion/
is_integer.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::Float;
10use malachite_base::num::conversion::traits::{ConvertibleFrom, IsInteger};
11use malachite_nz::integer::Integer;
12
13impl IsInteger for &Float {
14    /// Determines whether a [`Float`] is an integer.
15    ///
16    /// $f(x) = x \in \Z$.
17    ///
18    /// # Worst-case complexity
19    /// Constant time and additional memory.
20    ///
21    /// # Examples
22    /// ```
23    /// use malachite_base::num::basic::traits::{One, OneHalf, Zero};
24    /// use malachite_base::num::conversion::traits::IsInteger;
25    /// use malachite_float::Float;
26    ///
27    /// assert_eq!(Float::ZERO.is_integer(), true);
28    /// assert_eq!(Float::ONE.is_integer(), true);
29    /// assert_eq!(Float::from(100).is_integer(), true);
30    /// assert_eq!(Float::from(-100).is_integer(), true);
31    /// assert_eq!(Float::ONE_HALF.is_integer(), false);
32    /// assert_eq!((-Float::ONE_HALF).is_integer(), false);
33    /// ```
34    #[inline]
35    fn is_integer(self) -> bool {
36        Integer::convertible_from(self)
37    }
38}