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