malachite_q/conversion/
from_primitive_int.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::basic::traits::One;
11use malachite_nz::natural::Natural;
12use malachite_nz::platform::{Limb, SignedLimb};
13
14impl Rational {
15    /// Converts a [`Limb`](crate#limbs) to a [`Rational`].
16    ///
17    /// This function is const, so it may be used to define constants.
18    ///
19    /// # Worst-case complexity
20    /// Constant time and additional memory.
21    ///
22    /// # Examples
23    /// ```
24    /// use malachite_q::Rational;
25    ///
26    /// const TEN: Rational = Rational::const_from_unsigned(10);
27    /// assert_eq!(TEN, 10);
28    /// ```
29    pub const fn const_from_unsigned(x: Limb) -> Rational {
30        Rational {
31            sign: true,
32            numerator: Natural::const_from(x),
33            denominator: Natural::ONE,
34        }
35    }
36
37    /// Converts a [`SignedLimb`](crate#limbs) to a [`Rational`].
38    ///
39    /// This function is const, so it may be used to define constants.
40    ///
41    /// # Worst-case complexity
42    /// Constant time and additional memory.
43    ///
44    /// # Examples
45    /// ```
46    /// use malachite_q::Rational;
47    ///
48    /// const TEN: Rational = Rational::const_from_signed(10);
49    /// assert_eq!(TEN, 10);
50    ///
51    /// const NEGATIVE_TEN: Rational = Rational::const_from_signed(-10);
52    /// assert_eq!(NEGATIVE_TEN, -10);
53    /// ```
54    pub const fn const_from_signed(x: SignedLimb) -> Rational {
55        Rational {
56            sign: x >= 0,
57            numerator: Natural::const_from(x.unsigned_abs()),
58            denominator: Natural::ONE,
59        }
60    }
61}
62
63macro_rules! impl_from_unsigned {
64    ($t: ident) => {
65        impl From<$t> for Rational {
66            /// Converts an unsigned primitive integer to a [`Rational`].
67            ///
68            /// # Worst-case complexity
69            /// Constant time and additional memory.
70            ///
71            /// # Examples
72            /// See [here](super::from_primitive_int#from).
73            #[inline]
74            fn from(u: $t) -> Rational {
75                Rational {
76                    sign: true,
77                    numerator: Natural::from(u),
78                    denominator: Natural::ONE,
79                }
80            }
81        }
82    };
83}
84apply_to_unsigneds!(impl_from_unsigned);
85
86macro_rules! impl_from_signed {
87    ($t: ident) => {
88        impl From<$t> for Rational {
89            /// Converts a signed primitive integer to a [`Rational`].
90            ///
91            /// # Worst-case complexity
92            /// Constant time and additional memory.
93            ///
94            /// # Examples
95            /// See [here](super::from_primitive_int#from).
96            #[inline]
97            fn from(i: $t) -> Rational {
98                Rational {
99                    sign: i >= 0,
100                    numerator: Natural::from(i.unsigned_abs()),
101                    denominator: Natural::ONE,
102                }
103            }
104        }
105    };
106}
107apply_to_signeds!(impl_from_signed);