malachite_q/conversion/to_numerator_and_denominator.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_nz::natural::Natural;
11
12impl Rational {
13 /// Extracts the numerator of a [`Rational`], taking the [`Rational`] by reference and cloning.
14 ///
15 /// # Worst-case complexity
16 /// $T(n) = O(n)$
17 ///
18 /// $M(n) = O(n)$
19 ///
20 /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
21 ///
22 /// # Examples
23 /// ```
24 /// use malachite_q::Rational;
25 /// use std::str::FromStr;
26 ///
27 /// assert_eq!(Rational::from_str("2/3").unwrap().to_numerator(), 2);
28 /// assert_eq!(Rational::from_str("0").unwrap().to_numerator(), 0);
29 /// ```
30 #[inline]
31 pub fn to_numerator(&self) -> Natural {
32 self.numerator.clone()
33 }
34
35 /// Extracts the denominator of a [`Rational`], taking the [`Rational`] by reference and
36 /// cloning.
37 ///
38 /// # Worst-case complexity
39 /// $T(n) = O(n)$
40 ///
41 /// $M(n) = O(n)$
42 ///
43 /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
44 ///
45 /// # Examples
46 /// ```
47 /// use malachite_q::Rational;
48 /// use std::str::FromStr;
49 ///
50 /// assert_eq!(Rational::from_str("2/3").unwrap().to_denominator(), 3);
51 /// assert_eq!(Rational::from_str("0").unwrap().to_denominator(), 1);
52 /// ```
53 #[inline]
54 pub fn to_denominator(&self) -> Natural {
55 self.denominator.clone()
56 }
57
58 /// Extracts the numerator and denominator of a [`Rational`], taking the [`Rational`] by
59 /// reference and cloning.
60 ///
61 /// # Worst-case complexity
62 /// $T(n) = O(n)$
63 ///
64 /// $M(n) = O(n)$
65 ///
66 /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
67 ///
68 /// # Examples
69 /// ```
70 /// use malachite_base::strings::ToDebugString;
71 /// use malachite_q::Rational;
72 /// use std::str::FromStr;
73 ///
74 /// assert_eq!(
75 /// Rational::from_str("2/3")
76 /// .unwrap()
77 /// .to_numerator_and_denominator()
78 /// .to_debug_string(),
79 /// "(2, 3)"
80 /// );
81 /// assert_eq!(
82 /// Rational::from_str("0")
83 /// .unwrap()
84 /// .to_numerator_and_denominator()
85 /// .to_debug_string(),
86 /// "(0, 1)"
87 /// );
88 /// ```
89 #[inline]
90 pub fn to_numerator_and_denominator(&self) -> (Natural, Natural) {
91 (self.numerator.clone(), self.denominator.clone())
92 }
93
94 /// Extracts the numerator of a [`Rational`], taking the [`Rational`] by value.
95 ///
96 /// # Worst-case complexity
97 /// Constant time and additional memory.
98 ///
99 /// # Examples
100 /// ```
101 /// use malachite_q::Rational;
102 /// use std::str::FromStr;
103 ///
104 /// assert_eq!(Rational::from_str("2/3").unwrap().into_numerator(), 2);
105 /// assert_eq!(Rational::from_str("0").unwrap().into_numerator(), 0);
106 /// ```
107 #[inline]
108 #[allow(clippy::missing_const_for_fn)]
109 pub fn into_numerator(self) -> Natural {
110 self.numerator
111 }
112
113 /// Extracts the denominator of a [`Rational`], taking the [`Rational`] by value.
114 ///
115 /// # Worst-case complexity
116 /// Constant time and additional memory.
117 ///
118 /// # Examples
119 /// ```
120 /// use malachite_q::Rational;
121 /// use std::str::FromStr;
122 ///
123 /// assert_eq!(Rational::from_str("2/3").unwrap().into_denominator(), 3);
124 /// assert_eq!(Rational::from_str("0").unwrap().into_denominator(), 1);
125 /// ```
126 #[inline]
127 #[allow(clippy::missing_const_for_fn)]
128 pub fn into_denominator(self) -> Natural {
129 self.denominator
130 }
131
132 /// Extracts the numerator and denominator of a [`Rational`], taking the [`Rational`] by value.
133 ///
134 /// # Worst-case complexity
135 /// Constant time and additional memory.
136 ///
137 /// # Examples
138 /// ```
139 /// use malachite_base::strings::ToDebugString;
140 /// use malachite_q::Rational;
141 /// use std::str::FromStr;
142 ///
143 /// assert_eq!(
144 /// Rational::from_str("2/3")
145 /// .unwrap()
146 /// .into_numerator_and_denominator()
147 /// .to_debug_string(),
148 /// "(2, 3)"
149 /// );
150 /// assert_eq!(
151 /// Rational::from_str("0")
152 /// .unwrap()
153 /// .into_numerator_and_denominator()
154 /// .to_debug_string(),
155 /// "(0, 1)"
156 /// );
157 /// ```
158 #[inline]
159 #[allow(clippy::missing_const_for_fn)]
160 pub fn into_numerator_and_denominator(self) -> (Natural, Natural) {
161 (self.numerator, self.denominator)
162 }
163
164 /// Returns a reference to the numerator of a [`Rational`].
165 ///
166 /// # Worst-case complexity
167 /// Constant time and additional memory.
168 ///
169 /// # Examples
170 /// ```
171 /// use malachite_q::Rational;
172 /// use std::str::FromStr;
173 ///
174 /// assert_eq!(*Rational::from_str("2/3").unwrap().numerator_ref(), 2);
175 /// assert_eq!(*Rational::from_str("0").unwrap().numerator_ref(), 0);
176 /// ```
177 #[inline]
178 pub const fn numerator_ref(&self) -> &Natural {
179 &self.numerator
180 }
181
182 /// Returns a reference to the denominator of a [`Rational`].
183 ///
184 /// # Worst-case complexity
185 /// Constant time and additional memory.
186 ///
187 /// # Examples
188 /// ```
189 /// use malachite_q::Rational;
190 /// use std::str::FromStr;
191 ///
192 /// assert_eq!(*Rational::from_str("2/3").unwrap().denominator_ref(), 3);
193 /// assert_eq!(*Rational::from_str("0").unwrap().denominator_ref(), 1);
194 /// ```
195 #[inline]
196 pub const fn denominator_ref(&self) -> &Natural {
197 &self.denominator
198 }
199
200 /// Returns references to the numeraror and denominator of a [`Rational`].
201 ///
202 /// # Worst-case complexity
203 /// Constant time and additional memory.
204 ///
205 /// # Examples
206 /// ```
207 /// use malachite_base::strings::ToDebugString;
208 /// use malachite_q::Rational;
209 /// use std::str::FromStr;
210 ///
211 /// assert_eq!(
212 /// Rational::from_str("2/3")
213 /// .unwrap()
214 /// .numerator_and_denominator_ref()
215 /// .to_debug_string(),
216 /// "(2, 3)"
217 /// );
218 /// assert_eq!(
219 /// Rational::from_str("0")
220 /// .unwrap()
221 /// .numerator_and_denominator_ref()
222 /// .to_debug_string(),
223 /// "(0, 1)"
224 /// );
225 /// ```
226 #[inline]
227 pub const fn numerator_and_denominator_ref(&self) -> (&Natural, &Natural) {
228 (&self.numerator, &self.denominator)
229 }
230}