malachite_float/float/arithmetic/power_of_10.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, emulate_float_to_float_fn, emulate_rational_to_float_fn};
10use core::cmp::Ordering;
11use malachite_base::num::arithmetic::traits::{PowerOf10, PowerOf10Assign};
12use malachite_base::num::basic::floats::PrimitiveFloat;
13use malachite_base::num::conversion::traits::{ExactFrom, RoundingFrom};
14use malachite_base::num::logic::traits::SignificantBits;
15use malachite_base::rounding_modes::RoundingMode::{self, *};
16use malachite_q::Rational;
17
18impl Float {
19 #[allow(clippy::needless_pass_by_value)]
20 /// Computes $10^x$, where $x$ is a [`Float`], rounding the result to the specified precision
21 /// and with the specified rounding mode. The [`Float`] is taken by value. An [`Ordering`] is
22 /// also returned, indicating whether the rounded power is less than, equal to, or greater than
23 /// the exact power. Although `NaN`s are not comparable to any [`Float`], whenever this function
24 /// returns a `NaN` it also returns `Equal`.
25 ///
26 /// See [`RoundingMode`] for a description of the possible rounding modes.
27 ///
28 /// $$
29 /// f(x,p,m) = 10^x+\varepsilon.
30 /// $$
31 /// - If $10^x$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
32 /// - If $10^x$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
33 /// 2^{\lfloor\log_2 10^x\rfloor-p+1}$.
34 /// - If $10^x$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
35 /// 2^{\lfloor\log_2 10^x\rfloor-p}$.
36 ///
37 /// If the output has a precision, it is `prec`.
38 ///
39 /// Special cases:
40 /// - $f(\text{NaN},p,m)=\text{NaN}$
41 /// - $f(\infty,p,m)=\infty$
42 /// - $f(-\infty,p,m)=0.0$
43 /// - $f(\pm0.0,p,m)=1.0$
44 ///
45 /// Overflow and underflow:
46 /// - If $f(x,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
47 /// returned instead.
48 /// - If $f(x,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor` or `Down`, $(1-(1/2)^p)2^{2^{30}-1}$ is
49 /// returned instead.
50 /// - If $f(x,p,m)<2^{-2^{30}}$ and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
51 /// - If $f(x,p,m)<2^{-2^{30}}$ and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned instead.
52 /// - If $f(x,p,m)\leq2^{-2^{30}-1}$ and $m$ is `Nearest`, $0.0$ is returned instead.
53 /// - If $2^{-2^{30}-1}<f(x,p,m)<2^{-2^{30}}$ and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
54 /// instead.
55 ///
56 /// If you know you'll be using `Nearest`, consider using [`Float::power_of_10_of_float_prec`]
57 /// instead. If you know that your target precision is the precision of the input, consider
58 /// using [`Float::power_of_10_of_float_round`] instead. If both of these things are true,
59 /// consider using the [`PowerOf10`] implementation instead.
60 ///
61 /// # Worst-case complexity
62 /// $T(n, m) = O(n^{3/2} \log n \log\log n + m)$
63 ///
64 /// $M(n, m) = O(n \log n + m)$
65 ///
66 /// where $T$ is time, $M$ is additional memory, $n$ is `prec`, and $m$ is
67 /// `self.significant_bits()`.
68 ///
69 /// # Panics
70 /// Panics if `prec` is zero, or if `rm` is `Exact` but the result cannot be represented exactly
71 /// with the given precision.
72 ///
73 /// # Examples
74 /// ```
75 /// use malachite_base::rounding_modes::RoundingMode::*;
76 /// use malachite_float::Float;
77 /// use std::cmp::Ordering::*;
78 ///
79 /// let (p, o) = Float::power_of_10_of_float_prec_round(Float::from(0.5), 20, Floor);
80 /// assert_eq!(p.to_string(), "3.1622772");
81 /// assert_eq!(o, Less);
82 ///
83 /// let (p, o) = Float::power_of_10_of_float_prec_round(Float::from(0.5), 20, Ceiling);
84 /// assert_eq!(p.to_string(), "3.1622810");
85 /// assert_eq!(o, Greater);
86 /// ```
87 #[inline]
88 pub fn power_of_10_of_float_prec_round(
89 pow: Self,
90 prec: u64,
91 rm: RoundingMode,
92 ) -> (Self, Ordering) {
93 Self::unsigned_pow_prec_round(10, pow, prec, rm)
94 }
95
96 /// Computes $10^x$, where $x$ is a [`Float`], rounding the result to the specified precision
97 /// and with the specified rounding mode. The [`Float`] is taken by reference. An [`Ordering`]
98 /// is also returned, indicating whether the rounded power is less than, equal to, or greater
99 /// than the exact power. Although `NaN`s are not comparable to any [`Float`], whenever this
100 /// function returns a `NaN` it also returns `Equal`.
101 ///
102 /// See [`RoundingMode`] for a description of the possible rounding modes.
103 ///
104 /// $$
105 /// f(x,p,m) = 10^x+\varepsilon.
106 /// $$
107 /// - If $10^x$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
108 /// - If $10^x$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
109 /// 2^{\lfloor\log_2 10^x\rfloor-p+1}$.
110 /// - If $10^x$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
111 /// 2^{\lfloor\log_2 10^x\rfloor-p}$.
112 ///
113 /// If the output has a precision, it is `prec`.
114 ///
115 /// Special cases:
116 /// - $f(\text{NaN},p,m)=\text{NaN}$
117 /// - $f(\infty,p,m)=\infty$
118 /// - $f(-\infty,p,m)=0.0$
119 /// - $f(\pm0.0,p,m)=1.0$
120 ///
121 /// Overflow and underflow:
122 /// - If $f(x,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
123 /// returned instead.
124 /// - If $f(x,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor` or `Down`, $(1-(1/2)^p)2^{2^{30}-1}$ is
125 /// returned instead.
126 /// - If $f(x,p,m)<2^{-2^{30}}$ and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
127 /// - If $f(x,p,m)<2^{-2^{30}}$ and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned instead.
128 /// - If $f(x,p,m)\leq2^{-2^{30}-1}$ and $m$ is `Nearest`, $0.0$ is returned instead.
129 /// - If $2^{-2^{30}-1}<f(x,p,m)<2^{-2^{30}}$ and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
130 /// instead.
131 ///
132 /// If you know you'll be using `Nearest`, consider using
133 /// [`Float::power_of_10_of_float_prec_ref`] instead. If you know that your target precision is
134 /// the precision of the input, consider using [`Float::power_of_10_of_float_round_ref`]
135 /// instead. If both of these things are true, consider using the [`PowerOf10`] implementation
136 /// instead.
137 ///
138 /// # Worst-case complexity
139 /// $T(n, m) = O(n^{3/2} \log n \log\log n + m)$
140 ///
141 /// $M(n, m) = O(n \log n + m)$
142 ///
143 /// where $T$ is time, $M$ is additional memory, $n$ is `prec`, and $m$ is
144 /// `self.significant_bits()`.
145 ///
146 /// # Panics
147 /// Panics if `prec` is zero, or if `rm` is `Exact` but the result cannot be represented exactly
148 /// with the given precision.
149 ///
150 /// # Examples
151 /// ```
152 /// use malachite_base::rounding_modes::RoundingMode::*;
153 /// use malachite_float::Float;
154 /// use std::cmp::Ordering::*;
155 ///
156 /// let (p, o) = Float::power_of_10_of_float_prec_round_ref(&Float::from(0.5), 20, Floor);
157 /// assert_eq!(p.to_string(), "3.1622772");
158 /// assert_eq!(o, Less);
159 ///
160 /// let (p, o) = Float::power_of_10_of_float_prec_round_ref(&Float::from(0.5), 20, Ceiling);
161 /// assert_eq!(p.to_string(), "3.1622810");
162 /// assert_eq!(o, Greater);
163 /// ```
164 #[inline]
165 pub fn power_of_10_of_float_prec_round_ref(
166 pow: &Self,
167 prec: u64,
168 rm: RoundingMode,
169 ) -> (Self, Ordering) {
170 Self::unsigned_pow_prec_round_ref(10, pow, prec, rm)
171 }
172
173 #[allow(clippy::needless_pass_by_value)]
174 /// Computes $10^x$, where $x$ is a [`Float`], rounding the result to the nearest value of the
175 /// specified precision. The [`Float`] is taken by value. An [`Ordering`] is also returned,
176 /// indicating whether the rounded power is less than, equal to, or greater than the exact
177 /// power. Although `NaN`s are not comparable to any [`Float`], whenever this function returns a
178 /// `NaN` it also returns `Equal`.
179 ///
180 /// If the power is equidistant from two [`Float`]s with the specified precision, the [`Float`]
181 /// with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of
182 /// the `Nearest` rounding mode.
183 ///
184 /// $$
185 /// f(x,p) = 10^x+\varepsilon.
186 /// $$
187 /// - If $10^x$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
188 /// - If $10^x$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 10^x\rfloor-p}$.
189 ///
190 /// If the output has a precision, it is `prec`.
191 ///
192 /// Special cases:
193 /// - $f(\text{NaN},p)=\text{NaN}$
194 /// - $f(\infty,p)=\infty$
195 /// - $f(-\infty,p)=0.0$
196 /// - $f(\pm0.0,p)=1.0$
197 ///
198 /// Overflow and underflow:
199 /// - If $f(x,p)\geq 2^{2^{30}-1}$, $\infty$ is returned instead.
200 /// - If $f(x,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
201 /// - If $2^{-2^{30}-1}<f(x,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
202 ///
203 /// If you want to use a rounding mode other than `Nearest`, consider using
204 /// [`Float::power_of_10_of_float_prec_round`] instead. If you know that your target precision
205 /// is the precision of the input, consider using the [`PowerOf10`] implementation instead.
206 ///
207 /// # Worst-case complexity
208 /// $T(n, m) = O(n^{3/2} \log n \log\log n + m)$
209 ///
210 /// $M(n, m) = O(n \log n + m)$
211 ///
212 /// where $T$ is time, $M$ is additional memory, $n$ is `prec`, and $m$ is
213 /// `self.significant_bits()`.
214 ///
215 /// # Panics
216 /// Panics if `prec` is zero.
217 ///
218 /// # Examples
219 /// ```
220 /// use malachite_float::Float;
221 /// use std::cmp::Ordering::*;
222 ///
223 /// let (p, o) = Float::power_of_10_of_float_prec(Float::from(0.5), 20);
224 /// assert_eq!(p.to_string(), "3.1622772");
225 /// assert_eq!(o, Less);
226 ///
227 /// let (p, o) = Float::power_of_10_of_float_prec(Float::from(0.5), 53);
228 /// assert_eq!(p.to_string(), "3.1622776601683795");
229 /// assert_eq!(o, Greater);
230 /// ```
231 #[inline]
232 pub fn power_of_10_of_float_prec(pow: Self, prec: u64) -> (Self, Ordering) {
233 Self::power_of_10_of_float_prec_round(pow, prec, Nearest)
234 }
235
236 /// Computes $10^x$, where $x$ is a [`Float`], rounding the result to the nearest value of the
237 /// specified precision. The [`Float`] is taken by reference. An [`Ordering`] is also returned,
238 /// indicating whether the rounded power is less than, equal to, or greater than the exact
239 /// power. Although `NaN`s are not comparable to any [`Float`], whenever this function returns a
240 /// `NaN` it also returns `Equal`.
241 ///
242 /// If the power is equidistant from two [`Float`]s with the specified precision, the [`Float`]
243 /// with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of
244 /// the `Nearest` rounding mode.
245 ///
246 /// $$
247 /// f(x,p) = 10^x+\varepsilon.
248 /// $$
249 /// - If $10^x$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
250 /// - If $10^x$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 10^x\rfloor-p}$.
251 ///
252 /// If the output has a precision, it is `prec`.
253 ///
254 /// Special cases:
255 /// - $f(\text{NaN},p)=\text{NaN}$
256 /// - $f(\infty,p)=\infty$
257 /// - $f(-\infty,p)=0.0$
258 /// - $f(\pm0.0,p)=1.0$
259 ///
260 /// Overflow and underflow:
261 /// - If $f(x,p)\geq 2^{2^{30}-1}$, $\infty$ is returned instead.
262 /// - If $f(x,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
263 /// - If $2^{-2^{30}-1}<f(x,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
264 ///
265 /// If you want to use a rounding mode other than `Nearest`, consider using
266 /// [`Float::power_of_10_of_float_prec_round_ref`] instead. If you know that your target
267 /// precision is the precision of the input, consider using the [`PowerOf10`] implementation
268 /// instead.
269 ///
270 /// # Worst-case complexity
271 /// $T(n, m) = O(n^{3/2} \log n \log\log n + m)$
272 ///
273 /// $M(n, m) = O(n \log n + m)$
274 ///
275 /// where $T$ is time, $M$ is additional memory, $n$ is `prec`, and $m$ is
276 /// `self.significant_bits()`.
277 ///
278 /// # Panics
279 /// Panics if `prec` is zero.
280 ///
281 /// # Examples
282 /// ```
283 /// use malachite_float::Float;
284 /// use std::cmp::Ordering::*;
285 ///
286 /// let (p, o) = Float::power_of_10_of_float_prec_ref(&Float::from(0.5), 20);
287 /// assert_eq!(p.to_string(), "3.1622772");
288 /// assert_eq!(o, Less);
289 ///
290 /// let (p, o) = Float::power_of_10_of_float_prec_ref(&Float::from(0.5), 53);
291 /// assert_eq!(p.to_string(), "3.1622776601683795");
292 /// assert_eq!(o, Greater);
293 /// ```
294 #[inline]
295 pub fn power_of_10_of_float_prec_ref(pow: &Self, prec: u64) -> (Self, Ordering) {
296 Self::power_of_10_of_float_prec_round_ref(pow, prec, Nearest)
297 }
298
299 #[allow(clippy::needless_pass_by_value)]
300 /// Computes $10^x$, where $x$ is a [`Float`], rounding the result with the specified rounding
301 /// mode. The [`Float`] is taken by value. An [`Ordering`] is also returned, indicating whether
302 /// the rounded power is less than, equal to, or greater than the exact power. Although `NaN`s
303 /// are not comparable to any [`Float`], whenever this function returns a `NaN` it also returns
304 /// `Equal`.
305 ///
306 /// The precision of the output is the precision of the input. See [`RoundingMode`] for a
307 /// description of the possible rounding modes.
308 ///
309 /// $$
310 /// f(x,m) = 10^x+\varepsilon.
311 /// $$
312 /// - If $10^x$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
313 /// - If $10^x$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
314 /// 2^{\lfloor\log_2 10^x\rfloor-p+1}$, where $p$ is the precision of the input.
315 /// - If $10^x$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
316 /// 2^{\lfloor\log_2 10^x\rfloor-p}$, where $p$ is the precision of the input.
317 ///
318 /// If the output has a precision, it is the precision of the input.
319 ///
320 /// Special cases:
321 /// - $f(\text{NaN},m)=\text{NaN}$
322 /// - $f(\infty,m)=\infty$
323 /// - $f(-\infty,m)=0.0$
324 /// - $f(\pm0.0,m)=1.0$
325 ///
326 /// See the [`Float::power_of_10_of_float_prec_round`] documentation for information on overflow
327 /// and underflow.
328 ///
329 /// If you want to specify an output precision, consider using
330 /// [`Float::power_of_10_of_float_prec_round`] instead. If you know you'll be using the
331 /// `Nearest` rounding mode, consider using the [`PowerOf10`] implementation instead.
332 ///
333 /// # Worst-case complexity
334 /// $T(n) = O(n^{3/2} \log n \log\log n)$
335 ///
336 /// $M(n) = O(n \log n)$
337 ///
338 /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
339 ///
340 /// # Panics
341 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the input
342 /// precision.
343 ///
344 /// # Examples
345 /// ```
346 /// use malachite_base::num::basic::traits::OneHalf;
347 /// use malachite_base::rounding_modes::RoundingMode::*;
348 /// use malachite_float::Float;
349 /// use malachite_q::Rational;
350 /// use std::cmp::Ordering::*;
351 ///
352 /// let x = Float::from_rational_prec(Rational::ONE_HALF, 20).0;
353 ///
354 /// let (p, o) = Float::power_of_10_of_float_round(x.clone(), Floor);
355 /// assert_eq!(p.to_string(), "3.1622772");
356 /// assert_eq!(o, Less);
357 ///
358 /// let (p, o) = Float::power_of_10_of_float_round(x, Ceiling);
359 /// assert_eq!(p.to_string(), "3.1622810");
360 /// assert_eq!(o, Greater);
361 /// ```
362 #[inline]
363 pub fn power_of_10_of_float_round(pow: Self, rm: RoundingMode) -> (Self, Ordering) {
364 let prec = pow.significant_bits();
365 Self::power_of_10_of_float_prec_round_ref(&pow, prec, rm)
366 }
367
368 /// Computes $10^x$, where $x$ is a [`Float`], rounding the result with the specified rounding
369 /// mode. The [`Float`] is taken by reference. An [`Ordering`] is also returned, indicating
370 /// whether the rounded power is less than, equal to, or greater than the exact power. Although
371 /// `NaN`s are not comparable to any [`Float`], whenever this function returns a `NaN` it also
372 /// returns `Equal`.
373 ///
374 /// The precision of the output is the precision of the input. See [`RoundingMode`] for a
375 /// description of the possible rounding modes.
376 ///
377 /// $$
378 /// f(x,m) = 10^x+\varepsilon.
379 /// $$
380 /// - If $10^x$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
381 /// - If $10^x$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
382 /// 2^{\lfloor\log_2 10^x\rfloor-p+1}$, where $p$ is the precision of the input.
383 /// - If $10^x$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
384 /// 2^{\lfloor\log_2 10^x\rfloor-p}$, where $p$ is the precision of the input.
385 ///
386 /// If the output has a precision, it is the precision of the input.
387 ///
388 /// Special cases:
389 /// - $f(\text{NaN},m)=\text{NaN}$
390 /// - $f(\infty,m)=\infty$
391 /// - $f(-\infty,m)=0.0$
392 /// - $f(\pm0.0,m)=1.0$
393 ///
394 /// See the [`Float::power_of_10_of_float_prec_round`] documentation for information on overflow
395 /// and underflow.
396 ///
397 /// If you want to specify an output precision, consider using
398 /// [`Float::power_of_10_of_float_prec_round_ref`] instead. If you know you'll be using the
399 /// `Nearest` rounding mode, consider using the [`PowerOf10`] implementation instead.
400 ///
401 /// # Worst-case complexity
402 /// $T(n) = O(n^{3/2} \log n \log\log n)$
403 ///
404 /// $M(n) = O(n \log n)$
405 ///
406 /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
407 ///
408 /// # Panics
409 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the input
410 /// precision.
411 ///
412 /// # Examples
413 /// ```
414 /// use malachite_base::num::basic::traits::OneHalf;
415 /// use malachite_base::rounding_modes::RoundingMode::*;
416 /// use malachite_float::Float;
417 /// use malachite_q::Rational;
418 /// use std::cmp::Ordering::*;
419 ///
420 /// let x = Float::from_rational_prec(Rational::ONE_HALF, 20).0;
421 ///
422 /// let (p, o) = Float::power_of_10_of_float_round_ref(&x, Floor);
423 /// assert_eq!(p.to_string(), "3.1622772");
424 /// assert_eq!(o, Less);
425 ///
426 /// let (p, o) = Float::power_of_10_of_float_round_ref(&x, Ceiling);
427 /// assert_eq!(p.to_string(), "3.1622810");
428 /// assert_eq!(o, Greater);
429 /// ```
430 #[inline]
431 pub fn power_of_10_of_float_round_ref(pow: &Self, rm: RoundingMode) -> (Self, Ordering) {
432 Self::power_of_10_of_float_prec_round_ref(pow, pow.significant_bits(), rm)
433 }
434
435 /// Computes $10^x$, where $x$ is a [`Float`], in place, rounding the result to the specified
436 /// precision and with the specified rounding mode. An [`Ordering`] is returned, indicating
437 /// whether the rounded power is less than, equal to, or greater than the exact power. Although
438 /// `NaN`s are not comparable to any [`Float`], whenever this function sets the [`Float`] to
439 /// `NaN` it also returns `Equal`.
440 ///
441 /// See [`RoundingMode`] for a description of the possible rounding modes.
442 ///
443 /// $$
444 /// x \gets 10^x+\varepsilon.
445 /// $$
446 /// - If $10^x$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
447 /// - If $10^x$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
448 /// 2^{\lfloor\log_2 10^x\rfloor-p+1}$.
449 /// - If $10^x$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
450 /// 2^{\lfloor\log_2 10^x\rfloor-p}$.
451 ///
452 /// If the output has a precision, it is `prec`.
453 ///
454 /// See the [`Float::power_of_10_of_float_prec_round`] documentation for information on special
455 /// cases, overflow, and underflow.
456 ///
457 /// If you know you'll be using `Nearest`, consider using
458 /// [`Float::power_of_10_of_float_prec_assign`] instead. If you know that your target precision
459 /// is the precision of the input, consider using [`Float::power_of_10_of_float_round_assign`]
460 /// instead. If both of these things are true, consider using the [`PowerOf10Assign`]
461 /// implementation instead.
462 ///
463 /// # Worst-case complexity
464 /// $T(n, m) = O(n^{3/2} \log n \log\log n + m)$
465 ///
466 /// $M(n, m) = O(n \log n + m)$
467 ///
468 /// where $T$ is time, $M$ is additional memory, $n$ is `prec`, and $m$ is
469 /// `self.significant_bits()`.
470 ///
471 /// # Panics
472 /// Panics if `prec` is zero, or if `rm` is `Exact` but the result cannot be represented exactly
473 /// with the given precision.
474 ///
475 /// # Examples
476 /// ```
477 /// use malachite_base::rounding_modes::RoundingMode::*;
478 /// use malachite_float::Float;
479 /// use std::cmp::Ordering::*;
480 ///
481 /// let mut x = Float::from(0.5);
482 /// assert_eq!(x.power_of_10_of_float_prec_round_assign(20, Floor), Less);
483 /// assert_eq!(x.to_string(), "3.1622772");
484 /// ```
485 #[inline]
486 pub fn power_of_10_of_float_prec_round_assign(
487 &mut self,
488 prec: u64,
489 rm: RoundingMode,
490 ) -> Ordering {
491 let (result, o) = Self::power_of_10_of_float_prec_round_ref(self, prec, rm);
492 *self = result;
493 o
494 }
495
496 /// Computes $10^x$, where $x$ is a [`Float`], in place, rounding the result to the nearest
497 /// value of the specified precision. An [`Ordering`] is returned, indicating whether the
498 /// rounded power is less than, equal to, or greater than the exact power. Although `NaN`s are
499 /// not comparable to any [`Float`], whenever this function sets the [`Float`] to `NaN` it also
500 /// returns `Equal`.
501 ///
502 /// If the power is equidistant from two [`Float`]s with the specified precision, the [`Float`]
503 /// with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of
504 /// the `Nearest` rounding mode.
505 ///
506 /// $$
507 /// x \gets 10^x+\varepsilon.
508 /// $$
509 /// - If $10^x$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
510 /// - If $10^x$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 10^x\rfloor-p}$.
511 ///
512 /// If the output has a precision, it is `prec`.
513 ///
514 /// See the [`Float::power_of_10_of_float_prec`] documentation for information on special cases,
515 /// overflow, and underflow.
516 ///
517 /// If you want to use a rounding mode other than `Nearest`, consider using
518 /// [`Float::power_of_10_of_float_prec_round_assign`] instead. If you know that your target
519 /// precision is the precision of the input, consider using the [`PowerOf10Assign`]
520 /// implementation instead.
521 ///
522 /// # Worst-case complexity
523 /// $T(n, m) = O(n^{3/2} \log n \log\log n + m)$
524 ///
525 /// $M(n, m) = O(n \log n + m)$
526 ///
527 /// where $T$ is time, $M$ is additional memory, $n$ is `prec`, and $m$ is
528 /// `self.significant_bits()`.
529 ///
530 /// # Panics
531 /// Panics if `prec` is zero.
532 ///
533 /// # Examples
534 /// ```
535 /// use malachite_float::Float;
536 /// use std::cmp::Ordering::*;
537 ///
538 /// let mut x = Float::from(0.5);
539 /// assert_eq!(x.power_of_10_of_float_prec_assign(20), Less);
540 /// assert_eq!(x.to_string(), "3.1622772");
541 /// ```
542 #[inline]
543 pub fn power_of_10_of_float_prec_assign(&mut self, prec: u64) -> Ordering {
544 self.power_of_10_of_float_prec_round_assign(prec, Nearest)
545 }
546
547 /// Computes $10^x$, where $x$ is a [`Float`], in place, rounding the result with the specified
548 /// rounding mode. An [`Ordering`] is returned, indicating whether the rounded power is less
549 /// than, equal to, or greater than the exact power. Although `NaN`s are not comparable to any
550 /// [`Float`], whenever this function sets the [`Float`] to `NaN` it also returns `Equal`.
551 ///
552 /// The precision of the output is the precision of the input. See [`RoundingMode`] for a
553 /// description of the possible rounding modes.
554 ///
555 /// $$
556 /// x \gets 10^x+\varepsilon.
557 /// $$
558 /// - If $10^x$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
559 /// - If $10^x$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
560 /// 2^{\lfloor\log_2 10^x\rfloor-p+1}$, where $p$ is the precision of the input.
561 /// - If $10^x$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
562 /// 2^{\lfloor\log_2 10^x\rfloor-p}$, where $p$ is the precision of the input.
563 ///
564 /// If the output has a precision, it is the precision of the input.
565 ///
566 /// See the [`Float::power_of_10_of_float_round`] documentation for information on special
567 /// cases, overflow, and underflow.
568 ///
569 /// If you want to specify an output precision, consider using
570 /// [`Float::power_of_10_of_float_prec_round_assign`] instead. If you know you'll be using the
571 /// `Nearest` rounding mode, consider using the [`PowerOf10Assign`] implementation instead.
572 ///
573 /// # Worst-case complexity
574 /// $T(n) = O(n^{3/2} \log n \log\log n)$
575 ///
576 /// $M(n) = O(n \log n)$
577 ///
578 /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
579 ///
580 /// # Panics
581 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the input
582 /// precision.
583 ///
584 /// # Examples
585 /// ```
586 /// use malachite_base::num::basic::traits::OneHalf;
587 /// use malachite_base::rounding_modes::RoundingMode::*;
588 /// use malachite_float::Float;
589 /// use malachite_q::Rational;
590 /// use std::cmp::Ordering::*;
591 ///
592 /// let x = Float::from_rational_prec(Rational::ONE_HALF, 20).0;
593 ///
594 /// let mut x = x;
595 /// assert_eq!(x.power_of_10_of_float_round_assign(Ceiling), Greater);
596 /// assert_eq!(x.to_string(), "3.1622810");
597 /// ```
598 #[inline]
599 pub fn power_of_10_of_float_round_assign(&mut self, rm: RoundingMode) -> Ordering {
600 self.power_of_10_of_float_prec_round_assign(self.significant_bits(), rm)
601 }
602
603 #[allow(clippy::needless_pass_by_value)]
604 /// Computes $10^x$, where $x$ is a [`Rational`], rounding the result to the specified precision
605 /// and with the specified rounding mode and returning the result as a [`Float`]. The
606 /// [`Rational`] is taken by value. An [`Ordering`] is also returned, indicating whether the
607 /// rounded power is less than, equal to, or greater than the exact power.
608 ///
609 /// See [`RoundingMode`] for a description of the possible rounding modes.
610 ///
611 /// $$
612 /// f(x,p,m) = 10^x+\varepsilon.
613 /// $$
614 /// - If $m$ is not `Nearest`, then $|\varepsilon| < 2^{\lfloor\log_2 10^x\rfloor-p+1}$.
615 /// - If $m$ is `Nearest`, then $|\varepsilon| \leq 2^{\lfloor\log_2 10^x\rfloor-p}$.
616 ///
617 /// These bounds do not apply when the result overflows or underflows; see below.
618 ///
619 /// The output has precision `prec`.
620 ///
621 /// Special cases:
622 /// - $f(0,p,m)=1$.
623 ///
624 /// Overflow and underflow:
625 /// - If $f(x,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
626 /// returned instead.
627 /// - If $f(x,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor` or `Down`, $(1-(1/2)^p)2^{2^{30}-1}$ is
628 /// returned instead.
629 /// - If $f(x,p,m)<2^{-2^{30}}$ and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
630 /// - If $f(x,p,m)<2^{-2^{30}}$ and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned instead.
631 /// - If $f(x,p,m)\leq2^{-2^{30}-1}$ and $m$ is `Nearest`, $0.0$ is returned instead.
632 /// - If $2^{-2^{30}-1}<f(x,p,m)<2^{-2^{30}}$ and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
633 /// instead.
634 ///
635 /// If you know you'll be using `Nearest`, consider using [`Float::power_of_10_rational_prec`]
636 /// instead.
637 ///
638 /// # Worst-case complexity
639 /// $T(n, m) = O(n^{3/2} \log n \log\log n + m (\log m)^2 \log\log m)$
640 ///
641 /// $M(n, m) = O(n \log n + m \log m)$
642 ///
643 /// where $T$ is time, $M$ is additional memory, $n$ is `prec`, and $m$ is
644 /// `x.significant_bits()`.
645 ///
646 /// # Panics
647 /// Panics if `prec` is zero, or if `rm` is `Exact` but the result cannot be represented exactly
648 /// with the given precision (which is the case unless $x$ is a nonnegative integer).
649 ///
650 /// # Examples
651 /// ```
652 /// use malachite_base::rounding_modes::RoundingMode::*;
653 /// use malachite_float::Float;
654 /// use malachite_q::Rational;
655 /// use std::cmp::Ordering::*;
656 ///
657 /// let (p, o) =
658 /// Float::power_of_10_rational_prec_round(Rational::from_unsigneds(3u8, 5), 5, Floor);
659 /// assert_eq!(p.to_string(), "3.88");
660 /// assert_eq!(o, Less);
661 ///
662 /// let (p, o) =
663 /// Float::power_of_10_rational_prec_round(Rational::from_unsigneds(3u8, 5), 5, Ceiling);
664 /// assert_eq!(p.to_string(), "4.00");
665 /// assert_eq!(o, Greater);
666 ///
667 /// let (p, o) =
668 /// Float::power_of_10_rational_prec_round(Rational::from_unsigneds(3u8, 5), 20, Floor);
669 /// assert_eq!(p.to_string(), "3.9810715");
670 /// assert_eq!(o, Less);
671 ///
672 /// let (p, o) =
673 /// Float::power_of_10_rational_prec_round(Rational::from_unsigneds(3u8, 5), 20, Ceiling);
674 /// assert_eq!(p.to_string(), "3.9810753");
675 /// assert_eq!(o, Greater);
676 /// ```
677 #[inline]
678 pub fn power_of_10_rational_prec_round(
679 x: Rational,
680 prec: u64,
681 rm: RoundingMode,
682 ) -> (Self, Ordering) {
683 Self::unsigned_pow_rational_prec_round(10, x, prec, rm)
684 }
685
686 /// Computes $10^x$, where $x$ is a [`Rational`], rounding the result to the specified precision
687 /// and with the specified rounding mode and returning the result as a [`Float`]. The
688 /// [`Rational`] is taken by reference. An [`Ordering`] is also returned, indicating whether the
689 /// rounded power is less than, equal to, or greater than the exact power.
690 ///
691 /// See [`RoundingMode`] for a description of the possible rounding modes.
692 ///
693 /// $$
694 /// f(x,p,m) = 10^x+\varepsilon.
695 /// $$
696 /// - If $m$ is not `Nearest`, then $|\varepsilon| < 2^{\lfloor\log_2 10^x\rfloor-p+1}$.
697 /// - If $m$ is `Nearest`, then $|\varepsilon| \leq 2^{\lfloor\log_2 10^x\rfloor-p}$.
698 ///
699 /// These bounds do not apply when the result overflows or underflows; see below.
700 ///
701 /// The output has precision `prec`.
702 ///
703 /// Special cases:
704 /// - $f(0,p,m)=1$.
705 ///
706 /// Overflow and underflow:
707 /// - If $f(x,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
708 /// returned instead.
709 /// - If $f(x,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor` or `Down`, $(1-(1/2)^p)2^{2^{30}-1}$ is
710 /// returned instead.
711 /// - If $f(x,p,m)<2^{-2^{30}}$ and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
712 /// - If $f(x,p,m)<2^{-2^{30}}$ and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned instead.
713 /// - If $f(x,p,m)\leq2^{-2^{30}-1}$ and $m$ is `Nearest`, $0.0$ is returned instead.
714 /// - If $2^{-2^{30}-1}<f(x,p,m)<2^{-2^{30}}$ and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
715 /// instead.
716 ///
717 /// If you know you'll be using `Nearest`, consider using
718 /// [`Float::power_of_10_rational_prec_ref`] instead.
719 ///
720 /// # Worst-case complexity
721 /// $T(n, m) = O(n^{3/2} \log n \log\log n + m (\log m)^2 \log\log m)$
722 ///
723 /// $M(n, m) = O(n \log n + m \log m)$
724 ///
725 /// where $T$ is time, $M$ is additional memory, $n$ is `prec`, and $m$ is
726 /// `x.significant_bits()`.
727 ///
728 /// # Panics
729 /// Panics if `prec` is zero, or if `rm` is `Exact` but the result cannot be represented exactly
730 /// with the given precision (which is the case unless $x$ is a nonnegative integer).
731 ///
732 /// # Examples
733 /// ```
734 /// use malachite_base::rounding_modes::RoundingMode::*;
735 /// use malachite_float::Float;
736 /// use malachite_q::Rational;
737 /// use std::cmp::Ordering::*;
738 ///
739 /// let (p, o) =
740 /// Float::power_of_10_rational_prec_round_ref(&Rational::from_unsigneds(3u8, 5), 5, Floor);
741 /// assert_eq!(p.to_string(), "3.88");
742 /// assert_eq!(o, Less);
743 ///
744 /// let (p, o) = Float::power_of_10_rational_prec_round_ref(
745 /// &Rational::from_unsigneds(3u8, 5),
746 /// 5,
747 /// Ceiling,
748 /// );
749 /// assert_eq!(p.to_string(), "4.00");
750 /// assert_eq!(o, Greater);
751 ///
752 /// let (p, o) = Float::power_of_10_rational_prec_round_ref(
753 /// &Rational::from_unsigneds(3u8, 5),
754 /// 20,
755 /// Floor,
756 /// );
757 /// assert_eq!(p.to_string(), "3.9810715");
758 /// assert_eq!(o, Less);
759 ///
760 /// let (p, o) = Float::power_of_10_rational_prec_round_ref(
761 /// &Rational::from_unsigneds(3u8, 5),
762 /// 20,
763 /// Ceiling,
764 /// );
765 /// assert_eq!(p.to_string(), "3.9810753");
766 /// assert_eq!(o, Greater);
767 /// ```
768 #[inline]
769 pub fn power_of_10_rational_prec_round_ref(
770 x: &Rational,
771 prec: u64,
772 rm: RoundingMode,
773 ) -> (Self, Ordering) {
774 Self::unsigned_pow_rational_prec_round_ref(10, x, prec, rm)
775 }
776
777 #[allow(clippy::needless_pass_by_value)]
778 /// Computes $10^x$, where $x$ is a [`Rational`], rounding the result to the nearest value of
779 /// the specified precision and returning the result as a [`Float`]. The [`Rational`] is taken
780 /// by value. An [`Ordering`] is also returned, indicating whether the rounded power is less
781 /// than, equal to, or greater than the exact power.
782 ///
783 /// If the power is equidistant from two [`Float`]s with the specified precision, the [`Float`]
784 /// with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of
785 /// the `Nearest` rounding mode.
786 ///
787 /// $$
788 /// f(x,p) = 10^x+\varepsilon,
789 /// $$
790 /// where $|\varepsilon| \leq 2^{\lfloor\log_2 10^x\rfloor-p}$ (unless the result overflows or
791 /// underflows; see below).
792 ///
793 /// The output has precision `prec`.
794 ///
795 /// Special cases:
796 /// - $f(0,p)=1$.
797 ///
798 /// Overflow and underflow:
799 /// - If $f(x,p)\geq 2^{2^{30}-1}$, $\infty$ is returned instead.
800 /// - If $f(x,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
801 /// - If $2^{-2^{30}-1}<f(x,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
802 ///
803 /// If you want to use a rounding mode other than `Nearest`, consider using
804 /// [`Float::power_of_10_rational_prec_round`] instead.
805 ///
806 /// # Worst-case complexity
807 /// $T(n, m) = O(n^{3/2} \log n \log\log n + m (\log m)^2 \log\log m)$
808 ///
809 /// $M(n, m) = O(n \log n + m \log m)$
810 ///
811 /// where $T$ is time, $M$ is additional memory, $n$ is `prec`, and $m$ is
812 /// `x.significant_bits()`.
813 ///
814 /// # Panics
815 /// Panics if `prec` is zero.
816 ///
817 /// # Examples
818 /// ```
819 /// use malachite_base::num::basic::traits::Zero;
820 /// use malachite_float::Float;
821 /// use malachite_q::Rational;
822 /// use std::cmp::Ordering::*;
823 ///
824 /// let (p, o) = Float::power_of_10_rational_prec(Rational::from_unsigneds(3u8, 5), 5);
825 /// assert_eq!(p.to_string(), "4.00");
826 /// assert_eq!(o, Greater);
827 ///
828 /// let (p, o) = Float::power_of_10_rational_prec(Rational::from_unsigneds(3u8, 5), 20);
829 /// assert_eq!(p.to_string(), "3.9810715");
830 /// assert_eq!(o, Less);
831 ///
832 /// let (p, o) = Float::power_of_10_rational_prec(Rational::ZERO, 10);
833 /// assert_eq!(p.to_string(), "1.0000");
834 /// assert_eq!(o, Equal);
835 /// ```
836 #[inline]
837 pub fn power_of_10_rational_prec(x: Rational, prec: u64) -> (Self, Ordering) {
838 Self::power_of_10_rational_prec_round(x, prec, Nearest)
839 }
840
841 /// Computes $10^x$, where $x$ is a [`Rational`], rounding the result to the nearest value of
842 /// the specified precision and returning the result as a [`Float`]. The [`Rational`] is taken
843 /// by reference. An [`Ordering`] is also returned, indicating whether the rounded power is less
844 /// than, equal to, or greater than the exact power.
845 ///
846 /// If the power is equidistant from two [`Float`]s with the specified precision, the [`Float`]
847 /// with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of
848 /// the `Nearest` rounding mode.
849 ///
850 /// $$
851 /// f(x,p) = 10^x+\varepsilon,
852 /// $$
853 /// where $|\varepsilon| \leq 2^{\lfloor\log_2 10^x\rfloor-p}$ (unless the result overflows or
854 /// underflows; see below).
855 ///
856 /// The output has precision `prec`.
857 ///
858 /// Special cases:
859 /// - $f(0,p)=1$.
860 ///
861 /// Overflow and underflow:
862 /// - If $f(x,p)\geq 2^{2^{30}-1}$, $\infty$ is returned instead.
863 /// - If $f(x,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
864 /// - If $2^{-2^{30}-1}<f(x,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
865 ///
866 /// If you want to use a rounding mode other than `Nearest`, consider using
867 /// [`Float::power_of_10_rational_prec_round_ref`] instead.
868 ///
869 /// # Worst-case complexity
870 /// $T(n, m) = O(n^{3/2} \log n \log\log n + m (\log m)^2 \log\log m)$
871 ///
872 /// $M(n, m) = O(n \log n + m \log m)$
873 ///
874 /// where $T$ is time, $M$ is additional memory, $n$ is `prec`, and $m$ is
875 /// `x.significant_bits()`.
876 ///
877 /// # Panics
878 /// Panics if `prec` is zero.
879 ///
880 /// # Examples
881 /// ```
882 /// use malachite_base::num::basic::traits::Zero;
883 /// use malachite_float::Float;
884 /// use malachite_q::Rational;
885 /// use std::cmp::Ordering::*;
886 ///
887 /// let (p, o) = Float::power_of_10_rational_prec_ref(&Rational::from_unsigneds(3u8, 5), 5);
888 /// assert_eq!(p.to_string(), "4.00");
889 /// assert_eq!(o, Greater);
890 ///
891 /// let (p, o) = Float::power_of_10_rational_prec_ref(&Rational::from_unsigneds(3u8, 5), 20);
892 /// assert_eq!(p.to_string(), "3.9810715");
893 /// assert_eq!(o, Less);
894 ///
895 /// let (p, o) = Float::power_of_10_rational_prec_ref(&Rational::ZERO, 10);
896 /// assert_eq!(p.to_string(), "1.0000");
897 /// assert_eq!(o, Equal);
898 /// ```
899 #[inline]
900 pub fn power_of_10_rational_prec_ref(x: &Rational, prec: u64) -> (Self, Ordering) {
901 Self::power_of_10_rational_prec_round_ref(x, prec, Nearest)
902 }
903}
904
905impl PowerOf10<Self> for Float {
906 /// Computes $10^x$, where $x$ is a [`Float`], taking it by value.
907 ///
908 /// If the output has a precision, it is the precision of the input. If the power is equidistant
909 /// from two [`Float`]s with the specified precision, the [`Float`] with fewer 1s in its binary
910 /// expansion is chosen. See [`RoundingMode`] for a description of the `Nearest` rounding mode.
911 ///
912 /// $$
913 /// f(x) = 10^x+\varepsilon.
914 /// $$
915 /// - If $10^x$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
916 /// - If $10^x$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 10^x\rfloor-p}$,
917 /// where $p$ is the precision of the input.
918 ///
919 /// Special cases:
920 /// - $f(\text{NaN})=\text{NaN}$
921 /// - $f(\infty)=\infty$
922 /// - $f(-\infty)=0.0$
923 /// - $f(\pm0.0)=1.0$
924 ///
925 /// See the [`Float::power_of_10_of_float_round`] documentation for information on overflow and
926 /// underflow.
927 ///
928 /// If you want to use a rounding mode other than `Nearest`, consider using
929 /// [`Float::power_of_10_of_float_round`] instead. If you want to specify the output precision,
930 /// consider using [`Float::power_of_10_of_float_prec`]. If you want both of these things,
931 /// consider using [`Float::power_of_10_of_float_prec_round`].
932 ///
933 /// # Worst-case complexity
934 /// $T(n) = O(n^{3/2} \log n \log\log n)$
935 ///
936 /// $M(n) = O(n \log n)$
937 ///
938 /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
939 ///
940 /// # Examples
941 /// ```
942 /// use malachite_base::num::arithmetic::traits::PowerOf10;
943 /// use malachite_base::num::basic::traits::OneHalf;
944 /// use malachite_float::Float;
945 /// use malachite_q::Rational;
946 ///
947 /// let x = Float::from_rational_prec(Rational::ONE_HALF, 20).0;
948 ///
949 /// assert_eq!(Float::power_of_10(x).to_string(), "3.1622772");
950 /// ```
951 #[inline]
952 fn power_of_10(pow: Self) -> Self {
953 Self::power_of_10_of_float_round(pow, Nearest).0
954 }
955}
956
957impl PowerOf10<&Self> for Float {
958 /// Computes $10^x$, where $x$ is a [`Float`], taking it by reference.
959 ///
960 /// If the output has a precision, it is the precision of the input. If the power is equidistant
961 /// from two [`Float`]s with the specified precision, the [`Float`] with fewer 1s in its binary
962 /// expansion is chosen. See [`RoundingMode`] for a description of the `Nearest` rounding mode.
963 ///
964 /// $$
965 /// f(x) = 10^x+\varepsilon.
966 /// $$
967 /// - If $10^x$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
968 /// - If $10^x$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 10^x\rfloor-p}$,
969 /// where $p$ is the precision of the input.
970 ///
971 /// Special cases:
972 /// - $f(\text{NaN})=\text{NaN}$
973 /// - $f(\infty)=\infty$
974 /// - $f(-\infty)=0.0$
975 /// - $f(\pm0.0)=1.0$
976 ///
977 /// See the [`Float::power_of_10_of_float_round`] documentation for information on overflow and
978 /// underflow.
979 ///
980 /// If you want to use a rounding mode other than `Nearest`, consider using
981 /// [`Float::power_of_10_of_float_round_ref`] instead. If you want to specify the output
982 /// precision, consider using [`Float::power_of_10_of_float_prec_ref`]. If you want both of
983 /// these things, consider using [`Float::power_of_10_of_float_prec_round_ref`].
984 ///
985 /// # Worst-case complexity
986 /// $T(n) = O(n^{3/2} \log n \log\log n)$
987 ///
988 /// $M(n) = O(n \log n)$
989 ///
990 /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
991 ///
992 /// # Examples
993 /// ```
994 /// use malachite_base::num::arithmetic::traits::PowerOf10;
995 /// use malachite_base::num::basic::traits::OneHalf;
996 /// use malachite_float::Float;
997 /// use malachite_q::Rational;
998 ///
999 /// let x = Float::from_rational_prec(Rational::ONE_HALF, 20).0;
1000 ///
1001 /// assert_eq!(Float::power_of_10(&x).to_string(), "3.1622772");
1002 /// ```
1003 #[inline]
1004 fn power_of_10(pow: &Self) -> Self {
1005 Self::power_of_10_of_float_round_ref(pow, Nearest).0
1006 }
1007}
1008
1009impl PowerOf10Assign for Float {
1010 /// Computes $10^x$, where $x$ is a [`Float`], in place.
1011 ///
1012 /// If the output has a precision, it is the precision of the input. If the power is equidistant
1013 /// from two [`Float`]s with the specified precision, the [`Float`] with fewer 1s in its binary
1014 /// expansion is chosen. See [`RoundingMode`] for a description of the `Nearest` rounding mode.
1015 ///
1016 /// $$
1017 /// x \gets 10^x+\varepsilon.
1018 /// $$
1019 /// - If $10^x$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
1020 /// - If $10^x$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 10^x\rfloor-p}$,
1021 /// where $p$ is the precision of the input.
1022 ///
1023 /// See the [`Float::power_of_10_of_float_round`] documentation for information on special
1024 /// cases, overflow, and underflow.
1025 ///
1026 /// If you want to use a rounding mode other than `Nearest`, consider using
1027 /// [`Float::power_of_10_of_float_round_assign`] instead. If you want to specify the output
1028 /// precision, consider using [`Float::power_of_10_of_float_prec_assign`]. If you want both of
1029 /// these things, consider using [`Float::power_of_10_of_float_prec_round_assign`].
1030 ///
1031 /// # Worst-case complexity
1032 /// $T(n) = O(n^{3/2} \log n \log\log n)$
1033 ///
1034 /// $M(n) = O(n \log n)$
1035 ///
1036 /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
1037 ///
1038 /// # Examples
1039 /// ```
1040 /// use malachite_base::num::arithmetic::traits::PowerOf10Assign;
1041 /// use malachite_base::num::basic::traits::OneHalf;
1042 /// use malachite_float::Float;
1043 /// use malachite_q::Rational;
1044 ///
1045 /// let mut x = Float::from_rational_prec(Rational::ONE_HALF, 20).0;
1046 /// x.power_of_10_assign();
1047 /// assert_eq!(x.to_string(), "3.1622772");
1048 /// ```
1049 #[inline]
1050 fn power_of_10_assign(&mut self) {
1051 self.power_of_10_of_float_round_assign(Nearest);
1052 }
1053}
1054
1055// This is equivalent to `mpfr_exp10` from `exp10.c`, MPFR 4.3.0, which likewise delegates to
1056// `mpfr_ui_pow`.
1057
1058/// Computes $10^x$, where $x$ is a primitive float, returning the result as a primitive float of
1059/// the same type. Using this function is more accurate than using `x.exp2()` or the `exp2` function
1060/// provided by `libm`.
1061///
1062/// $$
1063/// f(x) = 10^x+\varepsilon.
1064/// $$
1065/// - If $10^x$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
1066/// - If $10^x$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 10^x\rfloor-p}$, where
1067/// $p$ is the precision of the output (typically 24 if `T` is a [`f32`] and 53 if `T` is a
1068/// [`f64`], but less if the output is subnormal).
1069///
1070/// Special cases:
1071/// - $f(\text{NaN})=\text{NaN}$
1072/// - $f(\infty)=\infty$
1073/// - $f(-\infty)=0.0$
1074/// - $f(\pm0.0)=1.0$
1075///
1076/// Overflow and underflow are possible: a large positive `x` gives $\infty$, and a large negative
1077/// `x` gives `0.0`.
1078///
1079/// # Worst-case complexity
1080/// Constant time and additional memory.
1081///
1082/// # Examples
1083/// ```
1084/// use malachite_base::num::float::NiceFloat;
1085/// use malachite_float::float::arithmetic::power_of_10::primitive_float_power_of_10;
1086///
1087/// assert_eq!(
1088/// NiceFloat(primitive_float_power_of_10(0.5f64)),
1089/// NiceFloat(3.1622776601683795)
1090/// );
1091/// assert_eq!(
1092/// NiceFloat(primitive_float_power_of_10(-3.0f64)),
1093/// NiceFloat(0.001)
1094/// );
1095/// ```
1096#[inline]
1097#[allow(clippy::type_repetition_in_bounds)]
1098pub fn primitive_float_power_of_10<T: PrimitiveFloat>(x: T) -> T
1099where
1100 Float: From<T> + PartialOrd<T>,
1101 for<'a> T: ExactFrom<&'a Float> + RoundingFrom<&'a Float>,
1102{
1103 emulate_float_to_float_fn(|x2, prec| Float::unsigned_pow_prec(10, x2, prec), x)
1104}
1105
1106/// Computes $10^x$, where $x$ is a [`Rational`], returning the result as a primitive float.
1107///
1108/// $$
1109/// f(x) = 10^x+\varepsilon.
1110/// $$
1111/// - If $10^x$ is infinite or zero, $\varepsilon$ may be ignored or assumed to be 0.
1112/// - If $10^x$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 10^x\rfloor-p}$, where
1113/// $p$ is the precision of the output (typically 24 if `T` is a [`f32`] and 53 if `T` is a
1114/// [`f64`], but less if the output is subnormal).
1115///
1116/// Special cases:
1117/// - $f(0)=1$
1118///
1119/// Overflow and underflow are possible: a large positive `x` gives $\infty$, and a large negative
1120/// `x` gives `0.0`.
1121///
1122/// # Worst-case complexity
1123/// $T(m) = O(m (\log m)^2 \log\log m)$
1124///
1125/// $M(m) = O(m \log m)$
1126///
1127/// where $T$ is time, $M$ is additional memory, and $m$ is `x.significant_bits()`.
1128///
1129/// # Examples
1130/// ```
1131/// use malachite_base::num::float::NiceFloat;
1132/// use malachite_float::float::arithmetic::power_of_10::primitive_float_power_of_10_rational;
1133/// use malachite_q::Rational;
1134///
1135/// assert_eq!(
1136/// NiceFloat(primitive_float_power_of_10_rational::<f32>(
1137/// &Rational::from_signeds(1, 3)
1138/// )),
1139/// NiceFloat(2.1544347)
1140/// );
1141/// ```
1142#[inline]
1143#[allow(clippy::type_repetition_in_bounds)]
1144pub fn primitive_float_power_of_10_rational<T: PrimitiveFloat>(x: &Rational) -> T
1145where
1146 Float: PartialOrd<T>,
1147 for<'a> T: ExactFrom<&'a Float> + RoundingFrom<&'a Float>,
1148{
1149 emulate_rational_to_float_fn(
1150 |q, prec| Float::unsigned_pow_rational_prec_ref(10, q, prec),
1151 x,
1152 )
1153}