malachite_float/float/arithmetic/pow.rs
1// Copyright © 2026 Mikhail Hogrefe
2//
3// Uses code adopted from the GNU MPFR Library.
4//
5// `mpfr_pow`, `mpfr_pow_general`, and `mpfr_pow_is_exact` from `pow.c`, and `mpfr_pow_z` and
6// `mpfr_pow_pos_z` from `pow_z.c`; MPFR 4.3.0.
7//
8// Copyright 2005-2024 Free Software Foundation, Inc. Contributed by the AriC and Caramba
9// projects, INRIA.
10//
11// This file is part of Malachite.
12//
13// Malachite is free software: you can redistribute it and/or modify it under the terms of the GNU
14// Lesser General Public License (LGPL) as published by the Free Software Foundation; either version
15// 3 of the License, or (at your option) any later version. See <https://www.gnu.org/licenses/>.
16
17use crate::InnerFloat::{Finite, Infinity, NaN, Zero};
18use crate::TWICE_WIDTH;
19use crate::emulate_float_float_to_float_fn;
20use crate::emulate_float_to_float_fn;
21use crate::float::arithmetic::exp::{
22 exp_overflow, exp_rational_near_one, exp_underflow, one_neighbor,
23};
24use crate::float::arithmetic::ln::ln_1_plus_rational_brackets;
25use crate::float::arithmetic::log_base_2::log_2_rational_brackets;
26use crate::float::arithmetic::round_near_x::float_round_near_x;
27use crate::{
28 Float, float_either_infinity, float_either_zero, float_nan, float_negative_zero,
29 floor_and_ceiling,
30};
31use core::cmp::Ordering::{self, *};
32use core::cmp::max;
33use core::mem::swap;
34use malachite_base::fail_on_untested_path;
35use malachite_base::num::arithmetic::traits::{
36 Abs, CeilingLogBase2, CheckedLogBase2, CheckedRoot, CheckedSqrt, DivisibleBy, IsPowerOf2,
37 NegAssign, Parity, Pow, PowAssign, Square, UnsignedAbs,
38};
39use malachite_base::num::basic::floats::PrimitiveFloat;
40use malachite_base::num::basic::integers::PrimitiveInt;
41use malachite_base::num::basic::traits::{
42 Infinity as InfinityTrait, NaN as NaNTrait, NegativeInfinity, NegativeZero, One,
43 Zero as ZeroTrait,
44};
45use malachite_base::num::comparison::traits::OrdAbs;
46use malachite_base::num::comparison::traits::PartialOrdAbs;
47use malachite_base::num::conversion::traits::{ExactFrom, IsInteger, RoundingFrom, SaturatingFrom};
48use malachite_base::num::logic::traits::{BitAccess, BitIterable, SignificantBits};
49use malachite_base::rounding_modes::RoundingMode::{self, *};
50use malachite_nz::integer::Integer;
51use malachite_nz::natural::Natural;
52use malachite_nz::natural::arithmetic::float_extras::float_can_round;
53use malachite_nz::platform::{Limb, SignedLimb};
54use malachite_q::Rational;
55
56// This is MPFR_POW_EXP_THRESHOLD from `pow.c`, MPFR 4.3.0.
57const POW_EXP_THRESHOLD: i64 = 256;
58
59// Whether y is an odd integer. This is equivalent to `mpfr_odd_p` from `mpfr-impl.h`, MPFR 4.3.0,
60// for finite nonzero y.
61fn float_odd_integer(y: &Float) -> bool {
62 if !y.is_finite() || y.is_zero() || !y.is_integer() {
63 return false;
64 }
65 // y = m * 2^(e - b) with m the b-bit significand: y is odd iff its unit bit is set, i.e. the
66 // significand's trailing zero count is exactly b - e. (For e > b, y is an even integer.) This
67 // avoids materializing the integer, whose bit length is the exponent and can be huge.
68 let e = i64::from(y.get_exponent().unwrap());
69 let m = y.significand_ref().unwrap();
70 let b = i64::exact_from(m.significant_bits());
71 e <= b && i64::exact_from(m.trailing_zeros().unwrap()) == b - e
72}
73
74// MPFR's `mpfr_underflow` as used by `mpfr_pow`: the callers pre-map Nearest per MPFR's convention.
75// A negative result mirrors the positive case with the rounding mode negated.
76fn pow_underflow(prec: u64, rm: RoundingMode, negative: bool) -> (Float, Ordering) {
77 if negative {
78 let (f, o) = exp_underflow(prec, -rm);
79 (-f, o.reverse())
80 } else {
81 exp_underflow(prec, rm)
82 }
83}
84
85// MPFR's `mpfr_overflow` as used by `mpfr_pow`.
86fn pow_overflow(prec: u64, rm: RoundingMode, negative: bool) -> (Float, Ordering) {
87 if negative {
88 let (f, o) = exp_overflow(prec, -rm);
89 (-f, o.reverse())
90 } else {
91 exp_overflow(prec, rm)
92 }
93}
94
95// Whether the significand of a finite nonzero Float is a power of 2 (sign-agnostic). This is
96// equivalent to `mpfr_powerof2_raw` from `mpfr-impl.h`, MPFR 4.3.0.
97fn raw_power_of_2(x: &Float) -> bool {
98 x.significand_ref().unwrap().is_power_of_2()
99}
100
101// The tiny-argument result 1 +/- ulp(1), following the tiny-x fast path of `mpfr_exp` and
102// MPFR_SMALL_INPUT_AFTER_SAVE_EXPO: the exact result is 1 + eps with sign(eps) given by `above`.
103fn float_one_plus_tiny(prec: u64, rm: RoundingMode, above: bool) -> (Float, Ordering) {
104 match (rm, above) {
105 (Up | Ceiling, true) => (one_neighbor(prec, true), Greater),
106 (Down | Floor, false) => (one_neighbor(prec, false), Less),
107 (_, true) => (Float::one_prec(prec), Less),
108 (_, false) => (Float::one_prec(prec), Greater),
109 }
110}
111
112// The outcome of `pow_near_one_fast_path`.
113enum NearOne {
114 // The result was rounded directly from 1 (or -1).
115 Rounded(Float, Ordering),
116 // The result is close to 1, but its interesting bits land within the output's window: the Ziv
117 // loop must run, but should start with this many extra bits of working precision, since the
118 // result's significand begins with about this many 0s or 1s after the leading bit. Without the
119 // jump start the loop would balloon, recomputing the power ~log(extra) times at growing
120 // precisions until the working precision covers the run.
121 JumpStart(u64),
122 // The fast path does not apply.
123 No,
124}
125
126// Fast path for x^z when x is so close to +/-1 that the result is very close to +/-1. Writing |x| =
127// 1 + d with d nonzero and fld = EXP(d), and sb_z = the bit length of |z| (z != 0, with its sign
128// given by `z_negative`), the path engages when fld + sb_z <= -3. Then |d| < 2^fld <= 2^-4 and
129// |z||d| < 2^(fld + sb_z) <= 2^-3, and with t = z ln(1 + d):
130// - |ln(1 + d)| <= |d|/(1 - |d|) <= (4/3)|d|, so |t| <= (4/3)|z||d| <= 1/6;
131// - |e^t - 1| <= |t| + t^2 <= (3/2)|t| for |t| <= 1/2;
132// so ||x|^z - 1| = |e^t - 1| <= 2|z||d| < 2^(fld + sb_z + 1), strictly (both |z| < 2^sb_z and |d| <
133// 2^fld are strict). This is exactly the error contract of `float_round_near_x` with v = 1 and err
134// = -(fld + sb_z).
135//
136// `float_round_near_x` also requires the exact result not to be representable, which holds whenever
137// it succeeds (it requires err > prec + 1): for positive z, the exact (1 + d)^z is a dyadic
138// rational whose bits span from its leading 1 down to exactly z*j, where 2^j is the lowest set bit
139// of d; since j <= fld and -fld >= err - sb_z > prec + 1 - sb_z, the span exceeds prec + 1 bits, so
140// the value is neither representable at prec nor a `Nearest` midpoint. For negative z the exact
141// value is not even dyadic (1/(1 + d)^|z| is dyadic only if (1 + d)^|z| is a power of 2, impossible
142// for 0 < |d| <= 2^-4).
143//
144// `negate` is true when the result is negative (x negative and z odd); the rounding is then
145// performed on the magnitude with the inverted rounding mode, and the ternary value is reversed.
146fn pow_near_one_fast_path(
147 x: &Float,
148 sb_z: u64,
149 z_negative: bool,
150 negate: bool,
151 prec: u64,
152 rm: RoundingMode,
153) -> NearOne {
154 // `Exact` is left entirely to the callers, so that this path never has to decide exactness.
155 if rm == Exact {
156 return NearOne::No;
157 }
158 let ex = i64::from(x.get_exponent().unwrap());
159 // |x| must be in [1/2, 2) for x to be near +/-1.
160 if ex != 0 && ex != 1 {
161 return NearOne::No;
162 }
163 // d = |x| - 1, exactly (the difference of two dyadic values whose bits span at most
164 // significant_bits(x) + 2 positions here).
165 let d = x
166 .abs()
167 .sub_prec_round(Float::ONE, x.significant_bits() + 2, Exact)
168 .0;
169 if d == 0u32 {
170 // |x| = 1 exactly; the callers' loops handle this case exactly and quickly.
171 return NearOne::No;
172 }
173 let fld = i64::from(d.get_exponent().unwrap());
174 let Some(shift) = fld.checked_add(i64::exact_from(sb_z)) else {
175 return NearOne::No;
176 };
177 if shift > -3 {
178 return NearOne::No;
179 }
180 let err = u64::exact_from(-shift);
181 // |x|^z > 1 iff |x| > 1 and z > 0, or |x| < 1 and z < 0.
182 let above = (d > 0u32) != z_negative;
183 let rm_abs = if negate { -rm } else { rm };
184 if let Some((v, o)) = float_round_near_x(&Float::ONE, err, above, prec, rm_abs) {
185 return if negate {
186 NearOne::Rounded(-v, o.reverse())
187 } else {
188 NearOne::Rounded(v, o)
189 };
190 }
191 NearOne::JumpStart(err)
192}
193
194// This is `mpfr_pow_pos_z` from `pow_z.c`, MPFR 4.3.0, with z positive. If `cr` is true the result
195// is correctly rounded; otherwise `prec` is used as the working precision. Returns the result and
196// its ordering; the result may be infinite or zero on intermediate overflow or underflow (the
197// callers handle those cases).
198fn pow_pos_natural(
199 x: &Float,
200 z: &Natural,
201 prec: u64,
202 rm: RoundingMode,
203 cr: bool,
204 extra_prec: u64,
205) -> (Float, Ordering) {
206 assert_ne!(*z, 0u32);
207 if *z == 1u32 {
208 return Float::from_float_prec_round_ref(x, prec, rm);
209 }
210 let size_z = z.significant_bits();
211 // Rounding directions chosen so that all intermediate roundings go the same way, making an
212 // intermediate overflow or underflow a true exception rather than rounding noise.
213 let x_exp_ge_1 = x.get_exponent().unwrap() >= 1;
214 let rnd1 = if x_exp_ge_1 {
215 Down
216 } else if x.is_sign_positive() {
217 Up
218 } else {
219 Floor
220 };
221 let rnd2 = if x_exp_ge_1 { Floor } else { Up };
222 // `extra_prec` is the near-1 jump start computed by the caller; see `pow_near_one_fast_path`.
223 let mut wprec = if cr {
224 prec + 3 + size_z + prec.ceiling_log_base_2() + extra_prec
225 } else {
226 prec
227 };
228 loop {
229 let mut inexmul;
230 let err = wprec - 1 - size_z;
231 let mut i = size_z;
232 let (mut res, o) = x.square_prec_round_ref(wprec, rnd2);
233 inexmul = o != Equal;
234 assert!(i >= 2);
235 if z.get_bit(i - 2) {
236 let o = res.mul_prec_round_assign_ref(x, wprec, rnd1);
237 inexmul |= o != Equal;
238 }
239 if i > 2 {
240 i -= 3;
241 while res.is_finite() && !res.is_zero() {
242 let o = res.square_prec_round_assign(wprec, rnd2);
243 inexmul |= o != Equal;
244 if z.get_bit(i) {
245 let o = res.mul_prec_round_assign_ref(x, wprec, rnd1);
246 inexmul |= o != Equal;
247 }
248 if i == 0 {
249 break;
250 }
251 i -= 1;
252 }
253 }
254 // In the shrinking regime (x's exponent < 1), rnd1/rnd2 are Up-directed, so `res` is an
255 // upper bound and can never round to zero. An inexact upper bound equal to the minimum
256 // positive Float proves the true value lies below it: a true underflow, reported as zero so
257 // the caller applies its underflow handling. (Values elsewhere in the bottom binade are
258 // representable and pass through normally; in the growing regime magnitudes only increase,
259 // so this cannot trigger.)
260 if !x_exp_ge_1
261 && inexmul
262 && res.is_finite()
263 && !res.is_zero()
264 && i64::from(res.get_exponent().unwrap()) == i64::from(Float::MIN_EXPONENT)
265 && raw_power_of_2(&res)
266 {
267 res = if res.is_sign_negative() {
268 Float::NEGATIVE_ZERO
269 } else {
270 Float::ZERO
271 };
272 }
273 let is_zero = res.is_zero();
274 let exceptional = res.is_infinite() || is_zero;
275 if !inexmul
276 || !cr
277 || exceptional
278 || float_can_round(res.significand_ref().unwrap(), err, prec, rm)
279 {
280 if exceptional {
281 // overflow or underflow: the sign and the exceptional value are already correct
282 if !is_zero {
283 // The growing regime rounds toward zero (lower bounds), and the callers decide
284 // the overflow boundary exactly before descending here.
285 fail_on_untested_path("pow_pos_natural, overflow");
286 }
287 // A zero lies toward zero from the true value and an infinity away from it, so the
288 // ternary depends on the sign: +0 and -inf are less than the true value, -0 and
289 // +inf greater.
290 let o = if is_zero == res.is_sign_positive() {
291 Less
292 } else {
293 Greater
294 };
295 return (res, o);
296 }
297 return Float::from_float_prec_round(res, prec, rm);
298 }
299 wprec += wprec >> 1;
300 }
301}
302
303// The round-to-nearest underflow fallback of `mpfr_pow_pos_z` from `pow_z.c`, MPFR 4.3.0:
304// nearest-mode underflow must choose between 0 and 2^(emin - 1) according to which side of 2^(emin
305// - 2) the true value lies, which the multiplication-based path cannot know. Rerun via pow_general
306// at 2 bits of precision: its 2^k scaling keeps the computation in range, and the final
307// shl_prec_round applies the correct nearest-mode underflow rounding.
308fn pow_integer_underflow_nearest(x: &Float, z: &Integer, prec: u64) -> (Float, Ordering) {
309 let z_bits = z.significant_bits();
310 let zz = Float::from_integer_prec_round(z.clone(), z_bits, Exact).0;
311 let (y2, o) = pow_general(x, &zz, 2, Nearest, true);
312 (Float::from_float_prec_round(y2, prec, Exact).0, o)
313}
314
315// This is `mpfr_pow_z` from `pow_z.c`, MPFR 4.3.0.
316fn pow_integer(x: &Float, z: &Integer, prec: u64, rm: RoundingMode) -> (Float, Ordering) {
317 if *z == 0u32 {
318 // The public entry handles y = 0 before calling pow_integer.
319 fail_on_untested_path("pow_integer, z == 0");
320 return (Float::one_prec(prec), Equal);
321 }
322 if x.is_nan() {
323 // The public entry filters singular x before calling pow_integer.
324 fail_on_untested_path("pow_integer, NaN x");
325 return (Float::NAN, Equal);
326 }
327 let z_pos = *z > 0u32;
328 let z_odd = z.odd();
329 if x.is_infinite() {
330 // The public entry filters singular x before calling pow_integer.
331 fail_on_untested_path("pow_integer, infinite x");
332 let negative = x.is_sign_negative() && z_odd;
333 return (
334 match (z_pos, negative) {
335 (true, false) => Float::INFINITY,
336 (true, true) => Float::NEGATIVE_INFINITY,
337 (false, false) => Float::ZERO,
338 (false, true) => Float::NEGATIVE_ZERO,
339 },
340 Equal,
341 );
342 }
343 if x.is_zero() {
344 // The public entry filters singular x before calling pow_integer.
345 fail_on_untested_path("pow_integer, zero x");
346 let negative = x.is_sign_negative() && z_odd;
347 return (
348 match (z_pos, negative) {
349 (true, false) => Float::ZERO,
350 (true, true) => Float::NEGATIVE_ZERO,
351 (false, false) => Float::INFINITY,
352 (false, true) => Float::NEGATIVE_INFINITY,
353 },
354 Equal,
355 );
356 }
357 // x = +/-2^b: x^z = (+/-1)^z * 2^(z*(b-1)+1-1)... handled exactly via the exponent.
358 if raw_power_of_2(x) {
359 let ex = i64::from(x.get_exponent().unwrap());
360 let sign_negative = x.is_sign_negative() && z_odd;
361 // new exponent = z * (ex - 1) + 1
362 let new_exp = z * Integer::from(ex - 1) + Integer::ONE;
363 let base = if sign_negative {
364 -Float::one_prec(prec)
365 } else {
366 Float::one_prec(prec)
367 };
368 return if new_exp < Float::MIN_EXPONENT {
369 pow_underflow(prec, if rm == Nearest { Down } else { rm }, sign_negative)
370 } else if new_exp > Float::MAX_EXPONENT {
371 // z(ex - 1) + 1 > MAX_EXPONENT implies z * log2|x| >= MAX_EXPONENT (the product is an
372 // exact integer at 64 bits here): a definite overflow. When called from `Float::pow`
373 // the entry's early overflow check already caught this; when called from the
374 // integer-exponent path of `Float::pow_rational` (which has no such pre-check), this is
375 // the first detection.
376 pow_overflow(prec, rm, sign_negative)
377 } else {
378 let sh = i64::exact_from(&(new_exp - Integer::ONE));
379 base.shl_prec_round(sh, prec, rm)
380 };
381 }
382 let negative = x.is_sign_negative() && z_odd;
383 // Near-1 fast path, checked before the exponent pre-bounds below: for x very close to +/-1,
384 // computing the 64-bit log2 estimate is itself expensive (the tiny logarithm must be resolved,
385 // which costs as much as the power itself), and in this regime |x^z| lies in (5/6, 6/5), so no
386 // overflow or underflow is possible and the pre-bounds are unnecessary.
387 let mut jump_extra = 0;
388 match pow_near_one_fast_path(
389 x,
390 z.unsigned_abs_ref().significant_bits(),
391 !z_pos,
392 negative,
393 prec,
394 rm,
395 ) {
396 NearOne::Rounded(v, o) => return (v, o),
397 NearOne::JumpStart(extra) => jump_extra = extra,
398 NearOne::No => {}
399 }
400 if jump_extra == 0 {
401 // Pre-bound the result exponent: result_exp ~ z * log2|x|. When it is far outside the
402 // exponent range (with a wide margin for the estimate's error), report the exception
403 // directly instead of letting the exponentiation saturate; this mirrors the role of MPFR's
404 // underflow/overflow flags, which malachite does not have, and keeps the Ziv loop from
405 // ballooning on saturated values.
406 let est = f64::rounding_from(x.abs().log_base_2_prec(64).0, Nearest).0
407 * f64::rounding_from(z, Nearest).0;
408 if est > const { Float::MAX_EXPONENT as f64 + 64.0 } {
409 // est > MAX_EXPONENT + 64: a definite overflow. When called from `Float::pow`, the
410 // entry's early overflow check already caught this; when called from the exact-power
411 // path of `Float::pow_rational` (which has no such pre-check), this is the first
412 // detection.
413 return pow_overflow(prec, rm, negative);
414 }
415 if est < const { Float::MIN_EXPONENT as f64 - 64.0 } {
416 return pow_underflow(prec, if rm == Nearest { Down } else { rm }, negative);
417 }
418 // Within the estimate's error margin of MAX_EXPONENT the overflow question is still open,
419 // and it must be decided here: every rounding used by `pow_pos_natural`'s growing regime
420 // and by the reciprocal path below decreases the magnitude, so an overflow would saturate
421 // at the largest finite value instead of reaching infinity, and the saturated all-ones
422 // significand is one that `float_can_round` never certifies -- the Ziv loop would grow
423 // forever. (Underflow needs no such decision: magnitude-decreasing rounding turns a true
424 // underflow into an exact zero, which the loops detect directly.) The check mirrors the
425 // role of MPFR's overflow flag.
426 if est >= const { Float::MAX_EXPONENT as f64 - 66.0 }
427 && pow_exponent_at_least(x, z, i64::from(Float::MAX_EXPONENT))
428 {
429 return pow_overflow(prec, rm, negative);
430 }
431 }
432 if z_pos {
433 let (result, o) = pow_pos_natural(x, z.unsigned_abs_ref(), prec, rm, true, jump_extra);
434 if result.is_zero() {
435 // pow_pos_natural only returns zero when the result underflowed.
436 return if rm == Nearest {
437 pow_integer_underflow_nearest(x, z, prec)
438 } else {
439 pow_underflow(prec, rm, x.is_sign_negative() && z_odd)
440 };
441 }
442 (result, o)
443 } else {
444 // z < 0: compute (1/x)^|z| via t = 1/x rounded toward 1/-1, then a non-correctly-rounded
445 // positive power at extended precision, with a Ziv loop.
446 let abs_z = z.unsigned_abs_ref();
447 let size_z = abs_z.significant_bits();
448 let mut wprec = prec + size_z + 3 + prec.ceiling_log_base_2() + jump_extra;
449 let rnd1 = if x.get_exponent().unwrap() < 1 {
450 Down
451 } else if x.is_sign_positive() {
452 Up
453 } else {
454 Floor
455 };
456 loop {
457 let t = Float::ONE.div_prec_round_val_ref(x, wprec, rnd1).0;
458 if t.is_infinite() {
459 // For |x| < 1 the reciprocal is rounded toward zero, so an overflowing 1/x
460 // saturates at the largest finite value rather than reaching infinity (and the
461 // exact overflow decision above has already returned in that case); for |x| >= 1 it
462 // is at most 1.
463 fail_on_untested_path("pow_integer, 1/x overflow");
464 return pow_overflow(prec, rm, t.is_sign_negative());
465 }
466 let t = pow_pos_natural(&t, abs_z, wprec, rm, false, 0).0;
467 if t.is_infinite() {
468 // The exact overflow decision above bounds |x^z| < 2^MAX_EXPONENT, and the
469 // magnitude-decreasing rounding directions keep the computed value below it.
470 fail_on_untested_path("pow_integer, (1/x)^|z| overflow");
471 return pow_overflow(prec, rm, t.is_sign_negative());
472 }
473 if t.is_zero() {
474 if rm == Nearest {
475 return pow_integer_underflow_nearest(x, z, prec);
476 }
477 return pow_underflow(prec, rm, x.is_sign_negative() && z_odd);
478 }
479 let err = wprec - size_z - 2;
480 if float_can_round(t.significand_ref().unwrap(), err, prec, rm) {
481 return Float::from_float_prec_round(t, prec, rm);
482 }
483 wprec += wprec >> 1;
484 }
485 }
486}
487
488// This is `mpfr_pow_ui` (`POW_U`) from `pow_ui.c`, MPFR 4.3.0: x^n for a `u64` n, by binary
489// exponentiation with a Ziv loop, falling back to `pow_integer` (`mpfr_pow_z`) on an internal
490// overflow or underflow.
491fn pow_u(x: Float, n: u64, prec: u64, rm: RoundingMode) -> (Float, Ordering) {
492 // x^0 = 1 for any x, even NaN
493 if n == 0 {
494 return (Float::one_prec(prec), Equal);
495 }
496 if x.is_nan() {
497 return (Float::NAN, Equal);
498 }
499 if x.is_infinite() {
500 // Inf^n = Inf; (-Inf)^n = Inf for n even, -Inf for n odd
501 return (
502 if x.is_sign_negative() && n.odd() {
503 Float::NEGATIVE_INFINITY
504 } else {
505 Float::INFINITY
506 },
507 Equal,
508 );
509 }
510 if x.is_zero() {
511 // 0^n = 0 for any n; positive unless x is negative and n is odd
512 return (
513 if x.is_sign_negative() && n.odd() {
514 Float::NEGATIVE_ZERO
515 } else {
516 Float::ZERO
517 },
518 Equal,
519 );
520 }
521 if n <= 2 {
522 return if n == 1 {
523 // x^1 = x
524 Float::from_float_prec_round(x, prec, rm)
525 } else {
526 // x^2 = sqr(x)
527 x.square_prec_round(prec, rm)
528 };
529 }
530 // n >= 3: square-and-multiply. `nlen` is the bit length of n, so 2^(nlen - 1) <= n < 2^nlen.
531 let nlen = n.significant_bits();
532 // Multiplications round away from zero (squares round up; their results are non-negative), so
533 // that an intermediate overflow or underflow is a true exception rather than rounding noise.
534 let rnd1 = if x.is_sign_positive() { Ceiling } else { Floor };
535 let mut wprec = {
536 let p = prec + 67 + prec.ceiling_log_base_2();
537 if p <= nlen {
538 // Unreachable for a `u64` n: p >= 1 + 3 + 64 = 68 always exceeds nlen, which is at most
539 // 64. (In MPFR, where GMP_NUMB_BITS may be 32 and n may be wider, this clamp matters.)
540 fail_on_untested_path("pow_u, working precision clamped up to nlen + 1");
541 nlen + 1
542 } else {
543 p
544 }
545 };
546 match pow_near_one_fast_path(&x, nlen, false, x.is_sign_negative() && n.odd(), prec, rm) {
547 NearOne::Rounded(v, o) => return (v, o),
548 NearOne::JumpStart(extra) => wprec += extra,
549 NearOne::No => {}
550 }
551 loop {
552 let err = wprec - 1 - nlen;
553 let (mut res, o) = x.square_prec_round_ref(wprec, Ceiling);
554 let mut inexact = o != Equal;
555 let mut i = nlen;
556 if n.get_bit(i - 2) {
557 inexact |= res.mul_prec_round_assign_ref(&x, wprec, rnd1) != Equal;
558 }
559 if i > 2 {
560 i -= 3;
561 loop {
562 if res.is_infinite() || res.is_zero() {
563 break;
564 }
565 inexact |= res.square_prec_round_assign(wprec, Ceiling) != Equal;
566 if n.get_bit(i) {
567 inexact |= res.mul_prec_round_assign_ref(&x, wprec, rnd1) != Equal;
568 }
569 if i == 0 {
570 break;
571 }
572 i -= 1;
573 }
574 }
575 // Internal overflow (res is infinite) or underflow (res reached the minimum exponent): the
576 // approximation error has not been accounted for, so hand off to `pow_integer`, which
577 // handles the exponent range precisely.
578 if res.is_infinite() || res.is_zero() || res.get_exponent().unwrap() <= Float::MIN_EXPONENT
579 {
580 if res.is_zero() {
581 // Unreachable: squares round up and multiplications round away from zero, so res is
582 // a magnitude over-estimate that never rounds to zero; underflow instead surfaces
583 // as the minimum binade, handled by the exponent check above.
584 fail_on_untested_path("pow_u, res rounded to zero");
585 }
586 return x.pow_integer_prec_round(Integer::from(n), prec, rm);
587 }
588 if !inexact || float_can_round(res.significand_ref().unwrap(), err, prec, rm) {
589 return Float::from_float_prec_round(res, prec, rm);
590 }
591 wprec += wprec >> 1;
592 }
593}
594
595// This is `mpfr_pow_ui` (`POW_U`) from `pow_ui.c`, MPFR 4.3.0: x^n for a `u64` n, by binary
596// exponentiation with a Ziv loop, falling back to `pow_integer` (`mpfr_pow_z`) on an internal
597// overflow or underflow.
598fn pow_u_ref(x: &Float, n: u64, prec: u64, rm: RoundingMode) -> (Float, Ordering) {
599 // x^0 = 1 for any x, even NaN
600 if n == 0 {
601 return (Float::one_prec(prec), Equal);
602 }
603 if x.is_nan() {
604 return (Float::NAN, Equal);
605 }
606 if x.is_infinite() {
607 // Inf^n = Inf; (-Inf)^n = Inf for n even, -Inf for n odd
608 return (
609 if x.is_sign_negative() && n.odd() {
610 Float::NEGATIVE_INFINITY
611 } else {
612 Float::INFINITY
613 },
614 Equal,
615 );
616 }
617 if x.is_zero() {
618 // 0^n = 0 for any n; positive unless x is negative and n is odd
619 return (
620 if x.is_sign_negative() && n.odd() {
621 Float::NEGATIVE_ZERO
622 } else {
623 Float::ZERO
624 },
625 Equal,
626 );
627 }
628 if n <= 2 {
629 return if n == 1 {
630 // x^1 = x
631 Float::from_float_prec_round_ref(x, prec, rm)
632 } else {
633 // x^2 = sqr(x)
634 x.square_prec_round_ref(prec, rm)
635 };
636 }
637 // n >= 3: square-and-multiply. `nlen` is the bit length of n, so 2^(nlen - 1) <= n < 2^nlen.
638 let nlen = n.significant_bits();
639 // Multiplications round away from zero (squares round up; their results are non-negative), so
640 // that an intermediate overflow or underflow is a true exception rather than rounding noise.
641 let rnd1 = if x.is_sign_positive() { Ceiling } else { Floor };
642 let mut wprec = {
643 let p = prec + 67 + prec.ceiling_log_base_2();
644 if p <= nlen {
645 // Unreachable for a `u64` n: p >= 1 + 3 + 64 = 68 always exceeds nlen, which is at most
646 // 64. (In MPFR, where GMP_NUMB_BITS may be 32 and n may be wider, this clamp matters.)
647 fail_on_untested_path("pow_u, working precision clamped up to nlen + 1");
648 nlen + 1
649 } else {
650 p
651 }
652 };
653 match pow_near_one_fast_path(x, nlen, false, x.is_sign_negative() && n.odd(), prec, rm) {
654 NearOne::Rounded(v, o) => return (v, o),
655 NearOne::JumpStart(extra) => wprec += extra,
656 NearOne::No => {}
657 }
658 loop {
659 let err = wprec - 1 - nlen;
660 let (mut res, o) = x.square_prec_round_ref(wprec, Ceiling);
661 let mut inexact = o != Equal;
662 let mut i = nlen;
663 if n.get_bit(i - 2) {
664 inexact |= res.mul_prec_round_assign_ref(x, wprec, rnd1) != Equal;
665 }
666 if i > 2 {
667 i -= 3;
668 loop {
669 if res.is_infinite() || res.is_zero() {
670 break;
671 }
672 inexact |= res.square_prec_round_assign(wprec, Ceiling) != Equal;
673 if n.get_bit(i) {
674 inexact |= res.mul_prec_round_assign_ref(x, wprec, rnd1) != Equal;
675 }
676 if i == 0 {
677 break;
678 }
679 i -= 1;
680 }
681 }
682 // Internal overflow (res is infinite) or underflow (res reached the minimum exponent): the
683 // approximation error has not been accounted for, so hand off to `pow_integer`, which
684 // handles the exponent range precisely.
685 if res.is_infinite() || res.is_zero() || res.get_exponent().unwrap() <= Float::MIN_EXPONENT
686 {
687 if res.is_zero() {
688 // Unreachable: squares round up and multiplications round away from zero, so res is
689 // a magnitude over-estimate that never rounds to zero; underflow instead surfaces
690 // as the minimum binade, handled by the exponent check above.
691 fail_on_untested_path("pow_u, res rounded to zero");
692 }
693 return x.pow_integer_prec_round_ref_val(Integer::from(n), prec, rm);
694 }
695 if !inexact || float_can_round(res.significand_ref().unwrap(), err, prec, rm) {
696 return Float::from_float_prec_round(res, prec, rm);
697 }
698 wprec += wprec >> 1;
699 }
700}
701
702// This is `mpfr_pow_si` (`POW_S`) from `pow_si.c`, MPFR 4.3.0: x^n for an `i64` n. For n >= 0 it is
703// `pow_u` (`mpfr_pow_ui`); for n < 0, x^n = (1/x)^|n| is computed by `pow_integer` (`mpfr_pow_z`),
704// whose negative-exponent path is exactly what `mpfr_pow_si` inlines.
705fn pow_s(x: Float, n: i64, prec: u64, rm: RoundingMode) -> (Float, Ordering) {
706 if n >= 0 {
707 pow_u(x, n.unsigned_abs(), prec, rm)
708 } else {
709 x.pow_integer_prec_round(Integer::from(n), prec, rm)
710 }
711}
712
713fn pow_s_ref(x: &Float, n: i64, prec: u64, rm: RoundingMode) -> (Float, Ordering) {
714 if n >= 0 {
715 pow_u_ref(x, n.unsigned_abs(), prec, rm)
716 } else {
717 x.pow_integer_prec_round_ref_val(Integer::from(n), prec, rm)
718 }
719}
720
721// This is `mpfr_ui_pow_ui` from `ui_pow_ui.c`, MPFR 4.3.0: k^n for `u64` k and n, as a Float, by
722// binary exponentiation (all roundings up, so the result is a magnitude over-estimate), falling
723// back to `pow_integer` (`mpfr_pow_z`) on overflow. Since k, n >= 0 the result never underflows.
724//
725// The error budget deliberately deviates from MPFR, whose accounting (one rounding for the initial
726// value plus one per squaring, size_n in all) undercounts: the initial rounding of k is amplified
727// to the n-th power through the squarings, and the multiplications contribute up to size_n - 1 more
728// factors, for at most 2n - 1 < 2^(size_n + 1) Higham factors in all -- a relative error below
729// 2^(size_n + 2 - wprec), so size_n + 2 bits are reserved. With MPFR's budget the `float_can_round`
730// gate certifies wrongly rounded results at small precisions (upstream mpfr_ui_pow_ui reproduces
731// this: 263^15 at precision 1 under `Nearest` returns 2^121 though the true value lies below the
732// tie 1.5 * 2^120, and 205^63 at precision 4 under `Down` returns a value above the true one).
733fn unsigned_pow_unsigned(k: u64, n: u64, prec: u64, rm: RoundingMode) -> (Float, Ordering) {
734 if n == 0 {
735 // k^0 = 1 for any k
736 return (Float::one_prec(prec), Equal);
737 } else if n == 1 || k <= 1 {
738 // k^1 = k; 1^n = 1 and 0^n = 0 for n >= 1; either way the value is k
739 return Float::from_unsigned_prec_round(k, prec, rm);
740 }
741 // k >= 2, n >= 2. `size_n` is the bit length of n, so 2^(size_n - 1) <= n < 2^size_n.
742 let size_n = n.significant_bits();
743 // k as an exact Float, for the multiplications.
744 let kf = Float::from(k);
745 let mut wprec = prec + 5 + size_n;
746 loop {
747 // res starts as k (rounded up), contributing the most significant bit of n.
748 let (mut res, o) = Float::from_unsigned_prec_round(k, wprec, Ceiling);
749 let mut inexact = o != Equal;
750 // err counts the roundings: 1 for the initial value, plus one per squaring.
751 for bit in n.bits().rev().skip(1) {
752 inexact |= res.square_prec_round_assign(wprec, Ceiling) != Equal;
753 if bit {
754 inexact |= res.mul_prec_round_assign_ref(&kf, wprec, Ceiling) != Equal;
755 }
756 }
757 if res.is_infinite() {
758 // Overflow: the approximation error has not been accounted for, so hand off to
759 // `pow_integer`, which handles the exponent range precisely.
760 return kf.pow_integer_prec_round(Integer::from(n), prec, rm);
761 }
762 if !inexact || float_can_round(res.significand_ref().unwrap(), wprec - size_n - 2, prec, rm)
763 {
764 return Float::from_float_prec_round(res, prec, rm);
765 }
766 wprec += wprec >> 1;
767 }
768}
769
770// This is `mpfr_pow_is_exact` from `pow.c`, MPFR 4.3.0: assuming x > 0, x not a power of 2, y
771// finite non-integer, decides whether x^y is exact, and if so computes it.
772fn pow_is_exact(x: &Float, y: &Float, prec: u64, rm: RoundingMode) -> Option<(Float, Ordering)> {
773 if y.is_sign_negative() {
774 return None;
775 }
776 // y = c * 2^d with c an odd integer, d < 0
777 let (c, mut d) = float_to_odd_mantissa_and_exponent(y);
778 // y is not an integer (the callers filter integers), so it has fractional bits.
779 assert!(d < 0);
780 // x = a * 2^b with a odd
781 let (mut a, mut b) = float_to_odd_mantissa_and_exponent_natural(x);
782 while d != 0 {
783 if b.odd() {
784 a <<= 1u32;
785 b -= 1;
786 }
787 a = a.checked_sqrt()?;
788 b >>= 1;
789 d += 1;
790 }
791 // x^y = (a * 2^b)^c with c an odd integer
792 let tmp_prec = a.significant_bits();
793 let tmp = Float::from_natural_prec_round(a, tmp_prec, Exact)
794 .0
795 .shl_prec_round(b, tmp_prec, Exact)
796 .0;
797 Some(pow_integer(&tmp, &c, prec, rm))
798}
799
800// Resolves |x|^y when the true product y * ln|x| lies at or below the bottom of the Float exponent
801// range. In that regime the Ziv loop's Ceiling-rounded product either underflows to -0.0 (making
802// exp return exactly 1, whose all-zero error window `float_can_round` can never certify -- an
803// infinite loop) or saturates at the minimum positive value (an overestimate whose error the loop's
804// budget does not account for, letting it certify a wrongly rounded result near the `Nearest` tie).
805// MPFR computes the product in an extended exponent range; malachite has none, so the tiny-product
806// case is resolved in exact Rational arithmetic, which has no exponent range at all.
807//
808// The true result is 1 + delta with 0 < |delta| <= 2^(MIN_EXPONENT + 1). Exact dyadic results --
809// including `Nearest` ties, which are dyadic -- are delegated to `pow_is_exact`; the remaining
810// values are irrationals strictly between any rounding boundaries, so bracketing exp(t) between the
811// exact Rationals 1 + t_lo and 1 + t_hi + t_hi^2 (valid for |t| <= 1/2) and widening the ln|x|
812// brackets Ziv-style always terminates. For |x| within a sliver of 1, ln|x| is bracketed by the
813// exact atanh-series helper -- a direct `ln` would need working precision on the order of the
814// sliver's depth (up to ~2^30 bits) to survive the cancellation.
815fn pow_general_tiny_product(
816 abs_x: &Float,
817 y: &Float,
818 prec: u64,
819 rm: RoundingMode,
820) -> (Float, Ordering) {
821 // y is never an integer here: the entry's sliver-of-one guard keeps |ln|x|| >= 2^(MIN_EXPONENT
822 // + 8), so an integer y (with |y| >= 1) cannot make the product underflow.
823 if let Some(result) = pow_is_exact(abs_x, y, prec, rm) {
824 return result;
825 }
826 let yr = Rational::exact_from(y);
827 let y_pos = *y > 0u32;
828 // Classify |x| as near 1 or not with a cheap low-precision subtraction; near the threshold
829 // either branch is correct, so the classification need not be exact.
830 let near_one = abs_x
831 .sub_prec_ref_val(Float::ONE, 64)
832 .0
833 .get_exponent()
834 .unwrap()
835 < -8;
836 let e = if near_one {
837 Some(Rational::exact_from(abs_x) - Rational::ONE)
838 } else {
839 None
840 };
841 let mut wp = 128;
842 loop {
843 // ln_lo <= ln|x| <= ln_hi, as exact Rationals
844 let (ln_lo, ln_hi) = if let Some(e) = &e {
845 ln_1_plus_rational_brackets(e, wp)
846 } else {
847 (
848 Rational::exact_from(abs_x.ln_prec_round_ref(wp, Floor).0),
849 Rational::exact_from(abs_x.ln_prec_round_ref(wp, Ceiling).0),
850 )
851 };
852 // t_lo <= y ln|x| <= t_hi
853 let (t_lo, t_hi) = if y_pos {
854 (&yr * ln_lo, &yr * ln_hi)
855 } else {
856 (&yr * ln_hi, &yr * ln_lo)
857 };
858 // 1 + t <= exp(t) <= 1 + t + t^2 for |t| <= 1/2
859 let lower = Rational::ONE + &t_lo;
860 let upper = Rational::ONE + &t_hi + (&t_hi).square();
861 let (p_lo, mut o_lo) = Float::from_rational_prec_round(lower, prec, rm);
862 let (p_hi, mut o_hi) = Float::from_rational_prec_round(upper, prec, rm);
863 // A bracket end landing exactly on a representable value rounds with `Equal`; the true
864 // value lies strictly between the ends, so the other end's ordering is the true one.
865 if o_lo == Equal {
866 o_lo = o_hi;
867 }
868 if o_hi == Equal {
869 o_hi = o_lo;
870 }
871 // `lower` and `upper` are positive Rationals near 1 (the result is `1 + tiny`), so
872 // `from_rational_prec_round` yields a positive value at precision `prec`, never `NaN` or
873 // `-0.0`, and a plain value comparison suffices.
874 if o_lo == o_hi && p_lo == p_hi {
875 return (p_lo, o_lo);
876 }
877 wp <<= 1;
878 }
879}
880
881// This is `mpfr_pow_general` from `pow.c`, MPFR 4.3.0: the Ziv loop computing exp(y * ln|x|), with
882// a scaling factor 2^k to dodge intermediate overflow and underflow.
883fn pow_general(
884 x: &Float,
885 y: &Float,
886 prec: u64,
887 mut rm: RoundingMode,
888 y_is_integer: bool,
889) -> (Float, Ordering) {
890 let abs_x = x.abs();
891 let mut neg_result = false;
892 if x.is_sign_negative() {
893 assert!(y_is_integer);
894 if float_odd_integer(y) {
895 neg_result = true;
896 rm.neg_assign(); // invert directed modes; Nearest stays
897 }
898 }
899 let mut wprec = prec + 9 + prec.ceiling_log_base_2();
900 // Pre-detect a product y * ln|x| below the exponent range, without first computing ln|x| at
901 // working precision: for |x| within a deep sliver of 1 that ln costs on the order of |log2(|x|
902 // - 1)| bits of internal precision (up to ~2^30) only for the product to underflow anyway. The
903 // exponent estimate errs on the side of not firing; the in-loop detection below is the
904 // backstop.
905 let ey = i64::from(y.get_exponent().unwrap());
906 let d = abs_x.sub_prec_ref_val(Float::ONE, 64).0;
907 let d_exp = i64::from(d.get_exponent().unwrap());
908 // the exponent of ln|x|, within ~1: for |x| near 1, ln|x| ~ |x| - 1; otherwise |ln|x|| > 2^-9
909 // and a 64-bit ln suffices
910 let ln_exp = if d_exp < -8 {
911 d_exp
912 } else {
913 i64::from(abs_x.ln_prec_round_ref(64, Floor).0.get_exponent().unwrap())
914 };
915 // Product exponents add within 1 (exp(a * b) is exp(a) + exp(b) or one less), and ln_exp itself
916 // is accurate within ~1, so trigger with a couple of binades of margin. Over-triggering is
917 // harmless: the resolver is correct for any small product, and for the borderline
918 // (bottom-binade but representable) products the x involved is deep within a near-sliver of 1,
919 // where the loop's `ln` would need catastrophic working precision anyway.
920 if ey.saturating_add(ln_exp) <= i64::from(Float::MIN_EXPONENT) + 2 {
921 let (mut result, mut o) = pow_general_tiny_product(&abs_x, y, prec, rm);
922 if neg_result {
923 result.neg_assign();
924 o = o.reverse();
925 }
926 return (result, o);
927 }
928 let mut k: Option<Integer> = None;
929 let mut check_exact_case = false;
930 let mut exact_case = false;
931 let mut result;
932 let mut o;
933 loop {
934 // t = ln|x|, rounded so that t is an upper bound on y * ln|x|
935 let mut t = abs_x
936 .ln_prec_round_ref(wprec, if y.is_sign_negative() { Floor } else { Ceiling })
937 .0;
938 t.mul_prec_round_assign_ref(y, wprec, Ceiling);
939 // A product below the exponent range comes back as -0.0 (negative underflow) or saturated
940 // at the minimum positive value (positive underflow); both derail the loop, so resolve them
941 // exactly. (A genuine product equal to the minimum positive value takes this path too,
942 // harmlessly.)
943 if k.is_none()
944 && (t.is_zero()
945 || (t.get_exponent() == Some(Float::MIN_EXPONENT) && raw_power_of_2(&t)))
946 {
947 (result, o) = pow_general_tiny_product(&abs_x, y, prec, rm);
948 break;
949 }
950 let exp_t = t.get_exponent().map_or(0, i64::from);
951 if let Some(kv) = &k {
952 t.sub_prec_round_assign(
953 Float::ln_2_prec_round(wprec, Floor)
954 .0
955 .mul_prec_round(
956 Float::from_signed_prec(i64::exact_from(kv), wprec).0,
957 wprec,
958 Floor,
959 )
960 .0,
961 wprec,
962 Ceiling,
963 );
964 }
965 let mut err = if !t.is_zero() && exp_t >= -1 {
966 exp_t + 3
967 } else {
968 1
969 };
970 if let Some(kv) = &k {
971 let exp_k = i64::exact_from(kv.significant_bits());
972 if exp_k > err {
973 err = exp_k;
974 }
975 err += 1;
976 }
977 t.exp_prec_assign(wprec);
978 // MPFR checks the underflow flag here, which also fires when the result rounds UP into the
979 // bottom binade (e.g. to the minimum positive value); malachite has no flags, so treat any
980 // bottom-binade result as "possibly spurious underflow" and take the 2^k rescue path, which
981 // recomputes in a comfortable range.
982 let t_bottom_binade = t.is_finite()
983 && !t.is_zero()
984 && k.is_none()
985 && t.get_exponent()
986 .is_some_and(|e| i64::from(e) == i64::from(Float::MIN_EXPONENT));
987 if t.is_zero() || t.is_infinite() || t_bottom_binade {
988 // After a 2^k rescue the computation stays comfortably in range, so a singular result
989 // cannot recur (MPFR_ASSERTN(!k_non_zero) in mpfr_pow_general).
990 assert!(k.is_none());
991 if t.is_zero() {
992 // real underflow of |x|^y
993 (result, o) = pow_underflow(prec, if rm == Nearest { Down } else { rm }, false);
994 break;
995 }
996 if t.is_infinite() {
997 // possible overflow: recompute a lower bound
998 let t2 = abs_x
999 .ln_prec_round_ref(wprec, if y.is_sign_negative() { Ceiling } else { Floor })
1000 .0
1001 .mul_prec_round_val_ref(y, wprec, Floor)
1002 .0
1003 .exp_prec_round(wprec, Floor)
1004 .0;
1005 if t2.is_infinite() {
1006 // The entry check bounds |x^y| < 2^MAX_EXPONENT, so the lower-bound
1007 // recomputation cannot be infinite.
1008 fail_on_untested_path("pow_general, confirmed overflow");
1009 (result, o) = pow_overflow(prec, rm, false);
1010 break;
1011 }
1012 }
1013 // scale by 2^-k with k ~ y*log2|x|
1014 k = Some(
1015 Integer::rounding_from(
1016 abs_x.log_base_2_prec_ref(64).0.mul_prec_val_ref(y, 64).0,
1017 Nearest,
1018 )
1019 .0,
1020 );
1021 continue;
1022 }
1023 if float_can_round(
1024 t.significand_ref().unwrap(),
1025 wprec.checked_sub(u64::saturating_from(err)).unwrap_or(1),
1026 prec,
1027 rm,
1028 ) {
1029 (result, o) = Float::from_float_prec_round(t, prec, rm);
1030 break;
1031 }
1032 if !check_exact_case && !y_is_integer {
1033 if let Some((z, oz)) = pow_is_exact(&abs_x, y, prec, rm) {
1034 result = z;
1035 o = oz;
1036 exact_case = true;
1037 break;
1038 }
1039 check_exact_case = true;
1040 }
1041 wprec += wprec >> 1;
1042 }
1043 if !exact_case && let Some(kv) = &k {
1044 let lk = i64::exact_from(kv);
1045 // Double-rounding guard from `mpfr_pow_general`: in rounding to nearest, if the scaled
1046 // result would be exactly 2^(emin - 2) but the unscaled rounding already went below the
1047 // exact value, the true result is above the underflow tie point and must round up to
1048 // 2^(emin - 1), not down to zero. (The result is positive here; the sign is applied below.)
1049 let mut shift_rm = rm;
1050 if rm == Nearest
1051 && o == Less
1052 && lk < 0
1053 && result
1054 .get_exponent()
1055 .is_some_and(|e| i64::from(e) == i64::from(Float::MIN_EXPONENT) - 1 - lk)
1056 && raw_power_of_2(&result)
1057 {
1058 shift_rm = Ceiling;
1059 }
1060 let (shifted, oo) = result.shl_prec_round(lk, prec, shift_rm);
1061 result = shifted;
1062 if oo != Equal {
1063 o = oo;
1064 }
1065 }
1066 if neg_result {
1067 result.neg_assign();
1068 o = o.reverse();
1069 }
1070 (result, o)
1071}
1072
1073// Decomposes a finite nonzero Float into (odd Integer mantissa, exponent): x = c * 2^d.
1074fn float_to_odd_mantissa_and_exponent(x: &Float) -> (Integer, i64) {
1075 let (n, d) = float_to_odd_mantissa_and_exponent_natural(&x.abs());
1076 (Integer::from_sign_and_abs(x.is_sign_positive(), n), d)
1077}
1078
1079fn float_to_odd_mantissa_and_exponent_natural(x: &Float) -> (Natural, i64) {
1080 let m = x.significand_ref().unwrap().clone();
1081 let e = i64::from(x.get_exponent().unwrap()) - i64::exact_from(m.significant_bits());
1082 let tz = m.trailing_zeros().unwrap();
1083 (m >> tz, e + i64::exact_from(tz))
1084}
1085
1086// Decides exactly whether z * log2|x| >= bound -- equivalently, whether |x|^z >= 2^bound -- for a
1087// finite nonzero x that is not a power of 2 and a nonzero z. Writing |x| = a * 2^b with a odd (and
1088// a >= 3, since x is not a power of 2), log2|x| = b + log2(a), and log2(a) is bracketed between
1089// exact Rationals at widening precision. log2(a) is irrational, so z * (b + log2(a)) never equals
1090// the integer bound and the comparison always resolves.
1091fn pow_exponent_at_least(x: &Float, z: &Integer, bound: i64) -> bool {
1092 let (a, b) = float_to_odd_mantissa_and_exponent_natural(&x.abs());
1093 debug_assert!(a > 1u32);
1094 let ar = Rational::from(a);
1095 let zr = Rational::from(z);
1096 let br = Rational::from(b);
1097 let bound_r = Rational::from(bound);
1098 let z_pos = *z > 0u32;
1099 let mut wprec = 128;
1100 loop {
1101 let (l_lo, l_hi) = log_2_rational_brackets(&ar, wprec);
1102 let (t_lo, t_hi) = if z_pos {
1103 (&zr * (&br + l_lo), &zr * (&br + l_hi))
1104 } else {
1105 (&zr * (&br + l_hi), &zr * (&br + l_lo))
1106 };
1107 if t_lo >= bound_r {
1108 return true;
1109 }
1110 if t_hi < bound_r {
1111 return false;
1112 }
1113 wprec <<= 1;
1114 }
1115}
1116
1117// If `|x|` is a sliver of 1 -- within a couple of binades of the smallest positive `Float`, where
1118// `ln|x|` falls below the smallest positive `Float` -- returns `x`'s exact `Rational` value, and
1119// otherwise `None`. Only a `Float` in `(1/2, 2)` with a precision near `2^30` can be a sliver, so
1120// the exact `Rational` (which occupies ~128 MB) is built only past the cheap exponent and precision
1121// tests.
1122fn float_sliver_of_one(x: &Float) -> Option<Rational> {
1123 let ex = i64::from(x.get_exponent().unwrap());
1124 if (ex == 0 || ex == 1)
1125 && x.get_prec().unwrap() >= u64::exact_from(-i64::from(Float::MIN_EXPONENT) - 8)
1126 {
1127 let xr = Rational::exact_from(x);
1128 let d = (&xr).abs() - Rational::ONE;
1129 if d != 0u32 && d.floor_log_base_2_abs() < i64::from(Float::MIN_EXPONENT) + 8 {
1130 return Some(xr);
1131 }
1132 }
1133 None
1134}
1135
1136impl Float {
1137 // This is `mpfr_pow` from `pow.c`, MPFR 4.3.0.
1138
1139 /// Raises a [`Float`] to a [`Float`] power, rounding the result to the specified precision and
1140 /// with the specified rounding mode. Both [`Float`]s are taken by reference. An [`Ordering`] is
1141 /// also returned, indicating whether the rounded power is less than, equal to, or greater than
1142 /// the exact power. Although `NaN`s are not comparable to any [`Float`], whenever this function
1143 /// returns a `NaN` it also returns `Equal`.
1144 ///
1145 /// See [`RoundingMode`] for a description of the possible rounding modes.
1146 ///
1147 /// $$
1148 /// f(x,y,p,m) = x^y+\varepsilon.
1149 /// $$
1150 /// - If $x^y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
1151 /// - If $x^y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
1152 /// 2^{\lfloor\log_2 |x^y|\rfloor-p+1}$.
1153 /// - If $x^y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
1154 /// 2^{\lfloor\log_2 |x^y|\rfloor-p}$.
1155 ///
1156 /// If the output has a precision, it is `prec`.
1157 ///
1158 /// Special cases:
1159 /// - $f(x,\pm0.0,p,m)=1.0$ for any $x$, even `NaN`
1160 /// - $f(1.0,y,p,m)=1.0$ for any $y$, even `NaN`
1161 /// - $f(\text{NaN},y,p,m)=f(x,\text{NaN},p,m)=\text{NaN}$ otherwise
1162 /// - $f(x,\infty,p,m)=\infty$ if $|x|>1$, and $0.0$ if $|x|<1$
1163 /// - $f(x,-\infty,p,m)=0.0$ if $|x|>1$, and $\infty$ if $|x|<1$
1164 /// - $f(-1.0,\pm\infty,p,m)=1.0$
1165 /// - $f(-1.0,y,p,m)=1.0$ if $y$ is an even integer, and $-1.0$ if $y$ is an odd integer
1166 /// - $f(\infty,y,p,m)=\infty$ if $y>0$, and $0.0$ if $y<0$
1167 /// - $f(-\infty,y,p,m)=-\infty$ if $y$ is a positive odd integer, $\infty$ if $y$ is positive
1168 /// and not an odd integer, $-0.0$ if $y$ is a negative odd integer, and $0.0$ if $y$ is
1169 /// negative and not an odd integer
1170 /// - $f(0.0,y,p,m)=0.0$ if $y>0$, and $\infty$ if $y<0$
1171 /// - $f(-0.0,y,p,m)=-0.0$ if $y$ is a positive odd integer, $0.0$ if $y$ is positive and not an
1172 /// odd integer, $-\infty$ if $y$ is a negative odd integer, and $\infty$ if $y$ is negative
1173 /// and not an odd integer
1174 /// - $f(x,y,p,m)=\text{NaN}$ if $x$ is finite and negative and $y$ is finite and not an integer
1175 ///
1176 /// Overflow and underflow:
1177 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
1178 /// returned instead.
1179 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor` or `Down`, $(1-(1/2)^p)2^{2^{30}-1}$
1180 /// is returned instead.
1181 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
1182 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
1183 /// instead.
1184 /// - If $0<f(x,y,p,m)\leq2^{-2^{30}-1}$ and $m$ is `Nearest`, $0.0$ is returned instead.
1185 /// - If $2^{-2^{30}-1}<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
1186 /// instead.
1187 /// - Negative results (from negative $x$ and odd integer $y$) mirror the bullets above, with
1188 /// the rounding directions reflected.
1189 ///
1190 /// If you know you'll be using `Nearest`, consider using [`Float::pow_prec_ref_ref`] instead.
1191 /// If you know that your target precision is the maximum of the precisions of the two inputs,
1192 /// consider using [`Float::pow_round_ref_ref`] instead. If both of these things are true,
1193 /// consider using [`Pow::pow`] instead.
1194 ///
1195 /// # Worst-case complexity
1196 /// $T(n) = O(n^{3/2} \log n \log\log n)$
1197 ///
1198 /// $M(n) = O(n (\log n)^2)$
1199 ///
1200 /// where $T$ is time, $M$ is additional memory, and $n$ is `prec`.
1201 ///
1202 /// # Panics
1203 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the given
1204 /// precision.
1205 ///
1206 /// # Examples
1207 /// ```
1208 /// use malachite_base::rounding_modes::RoundingMode::*;
1209 /// use malachite_float::Float;
1210 /// use std::cmp::Ordering::*;
1211 ///
1212 /// let (p, o) = (&Float::from(3)).pow_prec_round_ref_ref(&Float::from(2.5), 5, Floor);
1213 /// assert_eq!(p.to_string(), "15.5");
1214 /// assert_eq!(o, Less);
1215 ///
1216 /// let (p, o) = (&Float::from(3)).pow_prec_round_ref_ref(&Float::from(2.5), 5, Ceiling);
1217 /// assert_eq!(p.to_string(), "16.0");
1218 /// assert_eq!(o, Greater);
1219 ///
1220 /// let (p, o) = (&Float::from(3)).pow_prec_round_ref_ref(&Float::from(2.5), 5, Nearest);
1221 /// assert_eq!(p.to_string(), "15.5");
1222 /// assert_eq!(o, Less);
1223 ///
1224 /// let (p, o) = (&Float::from(3)).pow_prec_round_ref_ref(&Float::from(2.5), 20, Floor);
1225 /// assert_eq!(p.to_string(), "15.588455");
1226 /// assert_eq!(o, Less);
1227 ///
1228 /// let (p, o) = (&Float::from(3)).pow_prec_round_ref_ref(&Float::from(2.5), 20, Ceiling);
1229 /// assert_eq!(p.to_string(), "15.588470");
1230 /// assert_eq!(o, Greater);
1231 ///
1232 /// let (p, o) = (&Float::from(3)).pow_prec_round_ref_ref(&Float::from(2.5), 20, Nearest);
1233 /// assert_eq!(p.to_string(), "15.588455");
1234 /// assert_eq!(o, Less);
1235 /// ```
1236 pub fn pow_prec_round_ref_ref(
1237 &self,
1238 y: &Self,
1239 prec: u64,
1240 rm: RoundingMode,
1241 ) -> (Self, Ordering) {
1242 assert_ne!(prec, 0);
1243 // Exact rounding: compute with Nearest and demand exactness (the exact cases all flow
1244 // through the integer-power and exact-power paths, which report Equal).
1245 if rm == Exact {
1246 let (result, o) = self.pow_prec_ref_ref(y, prec);
1247 assert_eq!(o, Equal, "Inexact pow");
1248 return (result, Equal);
1249 }
1250 let x = self;
1251 // Singular cases; see Section F.9.4.4 of the C standard.
1252 match (x, y) {
1253 // pow(x, 0) = 1 for any x, even NaN
1254 (_, float_either_zero!()) => {
1255 return (Self::one_prec(prec), Equal);
1256 }
1257 (float_nan!(), _) => return (Self::NAN, Equal),
1258 // pow(+1, NaN) = 1
1259 (_, float_nan!()) => {
1260 return if *x == 1u32 {
1261 (Self::one_prec(prec), Equal)
1262 } else {
1263 (Self::NAN, Equal)
1264 };
1265 }
1266 (float_either_infinity!(), Self(Infinity { sign })) => {
1267 return if *sign {
1268 (Self::INFINITY, Equal)
1269 } else {
1270 (Self::ZERO, Equal)
1271 };
1272 }
1273 (_, Self(Infinity { sign })) => {
1274 let mut cmp = x.partial_cmp_abs(&Self::ONE).unwrap();
1275 if !*sign {
1276 cmp = cmp.reverse();
1277 }
1278 return match cmp {
1279 Greater => (Self::INFINITY, Equal),
1280 Less => (Self::ZERO, Equal),
1281 Equal => (Self::one_prec(prec), Equal),
1282 };
1283 }
1284 (Self(Infinity { sign }), _) => {
1285 let negative = !*sign && float_odd_integer(y);
1286 return (
1287 match (y.is_sign_positive(), negative) {
1288 (true, false) => Self::INFINITY,
1289 (true, true) => Self::NEGATIVE_INFINITY,
1290 (false, false) => Self::ZERO,
1291 (false, true) => Self::NEGATIVE_ZERO,
1292 },
1293 Equal,
1294 );
1295 }
1296 (Self(Zero { sign }), _) => {
1297 let negative = !*sign && float_odd_integer(y);
1298 return (
1299 match (y.is_sign_negative(), negative) {
1300 (true, false) => Self::INFINITY,
1301 (true, true) => Self::NEGATIVE_INFINITY,
1302 (false, false) => Self::ZERO,
1303 (false, true) => Self::NEGATIVE_ZERO,
1304 },
1305 Equal,
1306 );
1307 }
1308 _ => {}
1309 }
1310 // x^y for x < 0 and y not an integer is not defined
1311 let y_is_integer = y.is_integer();
1312 if x.is_sign_negative() && !y_is_integer {
1313 return (Self::NAN, Equal);
1314 }
1315 let cmp_x_1 = x.partial_cmp_abs(&Self::ONE).unwrap();
1316 if cmp_x_1 == Equal {
1317 let negative = x.is_sign_negative() && float_odd_integer(y);
1318 return Self::from_float_prec_round(
1319 if negative { -Self::ONE } else { Self::ONE },
1320 prec,
1321 rm,
1322 );
1323 }
1324 // When |x| is a sliver of 1 -- within a couple of binades of the smallest positive Float --
1325 // ln|x| falls below the smallest positive Float, so every Float-based route below (the
1326 // early over/underflow bounds, `pow_general`) would underflow it and lose the precision
1327 // needed for y * ln|x| (which can still be an ordinary, even overflowing, value). Delegate
1328 // to the exact-Rational power, which brackets log2 with the atanh series over `Rational`s
1329 // and never materializes a sub-`MIN_EXPONENT` Float logarithm. Only huge-precision Floats
1330 // in (1/2, 2) can be slivers, so the exact Rational is built only past those cheap tests.
1331 if let Some(xr) = float_sliver_of_one(x) {
1332 return Self::rational_pow_prec_round_val_ref(xr, y, prec, rm);
1333 }
1334 let ex = i64::from(x.get_exponent().unwrap());
1335 let ey = i64::from(y.get_exponent().unwrap());
1336 // Fast check for no possible overflow or underflow: |y| <= 2^15 and moderate ex means |y *
1337 // log2|x|| stays far from the exponent limits.
1338 let no_over_under = ey <= 15 && -32767 < ex && ex <= 32767;
1339 if !no_over_under {
1340 // early overflow detection: lower bound on y * log2|x|
1341 if (cmp_x_1 == Greater) == y.is_sign_positive() {
1342 let t = x
1343 .abs()
1344 .log_base_2_prec_round_ref(64, Down)
1345 .0
1346 .mul_prec_round_val_ref(y, 64, Down)
1347 .0;
1348 if t >= const { Self::const_from_signed(Self::MAX_EXPONENT as SignedLimb) } {
1349 return pow_overflow(prec, rm, x.is_sign_negative() && float_odd_integer(y));
1350 }
1351 }
1352 // early underflow detection: ebound such that |x^y| < 2^ebound
1353 if if y.is_sign_negative() { ex > 1 } else { ex < 0 } {
1354 let mut tmp = Self::from_signed_prec(ex, 64).0;
1355 if y.is_sign_negative() {
1356 tmp.sub_prec_assign(Self::ONE, 64);
1357 }
1358 tmp.mul_prec_round_assign_ref(y, 64, Ceiling);
1359 let mut ebound = i64::rounding_from(&tmp, Ceiling).0;
1360 // For y < 0 the bound |x^y| <= 2^((ex - 1) * y) is not strict, so if the product is
1361 // an exact integer the exponent bound must be bumped to keep |x^y| < 2^ebound
1362 // (mpfr_nextabove(tmp) in mpfr_pow); otherwise x = 2^(ex - 1) exactly achieves the
1363 // bound and a representable result would be misreported as underflow.
1364 if y.is_sign_negative() && tmp == ebound {
1365 ebound += 1;
1366 }
1367 let lim = i64::from(Self::MIN_EXPONENT) - if rm == Nearest { 2 } else { 1 };
1368 if ebound <= lim {
1369 return pow_underflow(
1370 prec,
1371 if rm == Nearest { Down } else { rm },
1372 x.is_sign_negative() && float_odd_integer(y),
1373 );
1374 }
1375 }
1376 }
1377 // y a not-too-large integer: use the multiplication-based algorithm
1378 if y_is_integer && ey <= POW_EXP_THRESHOLD {
1379 return pow_integer(x, &Integer::rounding_from(y, Nearest).0, prec, rm);
1380 }
1381 // (+/-2^b)^y, which could be exact
1382 if raw_power_of_2(x) {
1383 if x.is_sign_negative() {
1384 // necessarily ey > threshold; |x| <= 1/2 means underflow (overflow was already
1385 // detected above)
1386 let negative = float_odd_integer(y);
1387 return pow_underflow(prec, if rm == Nearest { Down } else { rm }, negative);
1388 }
1389 let b = ex - 1;
1390 let (tmp, o) = y.mul_prec_ref_val(Self::from(b), y.significant_bits() + 64);
1391 assert_eq!(o, Equal);
1392 return Self::power_of_2_of_float_prec_round(tmp, prec, rm);
1393 }
1394 // y * ln(x) very small: 1 + tiny
1395 let expx = if cmp_x_1 == Less { 1 - ex } else { ex };
1396 let logt = i64::exact_from(u64::exact_from(expx.max(1)).ceiling_log_base_2());
1397 let err = ey + logt;
1398 if err < -i64::exact_from(prec) - 1 {
1399 let above = y.is_sign_positive() == (cmp_x_1 == Greater);
1400 return float_one_plus_tiny(prec, rm, above);
1401 }
1402 pow_general(x, y, prec, rm, y_is_integer)
1403 }
1404}
1405
1406impl Float {
1407 #[allow(clippy::needless_pass_by_value)]
1408 /// Raises a [`Float`] to a [`Float`] power, rounding the result to the specified precision and
1409 /// with the specified rounding mode. Both [`Float`]s are taken by value. An [`Ordering`] is
1410 /// also returned, indicating whether the rounded power is less than, equal to, or greater than
1411 /// the exact power. Although `NaN`s are not comparable to any [`Float`], whenever this function
1412 /// returns a `NaN` it also returns `Equal`.
1413 ///
1414 /// See [`RoundingMode`] for a description of the possible rounding modes.
1415 ///
1416 /// $$
1417 /// f(x,y,p,m) = x^y+\varepsilon.
1418 /// $$
1419 /// - If $x^y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
1420 /// - If $x^y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
1421 /// 2^{\lfloor\log_2 |x^y|\rfloor-p+1}$.
1422 /// - If $x^y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
1423 /// 2^{\lfloor\log_2 |x^y|\rfloor-p}$.
1424 ///
1425 /// If the output has a precision, it is `prec`.
1426 ///
1427 /// Special cases:
1428 /// - $f(x,\pm0.0,p,m)=1.0$ for any $x$, even `NaN`
1429 /// - $f(1.0,y,p,m)=1.0$ for any $y$, even `NaN`
1430 /// - $f(\text{NaN},y,p,m)=f(x,\text{NaN},p,m)=\text{NaN}$ otherwise
1431 /// - $f(x,\infty,p,m)=\infty$ if $|x|>1$, and $0.0$ if $|x|<1$
1432 /// - $f(x,-\infty,p,m)=0.0$ if $|x|>1$, and $\infty$ if $|x|<1$
1433 /// - $f(-1.0,\pm\infty,p,m)=1.0$
1434 /// - $f(-1.0,y,p,m)=1.0$ if $y$ is an even integer, and $-1.0$ if $y$ is an odd integer
1435 /// - $f(\infty,y,p,m)=\infty$ if $y>0$, and $0.0$ if $y<0$
1436 /// - $f(-\infty,y,p,m)=-\infty$ if $y$ is a positive odd integer, $\infty$ if $y$ is positive
1437 /// and not an odd integer, $-0.0$ if $y$ is a negative odd integer, and $0.0$ if $y$ is
1438 /// negative and not an odd integer
1439 /// - $f(0.0,y,p,m)=0.0$ if $y>0$, and $\infty$ if $y<0$
1440 /// - $f(-0.0,y,p,m)=-0.0$ if $y$ is a positive odd integer, $0.0$ if $y$ is positive and not an
1441 /// odd integer, $-\infty$ if $y$ is a negative odd integer, and $\infty$ if $y$ is negative
1442 /// and not an odd integer
1443 /// - $f(x,y,p,m)=\text{NaN}$ if $x$ is finite and negative and $y$ is finite and not an integer
1444 ///
1445 /// Overflow and underflow:
1446 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
1447 /// returned instead.
1448 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor` or `Down`, $(1-(1/2)^p)2^{2^{30}-1}$
1449 /// is returned instead.
1450 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
1451 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
1452 /// instead.
1453 /// - If $0<f(x,y,p,m)\leq2^{-2^{30}-1}$ and $m$ is `Nearest`, $0.0$ is returned instead.
1454 /// - If $2^{-2^{30}-1}<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
1455 /// instead.
1456 /// - Negative results (from negative $x$ and odd integer $y$) mirror the bullets above, with
1457 /// the rounding directions reflected.
1458 ///
1459 /// If you know you'll be using `Nearest`, consider using [`Float::pow_prec`] instead. If you
1460 /// know that your target precision is the maximum of the precisions of the two inputs, consider
1461 /// using [`Float::pow_round`] instead. If both of these things are true, consider using
1462 /// [`Pow::pow`] instead.
1463 ///
1464 /// # Worst-case complexity
1465 /// $T(n) = O(n^{3/2} \log n \log\log n)$
1466 ///
1467 /// $M(n) = O(n (\log n)^2)$
1468 ///
1469 /// where $T$ is time, $M$ is additional memory, and $n$ is `prec`.
1470 ///
1471 /// # Panics
1472 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the given
1473 /// precision.
1474 ///
1475 /// # Examples
1476 /// ```
1477 /// use malachite_base::rounding_modes::RoundingMode::*;
1478 /// use malachite_float::Float;
1479 /// use std::cmp::Ordering::*;
1480 ///
1481 /// let (p, o) = Float::from(3).pow_prec_round(Float::from(2.5), 5, Floor);
1482 /// assert_eq!(p.to_string(), "15.5");
1483 /// assert_eq!(o, Less);
1484 ///
1485 /// let (p, o) = Float::from(3).pow_prec_round(Float::from(2.5), 5, Ceiling);
1486 /// assert_eq!(p.to_string(), "16.0");
1487 /// assert_eq!(o, Greater);
1488 ///
1489 /// let (p, o) = Float::from(3).pow_prec_round(Float::from(2.5), 5, Nearest);
1490 /// assert_eq!(p.to_string(), "15.5");
1491 /// assert_eq!(o, Less);
1492 ///
1493 /// let (p, o) = Float::from(3).pow_prec_round(Float::from(2.5), 20, Floor);
1494 /// assert_eq!(p.to_string(), "15.588455");
1495 /// assert_eq!(o, Less);
1496 ///
1497 /// let (p, o) = Float::from(3).pow_prec_round(Float::from(2.5), 20, Ceiling);
1498 /// assert_eq!(p.to_string(), "15.588470");
1499 /// assert_eq!(o, Greater);
1500 ///
1501 /// let (p, o) = Float::from(3).pow_prec_round(Float::from(2.5), 20, Nearest);
1502 /// assert_eq!(p.to_string(), "15.588455");
1503 /// assert_eq!(o, Less);
1504 /// ```
1505 #[inline]
1506 pub fn pow_prec_round(self, other: Self, prec: u64, rm: RoundingMode) -> (Self, Ordering) {
1507 self.pow_prec_round_ref_ref(&other, prec, rm)
1508 }
1509
1510 /// Raises a [`Float`] to a [`Float`] power, rounding the result to the specified precision and
1511 /// with the specified rounding mode. The first [`Float`] is taken by value and the second by
1512 /// reference. An [`Ordering`] is also returned, indicating whether the rounded power is less
1513 /// than, equal to, or greater than the exact power. Although `NaN`s are not comparable to any
1514 /// [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
1515 ///
1516 /// See [`RoundingMode`] for a description of the possible rounding modes.
1517 ///
1518 /// $$
1519 /// f(x,y,p,m) = x^y+\varepsilon.
1520 /// $$
1521 /// - If $x^y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
1522 /// - If $x^y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
1523 /// 2^{\lfloor\log_2 |x^y|\rfloor-p+1}$.
1524 /// - If $x^y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
1525 /// 2^{\lfloor\log_2 |x^y|\rfloor-p}$.
1526 ///
1527 /// If the output has a precision, it is `prec`.
1528 ///
1529 /// Special cases:
1530 /// - $f(x,\pm0.0,p,m)=1.0$ for any $x$, even `NaN`
1531 /// - $f(1.0,y,p,m)=1.0$ for any $y$, even `NaN`
1532 /// - $f(\text{NaN},y,p,m)=f(x,\text{NaN},p,m)=\text{NaN}$ otherwise
1533 /// - $f(x,\infty,p,m)=\infty$ if $|x|>1$, and $0.0$ if $|x|<1$
1534 /// - $f(x,-\infty,p,m)=0.0$ if $|x|>1$, and $\infty$ if $|x|<1$
1535 /// - $f(-1.0,\pm\infty,p,m)=1.0$
1536 /// - $f(-1.0,y,p,m)=1.0$ if $y$ is an even integer, and $-1.0$ if $y$ is an odd integer
1537 /// - $f(\infty,y,p,m)=\infty$ if $y>0$, and $0.0$ if $y<0$
1538 /// - $f(-\infty,y,p,m)=-\infty$ if $y$ is a positive odd integer, $\infty$ if $y$ is positive
1539 /// and not an odd integer, $-0.0$ if $y$ is a negative odd integer, and $0.0$ if $y$ is
1540 /// negative and not an odd integer
1541 /// - $f(0.0,y,p,m)=0.0$ if $y>0$, and $\infty$ if $y<0$
1542 /// - $f(-0.0,y,p,m)=-0.0$ if $y$ is a positive odd integer, $0.0$ if $y$ is positive and not an
1543 /// odd integer, $-\infty$ if $y$ is a negative odd integer, and $\infty$ if $y$ is negative
1544 /// and not an odd integer
1545 /// - $f(x,y,p,m)=\text{NaN}$ if $x$ is finite and negative and $y$ is finite and not an integer
1546 ///
1547 /// Overflow and underflow:
1548 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
1549 /// returned instead.
1550 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor` or `Down`, $(1-(1/2)^p)2^{2^{30}-1}$
1551 /// is returned instead.
1552 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
1553 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
1554 /// instead.
1555 /// - If $0<f(x,y,p,m)\leq2^{-2^{30}-1}$ and $m$ is `Nearest`, $0.0$ is returned instead.
1556 /// - If $2^{-2^{30}-1}<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
1557 /// instead.
1558 /// - Negative results (from negative $x$ and odd integer $y$) mirror the bullets above, with
1559 /// the rounding directions reflected.
1560 ///
1561 /// If you know you'll be using `Nearest`, consider using [`Float::pow_prec_val_ref`] instead.
1562 /// If you know that your target precision is the maximum of the precisions of the two inputs,
1563 /// consider using [`Float::pow_round_val_ref`] instead. If both of these things are true,
1564 /// consider using [`Pow::pow`] instead.
1565 ///
1566 /// # Worst-case complexity
1567 /// $T(n) = O(n^{3/2} \log n \log\log n)$
1568 ///
1569 /// $M(n) = O(n (\log n)^2)$
1570 ///
1571 /// where $T$ is time, $M$ is additional memory, and $n$ is `prec`.
1572 ///
1573 /// # Panics
1574 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the given
1575 /// precision.
1576 ///
1577 /// # Examples
1578 /// ```
1579 /// use malachite_base::rounding_modes::RoundingMode::*;
1580 /// use malachite_float::Float;
1581 /// use std::cmp::Ordering::*;
1582 ///
1583 /// let (p, o) = Float::from(3).pow_prec_round_val_ref(&Float::from(2.5), 5, Floor);
1584 /// assert_eq!(p.to_string(), "15.5");
1585 /// assert_eq!(o, Less);
1586 ///
1587 /// let (p, o) = Float::from(3).pow_prec_round_val_ref(&Float::from(2.5), 5, Ceiling);
1588 /// assert_eq!(p.to_string(), "16.0");
1589 /// assert_eq!(o, Greater);
1590 ///
1591 /// let (p, o) = Float::from(3).pow_prec_round_val_ref(&Float::from(2.5), 5, Nearest);
1592 /// assert_eq!(p.to_string(), "15.5");
1593 /// assert_eq!(o, Less);
1594 ///
1595 /// let (p, o) = Float::from(3).pow_prec_round_val_ref(&Float::from(2.5), 20, Floor);
1596 /// assert_eq!(p.to_string(), "15.588455");
1597 /// assert_eq!(o, Less);
1598 ///
1599 /// let (p, o) = Float::from(3).pow_prec_round_val_ref(&Float::from(2.5), 20, Ceiling);
1600 /// assert_eq!(p.to_string(), "15.588470");
1601 /// assert_eq!(o, Greater);
1602 ///
1603 /// let (p, o) = Float::from(3).pow_prec_round_val_ref(&Float::from(2.5), 20, Nearest);
1604 /// assert_eq!(p.to_string(), "15.588455");
1605 /// assert_eq!(o, Less);
1606 /// ```
1607 #[inline]
1608 pub fn pow_prec_round_val_ref(
1609 self,
1610 other: &Self,
1611 prec: u64,
1612 rm: RoundingMode,
1613 ) -> (Self, Ordering) {
1614 self.pow_prec_round_ref_ref(other, prec, rm)
1615 }
1616
1617 #[allow(clippy::needless_pass_by_value)]
1618 /// Raises a [`Float`] to a [`Float`] power, rounding the result to the specified precision and
1619 /// with the specified rounding mode. The first [`Float`] is taken by reference and the second
1620 /// by value. An [`Ordering`] is also returned, indicating whether the rounded power is less
1621 /// than, equal to, or greater than the exact power. Although `NaN`s are not comparable to any
1622 /// [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
1623 ///
1624 /// See [`RoundingMode`] for a description of the possible rounding modes.
1625 ///
1626 /// $$
1627 /// f(x,y,p,m) = x^y+\varepsilon.
1628 /// $$
1629 /// - If $x^y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
1630 /// - If $x^y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
1631 /// 2^{\lfloor\log_2 |x^y|\rfloor-p+1}$.
1632 /// - If $x^y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
1633 /// 2^{\lfloor\log_2 |x^y|\rfloor-p}$.
1634 ///
1635 /// If the output has a precision, it is `prec`.
1636 ///
1637 /// Special cases:
1638 /// - $f(x,\pm0.0,p,m)=1.0$ for any $x$, even `NaN`
1639 /// - $f(1.0,y,p,m)=1.0$ for any $y$, even `NaN`
1640 /// - $f(\text{NaN},y,p,m)=f(x,\text{NaN},p,m)=\text{NaN}$ otherwise
1641 /// - $f(x,\infty,p,m)=\infty$ if $|x|>1$, and $0.0$ if $|x|<1$
1642 /// - $f(x,-\infty,p,m)=0.0$ if $|x|>1$, and $\infty$ if $|x|<1$
1643 /// - $f(-1.0,\pm\infty,p,m)=1.0$
1644 /// - $f(-1.0,y,p,m)=1.0$ if $y$ is an even integer, and $-1.0$ if $y$ is an odd integer
1645 /// - $f(\infty,y,p,m)=\infty$ if $y>0$, and $0.0$ if $y<0$
1646 /// - $f(-\infty,y,p,m)=-\infty$ if $y$ is a positive odd integer, $\infty$ if $y$ is positive
1647 /// and not an odd integer, $-0.0$ if $y$ is a negative odd integer, and $0.0$ if $y$ is
1648 /// negative and not an odd integer
1649 /// - $f(0.0,y,p,m)=0.0$ if $y>0$, and $\infty$ if $y<0$
1650 /// - $f(-0.0,y,p,m)=-0.0$ if $y$ is a positive odd integer, $0.0$ if $y$ is positive and not an
1651 /// odd integer, $-\infty$ if $y$ is a negative odd integer, and $\infty$ if $y$ is negative
1652 /// and not an odd integer
1653 /// - $f(x,y,p,m)=\text{NaN}$ if $x$ is finite and negative and $y$ is finite and not an integer
1654 ///
1655 /// Overflow and underflow:
1656 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
1657 /// returned instead.
1658 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor` or `Down`, $(1-(1/2)^p)2^{2^{30}-1}$
1659 /// is returned instead.
1660 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
1661 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
1662 /// instead.
1663 /// - If $0<f(x,y,p,m)\leq2^{-2^{30}-1}$ and $m$ is `Nearest`, $0.0$ is returned instead.
1664 /// - If $2^{-2^{30}-1}<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
1665 /// instead.
1666 /// - Negative results (from negative $x$ and odd integer $y$) mirror the bullets above, with
1667 /// the rounding directions reflected.
1668 ///
1669 /// If you know you'll be using `Nearest`, consider using [`Float::pow_prec_ref_val`] instead.
1670 /// If you know that your target precision is the maximum of the precisions of the two inputs,
1671 /// consider using [`Float::pow_round_ref_val`] instead. If both of these things are true,
1672 /// consider using [`Pow::pow`] instead.
1673 ///
1674 /// # Worst-case complexity
1675 /// $T(n) = O(n^{3/2} \log n \log\log n)$
1676 ///
1677 /// $M(n) = O(n (\log n)^2)$
1678 ///
1679 /// where $T$ is time, $M$ is additional memory, and $n$ is `prec`.
1680 ///
1681 /// # Panics
1682 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the given
1683 /// precision.
1684 ///
1685 /// # Examples
1686 /// ```
1687 /// use malachite_base::rounding_modes::RoundingMode::*;
1688 /// use malachite_float::Float;
1689 /// use std::cmp::Ordering::*;
1690 ///
1691 /// let (p, o) = (&Float::from(3)).pow_prec_round_ref_val(Float::from(2.5), 5, Floor);
1692 /// assert_eq!(p.to_string(), "15.5");
1693 /// assert_eq!(o, Less);
1694 ///
1695 /// let (p, o) = (&Float::from(3)).pow_prec_round_ref_val(Float::from(2.5), 5, Ceiling);
1696 /// assert_eq!(p.to_string(), "16.0");
1697 /// assert_eq!(o, Greater);
1698 ///
1699 /// let (p, o) = (&Float::from(3)).pow_prec_round_ref_val(Float::from(2.5), 5, Nearest);
1700 /// assert_eq!(p.to_string(), "15.5");
1701 /// assert_eq!(o, Less);
1702 ///
1703 /// let (p, o) = (&Float::from(3)).pow_prec_round_ref_val(Float::from(2.5), 20, Floor);
1704 /// assert_eq!(p.to_string(), "15.588455");
1705 /// assert_eq!(o, Less);
1706 ///
1707 /// let (p, o) = (&Float::from(3)).pow_prec_round_ref_val(Float::from(2.5), 20, Ceiling);
1708 /// assert_eq!(p.to_string(), "15.588470");
1709 /// assert_eq!(o, Greater);
1710 ///
1711 /// let (p, o) = (&Float::from(3)).pow_prec_round_ref_val(Float::from(2.5), 20, Nearest);
1712 /// assert_eq!(p.to_string(), "15.588455");
1713 /// assert_eq!(o, Less);
1714 /// ```
1715 #[inline]
1716 pub fn pow_prec_round_ref_val(
1717 &self,
1718 other: Self,
1719 prec: u64,
1720 rm: RoundingMode,
1721 ) -> (Self, Ordering) {
1722 self.pow_prec_round_ref_ref(&other, prec, rm)
1723 }
1724
1725 #[allow(clippy::needless_pass_by_value)]
1726 /// Raises a [`Float`] to a [`Float`] power, rounding the result to the specified precision and
1727 /// to the nearest value. Both [`Float`]s are taken by value. An [`Ordering`] is also returned,
1728 /// indicating whether the rounded power is less than, equal to, or greater than the exact
1729 /// power. Although `NaN`s are not comparable to any [`Float`], whenever this function returns a
1730 /// `NaN` it also returns `Equal`.
1731 ///
1732 /// If the power is equidistant from two [`Float`]s with the specified precision, the [`Float`]
1733 /// with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of
1734 /// the `Nearest` rounding mode.
1735 ///
1736 /// $$
1737 /// f(x,y,p) = x^y+\varepsilon.
1738 /// $$
1739 /// - If $x^y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
1740 /// - If $x^y$ is finite and nonzero, then $|\varepsilon| \leq 2^{\lfloor\log_2
1741 /// |x^y|\rfloor-p}$.
1742 ///
1743 /// If the output has a precision, it is `prec`.
1744 ///
1745 /// Special cases:
1746 /// - $f(x,\pm0.0,p)=1.0$ for any $x$, even `NaN`
1747 /// - $f(1.0,y,p)=1.0$ for any $y$, even `NaN`
1748 /// - $f(\text{NaN},y,p)=f(x,\text{NaN},p)=\text{NaN}$ otherwise
1749 /// - $f(x,\infty,p)=\infty$ if $|x|>1$, and $0.0$ if $|x|<1$
1750 /// - $f(x,-\infty,p)=0.0$ if $|x|>1$, and $\infty$ if $|x|<1$
1751 /// - $f(-1.0,\pm\infty,p)=1.0$
1752 /// - $f(-1.0,y,p)=1.0$ if $y$ is an even integer, and $-1.0$ if $y$ is an odd integer
1753 /// - $f(\infty,y,p)=\infty$ if $y>0$, and $0.0$ if $y<0$
1754 /// - $f(-\infty,y,p)=-\infty$ if $y$ is a positive odd integer, $\infty$ if $y$ is positive and
1755 /// not an odd integer, $-0.0$ if $y$ is a negative odd integer, and $0.0$ if $y$ is negative
1756 /// and not an odd integer
1757 /// - $f(0.0,y,p)=0.0$ if $y>0$, and $\infty$ if $y<0$
1758 /// - $f(-0.0,y,p)=-0.0$ if $y$ is a positive odd integer, $0.0$ if $y$ is positive and not an
1759 /// odd integer, $-\infty$ if $y$ is a negative odd integer, and $\infty$ if $y$ is negative
1760 /// and not an odd integer
1761 /// - $f(x,y,p)=\text{NaN}$ if $x$ is finite and negative and $y$ is finite and not an integer
1762 ///
1763 /// Overflow and underflow:
1764 /// - If $f(x,y,p)\geq 2^{2^{30}-1}$, $\infty$ is returned instead.
1765 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
1766 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
1767 /// - Negative results (from negative $x$ and odd integer $y$) mirror the bullets above.
1768 ///
1769 /// If you want to use a rounding mode other than `Nearest`, consider using
1770 /// [`Float::pow_prec_round`] instead. If you know that your target precision is the maximum of
1771 /// the precisions of the two inputs, consider using [`Pow::pow`] instead.
1772 ///
1773 /// # Worst-case complexity
1774 /// $T(n) = O(n^{3/2} \log n \log\log n)$
1775 ///
1776 /// $M(n) = O(n (\log n)^2)$
1777 ///
1778 /// where $T$ is time, $M$ is additional memory, and $n$ is `prec`.
1779 ///
1780 /// # Examples
1781 /// ```
1782 /// use malachite_float::Float;
1783 /// use std::cmp::Ordering::*;
1784 ///
1785 /// let (p, o) = Float::from(3).pow_prec(Float::from(2.5), 5);
1786 /// assert_eq!(p.to_string(), "15.5");
1787 /// assert_eq!(o, Less);
1788 ///
1789 /// let (p, o) = Float::from(3).pow_prec(Float::from(2.5), 20);
1790 /// assert_eq!(p.to_string(), "15.588455");
1791 /// assert_eq!(o, Less);
1792 /// ```
1793 #[inline]
1794 pub fn pow_prec(self, other: Self, prec: u64) -> (Self, Ordering) {
1795 self.pow_prec_ref_ref(&other, prec)
1796 }
1797
1798 /// Raises a [`Float`] to a [`Float`] power, rounding the result to the specified precision and
1799 /// to the nearest value. Both [`Float`]s are taken by reference. An [`Ordering`] is also
1800 /// returned, indicating whether the rounded power is less than, equal to, or greater than the
1801 /// exact power. Although `NaN`s are not comparable to any [`Float`], whenever this function
1802 /// returns a `NaN` it also returns `Equal`.
1803 ///
1804 /// If the power is equidistant from two [`Float`]s with the specified precision, the [`Float`]
1805 /// with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of
1806 /// the `Nearest` rounding mode.
1807 ///
1808 /// $$
1809 /// f(x,y,p) = x^y+\varepsilon.
1810 /// $$
1811 /// - If $x^y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
1812 /// - If $x^y$ is finite and nonzero, then $|\varepsilon| \leq 2^{\lfloor\log_2
1813 /// |x^y|\rfloor-p}$.
1814 ///
1815 /// If the output has a precision, it is `prec`.
1816 ///
1817 /// Special cases:
1818 /// - $f(x,\pm0.0,p)=1.0$ for any $x$, even `NaN`
1819 /// - $f(1.0,y,p)=1.0$ for any $y$, even `NaN`
1820 /// - $f(\text{NaN},y,p)=f(x,\text{NaN},p)=\text{NaN}$ otherwise
1821 /// - $f(x,\infty,p)=\infty$ if $|x|>1$, and $0.0$ if $|x|<1$
1822 /// - $f(x,-\infty,p)=0.0$ if $|x|>1$, and $\infty$ if $|x|<1$
1823 /// - $f(-1.0,\pm\infty,p)=1.0$
1824 /// - $f(-1.0,y,p)=1.0$ if $y$ is an even integer, and $-1.0$ if $y$ is an odd integer
1825 /// - $f(\infty,y,p)=\infty$ if $y>0$, and $0.0$ if $y<0$
1826 /// - $f(-\infty,y,p)=-\infty$ if $y$ is a positive odd integer, $\infty$ if $y$ is positive and
1827 /// not an odd integer, $-0.0$ if $y$ is a negative odd integer, and $0.0$ if $y$ is negative
1828 /// and not an odd integer
1829 /// - $f(0.0,y,p)=0.0$ if $y>0$, and $\infty$ if $y<0$
1830 /// - $f(-0.0,y,p)=-0.0$ if $y$ is a positive odd integer, $0.0$ if $y$ is positive and not an
1831 /// odd integer, $-\infty$ if $y$ is a negative odd integer, and $\infty$ if $y$ is negative
1832 /// and not an odd integer
1833 /// - $f(x,y,p)=\text{NaN}$ if $x$ is finite and negative and $y$ is finite and not an integer
1834 ///
1835 /// Overflow and underflow:
1836 /// - If $f(x,y,p)\geq 2^{2^{30}-1}$, $\infty$ is returned instead.
1837 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
1838 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
1839 /// - Negative results (from negative $x$ and odd integer $y$) mirror the bullets above.
1840 ///
1841 /// If you want to use a rounding mode other than `Nearest`, consider using
1842 /// [`Float::pow_prec_round_ref_ref`] instead. If you know that your target precision is the
1843 /// maximum of the precisions of the two inputs, consider using [`Pow::pow`] instead.
1844 ///
1845 /// # Worst-case complexity
1846 /// $T(n) = O(n^{3/2} \log n \log\log n)$
1847 ///
1848 /// $M(n) = O(n (\log n)^2)$
1849 ///
1850 /// where $T$ is time, $M$ is additional memory, and $n$ is `prec`.
1851 ///
1852 /// # Examples
1853 /// ```
1854 /// use malachite_float::Float;
1855 /// use std::cmp::Ordering::*;
1856 ///
1857 /// let (p, o) = (&Float::from(3)).pow_prec_ref_ref(&Float::from(2.5), 5);
1858 /// assert_eq!(p.to_string(), "15.5");
1859 /// assert_eq!(o, Less);
1860 ///
1861 /// let (p, o) = (&Float::from(3)).pow_prec_ref_ref(&Float::from(2.5), 20);
1862 /// assert_eq!(p.to_string(), "15.588455");
1863 /// assert_eq!(o, Less);
1864 /// ```
1865 #[inline]
1866 pub fn pow_prec_ref_ref(&self, other: &Self, prec: u64) -> (Self, Ordering) {
1867 self.pow_prec_round_ref_ref(other, prec, Nearest)
1868 }
1869
1870 #[allow(clippy::needless_pass_by_value)]
1871 /// Raises a [`Float`] to a [`Float`] power, rounding the result to the maximum of the
1872 /// precisions of the two inputs and with the specified rounding mode. Both [`Float`]s are taken
1873 /// by value. An [`Ordering`] is also returned, indicating whether the rounded power is less
1874 /// than, equal to, or greater than the exact power. Although `NaN`s are not comparable to any
1875 /// [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
1876 ///
1877 /// See [`RoundingMode`] for a description of the possible rounding modes.
1878 ///
1879 /// $$
1880 /// f(x,y,p,m) = x^y+\varepsilon.
1881 /// $$
1882 /// - If $x^y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
1883 /// - If $x^y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
1884 /// 2^{\lfloor\log_2 |x^y|\rfloor-p+1}$.
1885 /// - If $x^y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
1886 /// 2^{\lfloor\log_2 |x^y|\rfloor-p}$.
1887 ///
1888 /// If the output has a precision, it is the maximum of the precisions of the inputs.
1889 ///
1890 /// Special cases:
1891 /// - $f(x,\pm0.0,m)=1.0$ for any $x$, even `NaN`
1892 /// - $f(1.0,y,m)=1.0$ for any $y$, even `NaN`
1893 /// - $f(\text{NaN},y,m)=f(x,\text{NaN},m)=\text{NaN}$ otherwise
1894 /// - $f(x,\infty,m)=\infty$ if $|x|>1$, and $0.0$ if $|x|<1$
1895 /// - $f(x,-\infty,m)=0.0$ if $|x|>1$, and $\infty$ if $|x|<1$
1896 /// - $f(-1.0,\pm\infty,m)=1.0$
1897 /// - $f(-1.0,y,m)=1.0$ if $y$ is an even integer, and $-1.0$ if $y$ is an odd integer
1898 /// - $f(\infty,y,m)=\infty$ if $y>0$, and $0.0$ if $y<0$
1899 /// - $f(-\infty,y,m)=-\infty$ if $y$ is a positive odd integer, $\infty$ if $y$ is positive and
1900 /// not an odd integer, $-0.0$ if $y$ is a negative odd integer, and $0.0$ if $y$ is negative
1901 /// and not an odd integer
1902 /// - $f(0.0,y,m)=0.0$ if $y>0$, and $\infty$ if $y<0$
1903 /// - $f(-0.0,y,m)=-0.0$ if $y$ is a positive odd integer, $0.0$ if $y$ is positive and not an
1904 /// odd integer, $-\infty$ if $y$ is a negative odd integer, and $\infty$ if $y$ is negative
1905 /// and not an odd integer
1906 /// - $f(x,y,m)=\text{NaN}$ if $x$ is finite and negative and $y$ is finite and not an integer
1907 ///
1908 /// Overflow and underflow:
1909 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
1910 /// returned instead.
1911 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor` or `Down`, $(1-(1/2)^p)2^{2^{30}-1}$ is
1912 /// returned instead.
1913 /// - If $0<f(x,y,m)<2^{-2^{30}}$ and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
1914 /// - If $0<f(x,y,m)<2^{-2^{30}}$ and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
1915 /// instead.
1916 /// - If $0<f(x,y,m)\leq2^{-2^{30}-1}$ and $m$ is `Nearest`, $0.0$ is returned instead.
1917 /// - If $2^{-2^{30}-1}<f(x,y,m)<2^{-2^{30}}$ and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
1918 /// instead.
1919 /// - Negative results (from negative $x$ and odd integer $y$) mirror the bullets above, with
1920 /// the rounding directions reflected.
1921 ///
1922 /// If you want to specify an output precision, consider using [`Float::pow_prec_round`]
1923 /// instead. If you know you'll be using the `Nearest` rounding mode, consider using
1924 /// [`Pow::pow`] instead.
1925 ///
1926 /// # Worst-case complexity
1927 /// $T(n) = O(n^{3/2} \log n \log\log n)$
1928 ///
1929 /// $M(n) = O(n (\log n)^2)$
1930 ///
1931 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
1932 /// other.significant_bits())`.
1933 ///
1934 /// # Panics
1935 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the given
1936 /// precision.
1937 ///
1938 /// # Examples
1939 /// ```
1940 /// use malachite_base::rounding_modes::RoundingMode::*;
1941 /// use malachite_float::Float;
1942 /// use std::cmp::Ordering::*;
1943 ///
1944 /// let (p, o) = Float::from(3).pow_round(Float::from(2.5), Floor);
1945 /// assert_eq!(p.to_string(), "14.0");
1946 /// assert_eq!(o, Less);
1947 ///
1948 /// let (p, o) = Float::from(3).pow_round(Float::from(2.5), Ceiling);
1949 /// assert_eq!(p.to_string(), "16.0");
1950 /// assert_eq!(o, Greater);
1951 ///
1952 /// let (p, o) = Float::from(3).pow_round(Float::from(2.5), Nearest);
1953 /// assert_eq!(p.to_string(), "16.0");
1954 /// assert_eq!(o, Greater);
1955 /// ```
1956 pub fn pow_round(self, other: Self, rm: RoundingMode) -> (Self, Ordering) {
1957 let prec = self.significant_bits().max(other.significant_bits());
1958 self.pow_prec_round_ref_ref(&other, prec, rm)
1959 }
1960
1961 /// Raises a [`Float`] to a [`Float`] power, rounding the result to the maximum of the
1962 /// precisions of the two inputs and with the specified rounding mode. Both [`Float`]s are taken
1963 /// by reference. An [`Ordering`] is also returned, indicating whether the rounded power is less
1964 /// than, equal to, or greater than the exact power. Although `NaN`s are not comparable to any
1965 /// [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
1966 ///
1967 /// See [`RoundingMode`] for a description of the possible rounding modes.
1968 ///
1969 /// $$
1970 /// f(x,y,p,m) = x^y+\varepsilon.
1971 /// $$
1972 /// - If $x^y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
1973 /// - If $x^y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
1974 /// 2^{\lfloor\log_2 |x^y|\rfloor-p+1}$.
1975 /// - If $x^y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
1976 /// 2^{\lfloor\log_2 |x^y|\rfloor-p}$.
1977 ///
1978 /// If the output has a precision, it is the maximum of the precisions of the inputs.
1979 ///
1980 /// Special cases:
1981 /// - $f(x,\pm0.0,m)=1.0$ for any $x$, even `NaN`
1982 /// - $f(1.0,y,m)=1.0$ for any $y$, even `NaN`
1983 /// - $f(\text{NaN},y,m)=f(x,\text{NaN},m)=\text{NaN}$ otherwise
1984 /// - $f(x,\infty,m)=\infty$ if $|x|>1$, and $0.0$ if $|x|<1$
1985 /// - $f(x,-\infty,m)=0.0$ if $|x|>1$, and $\infty$ if $|x|<1$
1986 /// - $f(-1.0,\pm\infty,m)=1.0$
1987 /// - $f(-1.0,y,m)=1.0$ if $y$ is an even integer, and $-1.0$ if $y$ is an odd integer
1988 /// - $f(\infty,y,m)=\infty$ if $y>0$, and $0.0$ if $y<0$
1989 /// - $f(-\infty,y,m)=-\infty$ if $y$ is a positive odd integer, $\infty$ if $y$ is positive and
1990 /// not an odd integer, $-0.0$ if $y$ is a negative odd integer, and $0.0$ if $y$ is negative
1991 /// and not an odd integer
1992 /// - $f(0.0,y,m)=0.0$ if $y>0$, and $\infty$ if $y<0$
1993 /// - $f(-0.0,y,m)=-0.0$ if $y$ is a positive odd integer, $0.0$ if $y$ is positive and not an
1994 /// odd integer, $-\infty$ if $y$ is a negative odd integer, and $\infty$ if $y$ is negative
1995 /// and not an odd integer
1996 /// - $f(x,y,m)=\text{NaN}$ if $x$ is finite and negative and $y$ is finite and not an integer
1997 ///
1998 /// Overflow and underflow:
1999 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
2000 /// returned instead.
2001 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor` or `Down`, $(1-(1/2)^p)2^{2^{30}-1}$ is
2002 /// returned instead.
2003 /// - If $0<f(x,y,m)<2^{-2^{30}}$ and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
2004 /// - If $0<f(x,y,m)<2^{-2^{30}}$ and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
2005 /// instead.
2006 /// - If $0<f(x,y,m)\leq2^{-2^{30}-1}$ and $m$ is `Nearest`, $0.0$ is returned instead.
2007 /// - If $2^{-2^{30}-1}<f(x,y,m)<2^{-2^{30}}$ and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
2008 /// instead.
2009 /// - Negative results (from negative $x$ and odd integer $y$) mirror the bullets above, with
2010 /// the rounding directions reflected.
2011 ///
2012 /// If you want to specify an output precision, consider using [`Float::pow_prec_round_ref_ref`]
2013 /// instead. If you know you'll be using the `Nearest` rounding mode, consider using
2014 /// [`Pow::pow`] instead.
2015 ///
2016 /// # Worst-case complexity
2017 /// $T(n) = O(n^{3/2} \log n \log\log n)$
2018 ///
2019 /// $M(n) = O(n (\log n)^2)$
2020 ///
2021 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
2022 /// other.significant_bits())`.
2023 ///
2024 /// # Panics
2025 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the given
2026 /// precision.
2027 ///
2028 /// # Examples
2029 /// ```
2030 /// use malachite_base::rounding_modes::RoundingMode::*;
2031 /// use malachite_float::Float;
2032 /// use std::cmp::Ordering::*;
2033 ///
2034 /// let (p, o) = (&Float::from(3)).pow_round_ref_ref(&Float::from(2.5), Floor);
2035 /// assert_eq!(p.to_string(), "14.0");
2036 /// assert_eq!(o, Less);
2037 ///
2038 /// let (p, o) = (&Float::from(3)).pow_round_ref_ref(&Float::from(2.5), Ceiling);
2039 /// assert_eq!(p.to_string(), "16.0");
2040 /// assert_eq!(o, Greater);
2041 ///
2042 /// let (p, o) = (&Float::from(3)).pow_round_ref_ref(&Float::from(2.5), Nearest);
2043 /// assert_eq!(p.to_string(), "16.0");
2044 /// assert_eq!(o, Greater);
2045 /// ```
2046 pub fn pow_round_ref_ref(&self, other: &Self, rm: RoundingMode) -> (Self, Ordering) {
2047 let prec = self.significant_bits().max(other.significant_bits());
2048 self.pow_prec_round_ref_ref(other, prec, rm)
2049 }
2050
2051 /// Raises a [`Float`] to a [`Float`] power, rounding the result to the maximum of the
2052 /// precisions of the two inputs and with the specified rounding mode. The first [`Float`] is
2053 /// taken by value and the second by reference. An [`Ordering`] is also returned, indicating
2054 /// whether the rounded power is less than, equal to, or greater than the exact power. Although
2055 /// `NaN`s are not comparable to any [`Float`], whenever this function returns a `NaN` it also
2056 /// returns `Equal`.
2057 ///
2058 /// See [`RoundingMode`] for a description of the possible rounding modes.
2059 ///
2060 /// $$
2061 /// f(x,y,p,m) = x^y+\varepsilon.
2062 /// $$
2063 /// - If $x^y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
2064 /// - If $x^y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
2065 /// 2^{\lfloor\log_2 |x^y|\rfloor-p+1}$.
2066 /// - If $x^y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
2067 /// 2^{\lfloor\log_2 |x^y|\rfloor-p}$.
2068 ///
2069 /// If the output has a precision, it is the maximum of the precisions of the inputs.
2070 ///
2071 /// Special cases:
2072 /// - $f(x,\pm0.0,m)=1.0$ for any $x$, even `NaN`
2073 /// - $f(1.0,y,m)=1.0$ for any $y$, even `NaN`
2074 /// - $f(\text{NaN},y,m)=f(x,\text{NaN},m)=\text{NaN}$ otherwise
2075 /// - $f(x,\infty,m)=\infty$ if $|x|>1$, and $0.0$ if $|x|<1$
2076 /// - $f(x,-\infty,m)=0.0$ if $|x|>1$, and $\infty$ if $|x|<1$
2077 /// - $f(-1.0,\pm\infty,m)=1.0$
2078 /// - $f(-1.0,y,m)=1.0$ if $y$ is an even integer, and $-1.0$ if $y$ is an odd integer
2079 /// - $f(\infty,y,m)=\infty$ if $y>0$, and $0.0$ if $y<0$
2080 /// - $f(-\infty,y,m)=-\infty$ if $y$ is a positive odd integer, $\infty$ if $y$ is positive and
2081 /// not an odd integer, $-0.0$ if $y$ is a negative odd integer, and $0.0$ if $y$ is negative
2082 /// and not an odd integer
2083 /// - $f(0.0,y,m)=0.0$ if $y>0$, and $\infty$ if $y<0$
2084 /// - $f(-0.0,y,m)=-0.0$ if $y$ is a positive odd integer, $0.0$ if $y$ is positive and not an
2085 /// odd integer, $-\infty$ if $y$ is a negative odd integer, and $\infty$ if $y$ is negative
2086 /// and not an odd integer
2087 /// - $f(x,y,m)=\text{NaN}$ if $x$ is finite and negative and $y$ is finite and not an integer
2088 ///
2089 /// Overflow and underflow:
2090 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
2091 /// returned instead.
2092 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor` or `Down`, $(1-(1/2)^p)2^{2^{30}-1}$ is
2093 /// returned instead.
2094 /// - If $0<f(x,y,m)<2^{-2^{30}}$ and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
2095 /// - If $0<f(x,y,m)<2^{-2^{30}}$ and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
2096 /// instead.
2097 /// - If $0<f(x,y,m)\leq2^{-2^{30}-1}$ and $m$ is `Nearest`, $0.0$ is returned instead.
2098 /// - If $2^{-2^{30}-1}<f(x,y,m)<2^{-2^{30}}$ and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
2099 /// instead.
2100 /// - Negative results (from negative $x$ and odd integer $y$) mirror the bullets above, with
2101 /// the rounding directions reflected.
2102 ///
2103 /// If you want to specify an output precision, consider using [`Float::pow_prec_round_val_ref`]
2104 /// instead. If you know you'll be using the `Nearest` rounding mode, consider using
2105 /// [`Pow::pow`] instead.
2106 ///
2107 /// # Worst-case complexity
2108 /// $T(n) = O(n^{3/2} \log n \log\log n)$
2109 ///
2110 /// $M(n) = O(n (\log n)^2)$
2111 ///
2112 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
2113 /// other.significant_bits())`.
2114 ///
2115 /// # Panics
2116 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the given
2117 /// precision.
2118 ///
2119 /// # Examples
2120 /// ```
2121 /// use malachite_base::rounding_modes::RoundingMode::*;
2122 /// use malachite_float::Float;
2123 /// use std::cmp::Ordering::*;
2124 ///
2125 /// let (p, o) = Float::from(3).pow_round_val_ref(&Float::from(2.5), Floor);
2126 /// assert_eq!(p.to_string(), "14.0");
2127 /// assert_eq!(o, Less);
2128 ///
2129 /// let (p, o) = Float::from(3).pow_round_val_ref(&Float::from(2.5), Ceiling);
2130 /// assert_eq!(p.to_string(), "16.0");
2131 /// assert_eq!(o, Greater);
2132 ///
2133 /// let (p, o) = Float::from(3).pow_round_val_ref(&Float::from(2.5), Nearest);
2134 /// assert_eq!(p.to_string(), "16.0");
2135 /// assert_eq!(o, Greater);
2136 /// ```
2137 #[inline]
2138 pub fn pow_round_val_ref(self, other: &Self, rm: RoundingMode) -> (Self, Ordering) {
2139 self.pow_round_ref_ref(other, rm)
2140 }
2141
2142 #[allow(clippy::needless_pass_by_value)]
2143 /// Raises a [`Float`] to a [`Float`] power, rounding the result to the maximum of the
2144 /// precisions of the two inputs and with the specified rounding mode. The first [`Float`] is
2145 /// taken by reference and the second by value. An [`Ordering`] is also returned, indicating
2146 /// whether the rounded power is less than, equal to, or greater than the exact power. Although
2147 /// `NaN`s are not comparable to any [`Float`], whenever this function returns a `NaN` it also
2148 /// returns `Equal`.
2149 ///
2150 /// See [`RoundingMode`] for a description of the possible rounding modes.
2151 ///
2152 /// $$
2153 /// f(x,y,p,m) = x^y+\varepsilon.
2154 /// $$
2155 /// - If $x^y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
2156 /// - If $x^y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
2157 /// 2^{\lfloor\log_2 |x^y|\rfloor-p+1}$.
2158 /// - If $x^y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
2159 /// 2^{\lfloor\log_2 |x^y|\rfloor-p}$.
2160 ///
2161 /// If the output has a precision, it is the maximum of the precisions of the inputs.
2162 ///
2163 /// Special cases:
2164 /// - $f(x,\pm0.0,m)=1.0$ for any $x$, even `NaN`
2165 /// - $f(1.0,y,m)=1.0$ for any $y$, even `NaN`
2166 /// - $f(\text{NaN},y,m)=f(x,\text{NaN},m)=\text{NaN}$ otherwise
2167 /// - $f(x,\infty,m)=\infty$ if $|x|>1$, and $0.0$ if $|x|<1$
2168 /// - $f(x,-\infty,m)=0.0$ if $|x|>1$, and $\infty$ if $|x|<1$
2169 /// - $f(-1.0,\pm\infty,m)=1.0$
2170 /// - $f(-1.0,y,m)=1.0$ if $y$ is an even integer, and $-1.0$ if $y$ is an odd integer
2171 /// - $f(\infty,y,m)=\infty$ if $y>0$, and $0.0$ if $y<0$
2172 /// - $f(-\infty,y,m)=-\infty$ if $y$ is a positive odd integer, $\infty$ if $y$ is positive and
2173 /// not an odd integer, $-0.0$ if $y$ is a negative odd integer, and $0.0$ if $y$ is negative
2174 /// and not an odd integer
2175 /// - $f(0.0,y,m)=0.0$ if $y>0$, and $\infty$ if $y<0$
2176 /// - $f(-0.0,y,m)=-0.0$ if $y$ is a positive odd integer, $0.0$ if $y$ is positive and not an
2177 /// odd integer, $-\infty$ if $y$ is a negative odd integer, and $\infty$ if $y$ is negative
2178 /// and not an odd integer
2179 /// - $f(x,y,m)=\text{NaN}$ if $x$ is finite and negative and $y$ is finite and not an integer
2180 ///
2181 /// Overflow and underflow:
2182 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
2183 /// returned instead.
2184 /// - If $f(x,y,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor` or `Down`, $(1-(1/2)^p)2^{2^{30}-1}$ is
2185 /// returned instead.
2186 /// - If $0<f(x,y,m)<2^{-2^{30}}$ and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
2187 /// - If $0<f(x,y,m)<2^{-2^{30}}$ and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
2188 /// instead.
2189 /// - If $0<f(x,y,m)\leq2^{-2^{30}-1}$ and $m$ is `Nearest`, $0.0$ is returned instead.
2190 /// - If $2^{-2^{30}-1}<f(x,y,m)<2^{-2^{30}}$ and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
2191 /// instead.
2192 /// - Negative results (from negative $x$ and odd integer $y$) mirror the bullets above, with
2193 /// the rounding directions reflected.
2194 ///
2195 /// If you want to specify an output precision, consider using [`Float::pow_prec_round_ref_val`]
2196 /// instead. If you know you'll be using the `Nearest` rounding mode, consider using
2197 /// [`Pow::pow`] instead.
2198 ///
2199 /// # Worst-case complexity
2200 /// $T(n) = O(n^{3/2} \log n \log\log n)$
2201 ///
2202 /// $M(n) = O(n (\log n)^2)$
2203 ///
2204 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
2205 /// other.significant_bits())`.
2206 ///
2207 /// # Panics
2208 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the given
2209 /// precision.
2210 ///
2211 /// # Examples
2212 /// ```
2213 /// use malachite_base::rounding_modes::RoundingMode::*;
2214 /// use malachite_float::Float;
2215 /// use std::cmp::Ordering::*;
2216 ///
2217 /// let (p, o) = (&Float::from(3)).pow_round_ref_val(Float::from(2.5), Floor);
2218 /// assert_eq!(p.to_string(), "14.0");
2219 /// assert_eq!(o, Less);
2220 ///
2221 /// let (p, o) = (&Float::from(3)).pow_round_ref_val(Float::from(2.5), Ceiling);
2222 /// assert_eq!(p.to_string(), "16.0");
2223 /// assert_eq!(o, Greater);
2224 ///
2225 /// let (p, o) = (&Float::from(3)).pow_round_ref_val(Float::from(2.5), Nearest);
2226 /// assert_eq!(p.to_string(), "16.0");
2227 /// assert_eq!(o, Greater);
2228 /// ```
2229 #[inline]
2230 pub fn pow_round_ref_val(&self, other: Self, rm: RoundingMode) -> (Self, Ordering) {
2231 self.pow_round_ref_ref(&other, rm)
2232 }
2233
2234 /// Raises a [`Float`] to a [`Float`] power, rounding the result to the specified precision and
2235 /// to the nearest value. The first [`Float`] is taken by value and the second by reference. An
2236 /// [`Ordering`] is also returned, indicating whether the rounded power is less than, equal to,
2237 /// or greater than the exact power. Although `NaN`s are not comparable to any [`Float`],
2238 /// whenever this function returns a `NaN` it also returns `Equal`.
2239 ///
2240 /// If the power is equidistant from two [`Float`]s with the specified precision, the [`Float`]
2241 /// with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of
2242 /// the `Nearest` rounding mode.
2243 ///
2244 /// $$
2245 /// f(x,y,p) = x^y+\varepsilon.
2246 /// $$
2247 /// - If $x^y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
2248 /// - If $x^y$ is finite and nonzero, then $|\varepsilon| \leq 2^{\lfloor\log_2
2249 /// |x^y|\rfloor-p}$.
2250 ///
2251 /// If the output has a precision, it is `prec`.
2252 ///
2253 /// Special cases:
2254 /// - $f(x,\pm0.0,p)=1.0$ for any $x$, even `NaN`
2255 /// - $f(1.0,y,p)=1.0$ for any $y$, even `NaN`
2256 /// - $f(\text{NaN},y,p)=f(x,\text{NaN},p)=\text{NaN}$ otherwise
2257 /// - $f(x,\infty,p)=\infty$ if $|x|>1$, and $0.0$ if $|x|<1$
2258 /// - $f(x,-\infty,p)=0.0$ if $|x|>1$, and $\infty$ if $|x|<1$
2259 /// - $f(-1.0,\pm\infty,p)=1.0$
2260 /// - $f(-1.0,y,p)=1.0$ if $y$ is an even integer, and $-1.0$ if $y$ is an odd integer
2261 /// - $f(\infty,y,p)=\infty$ if $y>0$, and $0.0$ if $y<0$
2262 /// - $f(-\infty,y,p)=-\infty$ if $y$ is a positive odd integer, $\infty$ if $y$ is positive and
2263 /// not an odd integer, $-0.0$ if $y$ is a negative odd integer, and $0.0$ if $y$ is negative
2264 /// and not an odd integer
2265 /// - $f(0.0,y,p)=0.0$ if $y>0$, and $\infty$ if $y<0$
2266 /// - $f(-0.0,y,p)=-0.0$ if $y$ is a positive odd integer, $0.0$ if $y$ is positive and not an
2267 /// odd integer, $-\infty$ if $y$ is a negative odd integer, and $\infty$ if $y$ is negative
2268 /// and not an odd integer
2269 /// - $f(x,y,p)=\text{NaN}$ if $x$ is finite and negative and $y$ is finite and not an integer
2270 ///
2271 /// Overflow and underflow:
2272 /// - If $f(x,y,p)\geq 2^{2^{30}-1}$, $\infty$ is returned instead.
2273 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
2274 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
2275 /// - Negative results (from negative $x$ and odd integer $y$) mirror the bullets above.
2276 ///
2277 /// If you want to use a rounding mode other than `Nearest`, consider using
2278 /// [`Float::pow_prec_round_val_ref`] instead. If you know that your target precision is the
2279 /// maximum of the precisions of the two inputs, consider using [`Pow::pow`] instead.
2280 ///
2281 /// # Worst-case complexity
2282 /// $T(n) = O(n^{3/2} \log n \log\log n)$
2283 ///
2284 /// $M(n) = O(n (\log n)^2)$
2285 ///
2286 /// where $T$ is time, $M$ is additional memory, and $n$ is `prec`.
2287 ///
2288 /// # Examples
2289 /// ```
2290 /// use malachite_float::Float;
2291 /// use std::cmp::Ordering::*;
2292 ///
2293 /// let (p, o) = Float::from(3).pow_prec_val_ref(&Float::from(2.5), 5);
2294 /// assert_eq!(p.to_string(), "15.5");
2295 /// assert_eq!(o, Less);
2296 ///
2297 /// let (p, o) = Float::from(3).pow_prec_val_ref(&Float::from(2.5), 20);
2298 /// assert_eq!(p.to_string(), "15.588455");
2299 /// assert_eq!(o, Less);
2300 /// ```
2301 #[inline]
2302 pub fn pow_prec_val_ref(self, other: &Self, prec: u64) -> (Self, Ordering) {
2303 self.pow_prec_ref_ref(other, prec)
2304 }
2305
2306 #[allow(clippy::needless_pass_by_value)]
2307 /// Raises a [`Float`] to a [`Float`] power, rounding the result to the specified precision and
2308 /// to the nearest value. The first [`Float`] is taken by reference and the second by value. An
2309 /// [`Ordering`] is also returned, indicating whether the rounded power is less than, equal to,
2310 /// or greater than the exact power. Although `NaN`s are not comparable to any [`Float`],
2311 /// whenever this function returns a `NaN` it also returns `Equal`.
2312 ///
2313 /// If the power is equidistant from two [`Float`]s with the specified precision, the [`Float`]
2314 /// with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of
2315 /// the `Nearest` rounding mode.
2316 ///
2317 /// $$
2318 /// f(x,y,p) = x^y+\varepsilon.
2319 /// $$
2320 /// - If $x^y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
2321 /// - If $x^y$ is finite and nonzero, then $|\varepsilon| \leq 2^{\lfloor\log_2
2322 /// |x^y|\rfloor-p}$.
2323 ///
2324 /// If the output has a precision, it is `prec`.
2325 ///
2326 /// Special cases:
2327 /// - $f(x,\pm0.0,p)=1.0$ for any $x$, even `NaN`
2328 /// - $f(1.0,y,p)=1.0$ for any $y$, even `NaN`
2329 /// - $f(\text{NaN},y,p)=f(x,\text{NaN},p)=\text{NaN}$ otherwise
2330 /// - $f(x,\infty,p)=\infty$ if $|x|>1$, and $0.0$ if $|x|<1$
2331 /// - $f(x,-\infty,p)=0.0$ if $|x|>1$, and $\infty$ if $|x|<1$
2332 /// - $f(-1.0,\pm\infty,p)=1.0$
2333 /// - $f(-1.0,y,p)=1.0$ if $y$ is an even integer, and $-1.0$ if $y$ is an odd integer
2334 /// - $f(\infty,y,p)=\infty$ if $y>0$, and $0.0$ if $y<0$
2335 /// - $f(-\infty,y,p)=-\infty$ if $y$ is a positive odd integer, $\infty$ if $y$ is positive and
2336 /// not an odd integer, $-0.0$ if $y$ is a negative odd integer, and $0.0$ if $y$ is negative
2337 /// and not an odd integer
2338 /// - $f(0.0,y,p)=0.0$ if $y>0$, and $\infty$ if $y<0$
2339 /// - $f(-0.0,y,p)=-0.0$ if $y$ is a positive odd integer, $0.0$ if $y$ is positive and not an
2340 /// odd integer, $-\infty$ if $y$ is a negative odd integer, and $\infty$ if $y$ is negative
2341 /// and not an odd integer
2342 /// - $f(x,y,p)=\text{NaN}$ if $x$ is finite and negative and $y$ is finite and not an integer
2343 ///
2344 /// Overflow and underflow:
2345 /// - If $f(x,y,p)\geq 2^{2^{30}-1}$, $\infty$ is returned instead.
2346 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
2347 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
2348 /// - Negative results (from negative $x$ and odd integer $y$) mirror the bullets above.
2349 ///
2350 /// If you want to use a rounding mode other than `Nearest`, consider using
2351 /// [`Float::pow_prec_round_ref_val`] instead. If you know that your target precision is the
2352 /// maximum of the precisions of the two inputs, consider using [`Pow::pow`] instead.
2353 ///
2354 /// # Worst-case complexity
2355 /// $T(n) = O(n^{3/2} \log n \log\log n)$
2356 ///
2357 /// $M(n) = O(n (\log n)^2)$
2358 ///
2359 /// where $T$ is time, $M$ is additional memory, and $n$ is `prec`.
2360 ///
2361 /// # Examples
2362 /// ```
2363 /// use malachite_float::Float;
2364 /// use std::cmp::Ordering::*;
2365 ///
2366 /// let (p, o) = (&Float::from(3)).pow_prec_ref_val(Float::from(2.5), 5);
2367 /// assert_eq!(p.to_string(), "15.5");
2368 /// assert_eq!(o, Less);
2369 ///
2370 /// let (p, o) = (&Float::from(3)).pow_prec_ref_val(Float::from(2.5), 20);
2371 /// assert_eq!(p.to_string(), "15.588455");
2372 /// assert_eq!(o, Less);
2373 /// ```
2374 #[inline]
2375 pub fn pow_prec_ref_val(&self, other: Self, prec: u64) -> (Self, Ordering) {
2376 self.pow_prec_ref_ref(&other, prec)
2377 }
2378
2379 #[allow(clippy::needless_pass_by_value)]
2380 /// Raises a [`Float`] to a [`Float`] power in place, rounding the result to the specified
2381 /// precision and with the specified rounding mode. The [`Float`] on the right-hand side is
2382 /// taken by value. An [`Ordering`] is returned, indicating whether the rounded power is less
2383 /// than, equal to, or greater than the exact power. Although `NaN`s are not comparable to any
2384 /// [`Float`], whenever this function sets a `NaN` it also returns `Equal`.
2385 ///
2386 /// See [`RoundingMode`] for a description of the possible rounding modes.
2387 ///
2388 /// $$
2389 /// f(x,y,p,m) = x^y+\varepsilon.
2390 /// $$
2391 /// - If $x^y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
2392 /// - If $x^y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
2393 /// 2^{\lfloor\log_2 |x^y|\rfloor-p+1}$.
2394 /// - If $x^y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
2395 /// 2^{\lfloor\log_2 |x^y|\rfloor-p}$.
2396 ///
2397 /// If the output has a precision, it is `prec`.
2398 ///
2399 /// See the [`Float::pow_prec_round`] documentation for information on special cases, overflow,
2400 /// and underflow.
2401 ///
2402 /// If you know you'll be using `Nearest`, consider using [`Float::pow_prec_assign`] instead. If
2403 /// you know that your target precision is the maximum of the precisions of the two inputs,
2404 /// consider using [`Float::pow_round_assign`] instead. If both of these things are true,
2405 /// consider using [`PowAssign::pow_assign`] instead.
2406 ///
2407 /// # Worst-case complexity
2408 /// $T(n) = O(n^{3/2} \log n \log\log n)$
2409 ///
2410 /// $M(n) = O(n (\log n)^2)$
2411 ///
2412 /// where $T$ is time, $M$ is additional memory, and $n$ is `prec`.
2413 ///
2414 /// # Panics
2415 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the given
2416 /// precision.
2417 ///
2418 /// # Examples
2419 /// ```
2420 /// use malachite_base::rounding_modes::RoundingMode::*;
2421 /// use malachite_float::Float;
2422 /// use std::cmp::Ordering::*;
2423 ///
2424 /// let mut x = Float::from(3);
2425 /// assert_eq!(x.pow_prec_round_assign(Float::from(2.5), 5, Floor), Less);
2426 /// assert_eq!(x.to_string(), "15.5");
2427 ///
2428 /// let mut x = Float::from(3);
2429 /// assert_eq!(
2430 /// x.pow_prec_round_assign(Float::from(2.5), 5, Ceiling),
2431 /// Greater
2432 /// );
2433 /// assert_eq!(x.to_string(), "16.0");
2434 ///
2435 /// let mut x = Float::from(3);
2436 /// assert_eq!(x.pow_prec_round_assign(Float::from(2.5), 5, Nearest), Less);
2437 /// assert_eq!(x.to_string(), "15.5");
2438 ///
2439 /// let mut x = Float::from(3);
2440 /// assert_eq!(x.pow_prec_round_assign(Float::from(2.5), 20, Floor), Less);
2441 /// assert_eq!(x.to_string(), "15.588455");
2442 ///
2443 /// let mut x = Float::from(3);
2444 /// assert_eq!(
2445 /// x.pow_prec_round_assign(Float::from(2.5), 20, Ceiling),
2446 /// Greater
2447 /// );
2448 /// assert_eq!(x.to_string(), "15.588470");
2449 ///
2450 /// let mut x = Float::from(3);
2451 /// assert_eq!(x.pow_prec_round_assign(Float::from(2.5), 20, Nearest), Less);
2452 /// assert_eq!(x.to_string(), "15.588455");
2453 /// ```
2454 pub fn pow_prec_round_assign(&mut self, other: Self, prec: u64, rm: RoundingMode) -> Ordering {
2455 let (result, o) = self.pow_prec_round_ref_ref(&other, prec, rm);
2456 *self = result;
2457 o
2458 }
2459
2460 /// Raises a [`Float`] to a [`Float`] power in place, rounding the result to the specified
2461 /// precision and with the specified rounding mode. The [`Float`] on the right-hand side is
2462 /// taken by reference. An [`Ordering`] is returned, indicating whether the rounded power is
2463 /// less than, equal to, or greater than the exact power. Although `NaN`s are not comparable to
2464 /// any [`Float`], whenever this function sets a `NaN` it also returns `Equal`.
2465 ///
2466 /// See [`RoundingMode`] for a description of the possible rounding modes.
2467 ///
2468 /// $$
2469 /// f(x,y,p,m) = x^y+\varepsilon.
2470 /// $$
2471 /// - If $x^y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
2472 /// - If $x^y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
2473 /// 2^{\lfloor\log_2 |x^y|\rfloor-p+1}$.
2474 /// - If $x^y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
2475 /// 2^{\lfloor\log_2 |x^y|\rfloor-p}$.
2476 ///
2477 /// If the output has a precision, it is `prec`.
2478 ///
2479 /// See the [`Float::pow_prec_round`] documentation for information on special cases, overflow,
2480 /// and underflow.
2481 ///
2482 /// If you know you'll be using `Nearest`, consider using [`Float::pow_prec_assign_ref`]
2483 /// instead. If you know that your target precision is the maximum of the precisions of the two
2484 /// inputs, consider using [`Float::pow_round_assign_ref`] instead. If both of these things are
2485 /// true, consider using [`PowAssign::pow_assign`] instead.
2486 ///
2487 /// # Worst-case complexity
2488 /// $T(n) = O(n^{3/2} \log n \log\log n)$
2489 ///
2490 /// $M(n) = O(n (\log n)^2)$
2491 ///
2492 /// where $T$ is time, $M$ is additional memory, and $n$ is `prec`.
2493 ///
2494 /// # Panics
2495 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the given
2496 /// precision.
2497 ///
2498 /// # Examples
2499 /// ```
2500 /// use malachite_base::rounding_modes::RoundingMode::*;
2501 /// use malachite_float::Float;
2502 /// use std::cmp::Ordering::*;
2503 ///
2504 /// let mut x = Float::from(3);
2505 /// assert_eq!(
2506 /// x.pow_prec_round_assign_ref(&Float::from(2.5), 5, Floor),
2507 /// Less
2508 /// );
2509 /// assert_eq!(x.to_string(), "15.5");
2510 ///
2511 /// let mut x = Float::from(3);
2512 /// assert_eq!(
2513 /// x.pow_prec_round_assign_ref(&Float::from(2.5), 5, Ceiling),
2514 /// Greater
2515 /// );
2516 /// assert_eq!(x.to_string(), "16.0");
2517 ///
2518 /// let mut x = Float::from(3);
2519 /// assert_eq!(
2520 /// x.pow_prec_round_assign_ref(&Float::from(2.5), 5, Nearest),
2521 /// Less
2522 /// );
2523 /// assert_eq!(x.to_string(), "15.5");
2524 ///
2525 /// let mut x = Float::from(3);
2526 /// assert_eq!(
2527 /// x.pow_prec_round_assign_ref(&Float::from(2.5), 20, Floor),
2528 /// Less
2529 /// );
2530 /// assert_eq!(x.to_string(), "15.588455");
2531 ///
2532 /// let mut x = Float::from(3);
2533 /// assert_eq!(
2534 /// x.pow_prec_round_assign_ref(&Float::from(2.5), 20, Ceiling),
2535 /// Greater
2536 /// );
2537 /// assert_eq!(x.to_string(), "15.588470");
2538 ///
2539 /// let mut x = Float::from(3);
2540 /// assert_eq!(
2541 /// x.pow_prec_round_assign_ref(&Float::from(2.5), 20, Nearest),
2542 /// Less
2543 /// );
2544 /// assert_eq!(x.to_string(), "15.588455");
2545 /// ```
2546 pub fn pow_prec_round_assign_ref(
2547 &mut self,
2548 other: &Self,
2549 prec: u64,
2550 rm: RoundingMode,
2551 ) -> Ordering {
2552 let (result, o) = self.pow_prec_round_ref_ref(other, prec, rm);
2553 *self = result;
2554 o
2555 }
2556
2557 /// Raises a [`Float`] to a [`Float`] power in place, rounding the result to the specified
2558 /// precision and to the nearest value. The [`Float`] on the right-hand side is taken by value.
2559 /// An [`Ordering`] is returned, indicating whether the rounded power is less than, equal to, or
2560 /// greater than the exact power. Although `NaN`s are not comparable to any [`Float`], whenever
2561 /// this function sets a `NaN` it also returns `Equal`.
2562 ///
2563 /// If the power is equidistant from two [`Float`]s with the specified precision, the [`Float`]
2564 /// with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of
2565 /// the `Nearest` rounding mode.
2566 ///
2567 /// $$
2568 /// f(x,y,p) = x^y+\varepsilon.
2569 /// $$
2570 /// - If $x^y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
2571 /// - If $x^y$ is finite and nonzero, then $|\varepsilon| \leq 2^{\lfloor\log_2
2572 /// |x^y|\rfloor-p}$.
2573 ///
2574 /// If the output has a precision, it is `prec`.
2575 ///
2576 /// See the [`Float::pow_prec_round`] documentation for information on special cases, overflow,
2577 /// and underflow.
2578 ///
2579 /// If you want to use a rounding mode other than `Nearest`, consider using
2580 /// [`Float::pow_prec_round_assign`] instead. If you know that your target precision is the
2581 /// maximum of the precisions of the two inputs, consider using [`PowAssign::pow_assign`]
2582 /// instead.
2583 ///
2584 /// # Worst-case complexity
2585 /// $T(n) = O(n^{3/2} \log n \log\log n)$
2586 ///
2587 /// $M(n) = O(n (\log n)^2)$
2588 ///
2589 /// where $T$ is time, $M$ is additional memory, and $n$ is `prec`.
2590 ///
2591 /// # Examples
2592 /// ```
2593 /// use malachite_float::Float;
2594 /// use std::cmp::Ordering::*;
2595 ///
2596 /// let mut x = Float::from(3);
2597 /// assert_eq!(x.pow_prec_assign(Float::from(2.5), 5), Less);
2598 /// assert_eq!(x.to_string(), "15.5");
2599 ///
2600 /// let mut x = Float::from(3);
2601 /// assert_eq!(x.pow_prec_assign(Float::from(2.5), 20), Less);
2602 /// assert_eq!(x.to_string(), "15.588455");
2603 /// ```
2604 #[inline]
2605 pub fn pow_prec_assign(&mut self, other: Self, prec: u64) -> Ordering {
2606 self.pow_prec_round_assign(other, prec, Nearest)
2607 }
2608
2609 /// Raises a [`Float`] to a [`Float`] power in place, rounding the result to the specified
2610 /// precision and to the nearest value. The [`Float`] on the right-hand side is taken by
2611 /// reference. An [`Ordering`] is returned, indicating whether the rounded power is less than,
2612 /// equal to, or greater than the exact power. Although `NaN`s are not comparable to any
2613 /// [`Float`], whenever this function sets a `NaN` it also returns `Equal`.
2614 ///
2615 /// If the power is equidistant from two [`Float`]s with the specified precision, the [`Float`]
2616 /// with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of
2617 /// the `Nearest` rounding mode.
2618 ///
2619 /// $$
2620 /// f(x,y,p) = x^y+\varepsilon.
2621 /// $$
2622 /// - If $x^y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
2623 /// - If $x^y$ is finite and nonzero, then $|\varepsilon| \leq 2^{\lfloor\log_2
2624 /// |x^y|\rfloor-p}$.
2625 ///
2626 /// If the output has a precision, it is `prec`.
2627 ///
2628 /// See the [`Float::pow_prec_round`] documentation for information on special cases, overflow,
2629 /// and underflow.
2630 ///
2631 /// If you want to use a rounding mode other than `Nearest`, consider using
2632 /// [`Float::pow_prec_round_assign_ref`] instead. If you know that your target precision is the
2633 /// maximum of the precisions of the two inputs, consider using [`PowAssign::pow_assign`]
2634 /// instead.
2635 ///
2636 /// # Worst-case complexity
2637 /// $T(n) = O(n^{3/2} \log n \log\log n)$
2638 ///
2639 /// $M(n) = O(n (\log n)^2)$
2640 ///
2641 /// where $T$ is time, $M$ is additional memory, and $n$ is `prec`.
2642 ///
2643 /// # Examples
2644 /// ```
2645 /// use malachite_float::Float;
2646 /// use std::cmp::Ordering::*;
2647 ///
2648 /// let mut x = Float::from(3);
2649 /// assert_eq!(x.pow_prec_assign_ref(&Float::from(2.5), 5), Less);
2650 /// assert_eq!(x.to_string(), "15.5");
2651 ///
2652 /// let mut x = Float::from(3);
2653 /// assert_eq!(x.pow_prec_assign_ref(&Float::from(2.5), 20), Less);
2654 /// assert_eq!(x.to_string(), "15.588455");
2655 /// ```
2656 #[inline]
2657 pub fn pow_prec_assign_ref(&mut self, other: &Self, prec: u64) -> Ordering {
2658 self.pow_prec_round_assign_ref(other, prec, Nearest)
2659 }
2660
2661 /// Raises a [`Float`] to a [`Float`] power in place, rounding the result to the maximum of the
2662 /// precisions of the two inputs and with the specified rounding mode. The [`Float`] on the
2663 /// right-hand side is taken by value. An [`Ordering`] is returned, indicating whether the
2664 /// rounded power is less than, equal to, or greater than the exact power. Although `NaN`s are
2665 /// not comparable to any [`Float`], whenever this function sets a `NaN` it also returns
2666 /// `Equal`.
2667 ///
2668 /// See [`RoundingMode`] for a description of the possible rounding modes.
2669 ///
2670 /// $$
2671 /// f(x,y,p,m) = x^y+\varepsilon.
2672 /// $$
2673 /// - If $x^y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
2674 /// - If $x^y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
2675 /// 2^{\lfloor\log_2 |x^y|\rfloor-p+1}$.
2676 /// - If $x^y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
2677 /// 2^{\lfloor\log_2 |x^y|\rfloor-p}$.
2678 ///
2679 /// If the output has a precision, it is the maximum of the precisions of the inputs.
2680 ///
2681 /// See the [`Float::pow_prec_round`] documentation for information on special cases, overflow,
2682 /// and underflow.
2683 ///
2684 /// If you want to specify an output precision, consider using [`Float::pow_prec_round_assign`]
2685 /// instead. If you know you'll be using the `Nearest` rounding mode, consider using
2686 /// [`PowAssign::pow_assign`] instead.
2687 ///
2688 /// # Worst-case complexity
2689 /// $T(n) = O(n^{3/2} \log n \log\log n)$
2690 ///
2691 /// $M(n) = O(n (\log n)^2)$
2692 ///
2693 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
2694 /// other.significant_bits())`.
2695 ///
2696 /// # Panics
2697 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the given
2698 /// precision.
2699 ///
2700 /// # Examples
2701 /// ```
2702 /// use malachite_base::rounding_modes::RoundingMode::*;
2703 /// use malachite_float::Float;
2704 /// use std::cmp::Ordering::*;
2705 ///
2706 /// let mut x = Float::from(3);
2707 /// assert_eq!(x.pow_round_assign(Float::from(2.5), Floor), Less);
2708 /// assert_eq!(x.to_string(), "14.0");
2709 ///
2710 /// let mut x = Float::from(3);
2711 /// assert_eq!(x.pow_round_assign(Float::from(2.5), Ceiling), Greater);
2712 /// assert_eq!(x.to_string(), "16.0");
2713 ///
2714 /// let mut x = Float::from(3);
2715 /// assert_eq!(x.pow_round_assign(Float::from(2.5), Nearest), Greater);
2716 /// assert_eq!(x.to_string(), "16.0");
2717 /// ```
2718 pub fn pow_round_assign(&mut self, other: Self, rm: RoundingMode) -> Ordering {
2719 let prec = self.significant_bits().max(other.significant_bits());
2720 self.pow_prec_round_assign(other, prec, rm)
2721 }
2722
2723 /// Raises a [`Float`] to a [`Float`] power in place, rounding the result to the maximum of the
2724 /// precisions of the two inputs and with the specified rounding mode. The [`Float`] on the
2725 /// right-hand side is taken by reference. An [`Ordering`] is returned, indicating whether the
2726 /// rounded power is less than, equal to, or greater than the exact power. Although `NaN`s are
2727 /// not comparable to any [`Float`], whenever this function sets a `NaN` it also returns
2728 /// `Equal`.
2729 ///
2730 /// See [`RoundingMode`] for a description of the possible rounding modes.
2731 ///
2732 /// $$
2733 /// f(x,y,p,m) = x^y+\varepsilon.
2734 /// $$
2735 /// - If $x^y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
2736 /// - If $x^y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
2737 /// 2^{\lfloor\log_2 |x^y|\rfloor-p+1}$.
2738 /// - If $x^y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
2739 /// 2^{\lfloor\log_2 |x^y|\rfloor-p}$.
2740 ///
2741 /// If the output has a precision, it is the maximum of the precisions of the inputs.
2742 ///
2743 /// See the [`Float::pow_prec_round`] documentation for information on special cases, overflow,
2744 /// and underflow.
2745 ///
2746 /// If you want to specify an output precision, consider using
2747 /// [`Float::pow_prec_round_assign_ref`] instead. If you know you'll be using the `Nearest`
2748 /// rounding mode, consider using [`PowAssign::pow_assign`] instead.
2749 ///
2750 /// # Worst-case complexity
2751 /// $T(n) = O(n^{3/2} \log n \log\log n)$
2752 ///
2753 /// $M(n) = O(n (\log n)^2)$
2754 ///
2755 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
2756 /// other.significant_bits())`.
2757 ///
2758 /// # Panics
2759 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the given
2760 /// precision.
2761 ///
2762 /// # Examples
2763 /// ```
2764 /// use malachite_base::rounding_modes::RoundingMode::*;
2765 /// use malachite_float::Float;
2766 /// use std::cmp::Ordering::*;
2767 ///
2768 /// let mut x = Float::from(3);
2769 /// assert_eq!(x.pow_round_assign_ref(&Float::from(2.5), Floor), Less);
2770 /// assert_eq!(x.to_string(), "14.0");
2771 ///
2772 /// let mut x = Float::from(3);
2773 /// assert_eq!(x.pow_round_assign_ref(&Float::from(2.5), Ceiling), Greater);
2774 /// assert_eq!(x.to_string(), "16.0");
2775 ///
2776 /// let mut x = Float::from(3);
2777 /// assert_eq!(x.pow_round_assign_ref(&Float::from(2.5), Nearest), Greater);
2778 /// assert_eq!(x.to_string(), "16.0");
2779 /// ```
2780 pub fn pow_round_assign_ref(&mut self, other: &Self, rm: RoundingMode) -> Ordering {
2781 let prec = self.significant_bits().max(other.significant_bits());
2782 self.pow_prec_round_assign_ref(other, prec, rm)
2783 }
2784}
2785
2786impl Pow<Self> for Float {
2787 type Output = Self;
2788
2789 /// Raises a [`Float`] to a [`Float`] power, rounding the result to the nearest value. Both
2790 /// [`Float`]s are taken by value.
2791 ///
2792 /// If the output has a precision, it is the maximum of the precisions of the inputs. If the
2793 /// power is equidistant from two [`Float`]s with the specified precision, the [`Float`] with
2794 /// fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of the
2795 /// `Nearest` rounding mode.
2796 ///
2797 /// $$
2798 /// f(x,y) = x^y+\varepsilon.
2799 /// $$
2800 /// - If $x^y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
2801 /// - If $x^y$ is finite and nonzero, then $|\varepsilon| \leq 2^{\lfloor\log_2
2802 /// |x^y|\rfloor-p}$, where $p$ is the maximum precision of the inputs.
2803 ///
2804 /// See the [`Float::pow_prec_round`] documentation for information on special cases, overflow,
2805 /// and underflow.
2806 ///
2807 /// If you want to specify an output precision, consider using [`Float::pow_prec`] instead. If
2808 /// you want both of these things, consider using [`Float::pow_prec_round`] instead.
2809 ///
2810 /// # Worst-case complexity
2811 /// $T(n) = O(n^{3/2} \log n \log\log n)$
2812 ///
2813 /// $M(n) = O(n (\log n)^2)$
2814 ///
2815 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
2816 /// other.significant_bits())`.
2817 ///
2818 /// # Examples
2819 /// ```
2820 /// use malachite_base::num::arithmetic::traits::Pow;
2821 /// use malachite_float::Float;
2822 ///
2823 /// assert_eq!(Float::from(3).pow(Float::from(2.5)).to_string(), "16.0");
2824 /// assert_eq!(Float::from(10).pow(Float::from(-0.5)).to_string(), "0.31");
2825 /// ```
2826 fn pow(self, other: Self) -> Self {
2827 let prec = self.significant_bits().max(other.significant_bits());
2828 self.pow_prec_ref_ref(&other, prec).0
2829 }
2830}
2831
2832impl Pow<&Self> for Float {
2833 type Output = Self;
2834
2835 /// Raises a [`Float`] to a [`Float`] power, rounding the result to the nearest value. The first
2836 /// [`Float`] is taken by value and the second by reference.
2837 ///
2838 /// If the output has a precision, it is the maximum of the precisions of the inputs. If the
2839 /// power is equidistant from two [`Float`]s with the specified precision, the [`Float`] with
2840 /// fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of the
2841 /// `Nearest` rounding mode.
2842 ///
2843 /// $$
2844 /// f(x,y) = x^y+\varepsilon.
2845 /// $$
2846 /// - If $x^y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
2847 /// - If $x^y$ is finite and nonzero, then $|\varepsilon| \leq 2^{\lfloor\log_2
2848 /// |x^y|\rfloor-p}$, where $p$ is the maximum precision of the inputs.
2849 ///
2850 /// See the [`Float::pow_prec_round`] documentation for information on special cases, overflow,
2851 /// and underflow.
2852 ///
2853 /// If you want to specify an output precision, consider using [`Float::pow_prec`] instead. If
2854 /// you want both of these things, consider using [`Float::pow_prec_round`] instead.
2855 ///
2856 /// # Worst-case complexity
2857 /// $T(n) = O(n^{3/2} \log n \log\log n)$
2858 ///
2859 /// $M(n) = O(n (\log n)^2)$
2860 ///
2861 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
2862 /// other.significant_bits())`.
2863 ///
2864 /// # Examples
2865 /// ```
2866 /// use malachite_base::num::arithmetic::traits::Pow;
2867 /// use malachite_float::Float;
2868 ///
2869 /// assert_eq!(Float::from(3).pow(&Float::from(2.5)).to_string(), "16.0");
2870 /// assert_eq!(Float::from(10).pow(&Float::from(-0.5)).to_string(), "0.31");
2871 /// ```
2872 fn pow(self, other: &Self) -> Self {
2873 let prec = self.significant_bits().max(other.significant_bits());
2874 self.pow_prec_ref_ref(other, prec).0
2875 }
2876}
2877
2878impl Pow<Float> for &Float {
2879 type Output = Float;
2880
2881 /// Raises a [`Float`] to a [`Float`] power, rounding the result to the nearest value. The first
2882 /// [`Float`] is taken by reference and the second by value.
2883 ///
2884 /// If the output has a precision, it is the maximum of the precisions of the inputs. If the
2885 /// power is equidistant from two [`Float`]s with the specified precision, the [`Float`] with
2886 /// fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of the
2887 /// `Nearest` rounding mode.
2888 ///
2889 /// $$
2890 /// f(x,y) = x^y+\varepsilon.
2891 /// $$
2892 /// - If $x^y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
2893 /// - If $x^y$ is finite and nonzero, then $|\varepsilon| \leq 2^{\lfloor\log_2
2894 /// |x^y|\rfloor-p}$, where $p$ is the maximum precision of the inputs.
2895 ///
2896 /// See the [`Float::pow_prec_round`] documentation for information on special cases, overflow,
2897 /// and underflow.
2898 ///
2899 /// If you want to specify an output precision, consider using [`Float::pow_prec`] instead. If
2900 /// you want both of these things, consider using [`Float::pow_prec_round`] instead.
2901 ///
2902 /// # Worst-case complexity
2903 /// $T(n) = O(n^{3/2} \log n \log\log n)$
2904 ///
2905 /// $M(n) = O(n (\log n)^2)$
2906 ///
2907 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
2908 /// other.significant_bits())`.
2909 ///
2910 /// # Examples
2911 /// ```
2912 /// use malachite_base::num::arithmetic::traits::Pow;
2913 /// use malachite_float::Float;
2914 ///
2915 /// assert_eq!((&Float::from(3)).pow(Float::from(2.5)).to_string(), "16.0");
2916 /// assert_eq!(
2917 /// (&Float::from(10)).pow(Float::from(-0.5)).to_string(),
2918 /// "0.31"
2919 /// );
2920 /// ```
2921 fn pow(self, other: Float) -> Float {
2922 let prec = self.significant_bits().max(other.significant_bits());
2923 self.pow_prec_ref_ref(&other, prec).0
2924 }
2925}
2926
2927impl Pow<&Float> for &Float {
2928 type Output = Float;
2929
2930 /// Raises a [`Float`] to a [`Float`] power, rounding the result to the nearest value. Both
2931 /// [`Float`]s are taken by reference.
2932 ///
2933 /// If the output has a precision, it is the maximum of the precisions of the inputs. If the
2934 /// power is equidistant from two [`Float`]s with the specified precision, the [`Float`] with
2935 /// fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of the
2936 /// `Nearest` rounding mode.
2937 ///
2938 /// $$
2939 /// f(x,y) = x^y+\varepsilon.
2940 /// $$
2941 /// - If $x^y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
2942 /// - If $x^y$ is finite and nonzero, then $|\varepsilon| \leq 2^{\lfloor\log_2
2943 /// |x^y|\rfloor-p}$, where $p$ is the maximum precision of the inputs.
2944 ///
2945 /// See the [`Float::pow_prec_round`] documentation for information on special cases, overflow,
2946 /// and underflow.
2947 ///
2948 /// If you want to specify an output precision, consider using [`Float::pow_prec`] instead. If
2949 /// you want both of these things, consider using [`Float::pow_prec_round`] instead.
2950 ///
2951 /// # Worst-case complexity
2952 /// $T(n) = O(n^{3/2} \log n \log\log n)$
2953 ///
2954 /// $M(n) = O(n (\log n)^2)$
2955 ///
2956 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
2957 /// other.significant_bits())`.
2958 ///
2959 /// # Examples
2960 /// ```
2961 /// use malachite_base::num::arithmetic::traits::Pow;
2962 /// use malachite_float::Float;
2963 ///
2964 /// assert_eq!((&Float::from(3)).pow(&Float::from(2.5)).to_string(), "16.0");
2965 /// assert_eq!(
2966 /// (&Float::from(10)).pow(&Float::from(-0.5)).to_string(),
2967 /// "0.31"
2968 /// );
2969 /// ```
2970 fn pow(self, other: &Float) -> Float {
2971 let prec = self.significant_bits().max(other.significant_bits());
2972 self.pow_prec_ref_ref(other, prec).0
2973 }
2974}
2975
2976impl PowAssign<Self> for Float {
2977 /// Raises a [`Float`] to a [`Float`] power in place, rounding the result to the nearest value.
2978 /// The [`Float`] on the right-hand side is taken by value.
2979 ///
2980 /// If the output has a precision, it is the maximum of the precisions of the inputs. If the
2981 /// power is equidistant from two [`Float`]s with the specified precision, the [`Float`] with
2982 /// fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of the
2983 /// `Nearest` rounding mode.
2984 ///
2985 /// $$
2986 /// f(x,y) = x^y+\varepsilon.
2987 /// $$
2988 /// - If $x^y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
2989 /// - If $x^y$ is finite and nonzero, then $|\varepsilon| \leq 2^{\lfloor\log_2
2990 /// |x^y|\rfloor-p}$, where $p$ is the maximum precision of the inputs.
2991 ///
2992 /// See the [`Float::pow_prec_round`] documentation for information on special cases, overflow,
2993 /// and underflow.
2994 ///
2995 /// If you want to specify an output precision, consider using [`Float::pow_prec`] instead. If
2996 /// you want both of these things, consider using [`Float::pow_prec_round`] instead.
2997 ///
2998 /// # Worst-case complexity
2999 /// $T(n) = O(n^{3/2} \log n \log\log n)$
3000 ///
3001 /// $M(n) = O(n (\log n)^2)$
3002 ///
3003 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
3004 /// other.significant_bits())`.
3005 ///
3006 /// # Examples
3007 /// ```
3008 /// use malachite_base::num::arithmetic::traits::PowAssign;
3009 /// use malachite_float::Float;
3010 ///
3011 /// let mut x = Float::from(3);
3012 /// x.pow_assign(Float::from(2.5));
3013 /// assert_eq!(x.to_string(), "16.0");
3014 /// ```
3015 fn pow_assign(&mut self, other: Self) {
3016 let prec = self.significant_bits().max(other.significant_bits());
3017 *self = self.pow_prec_ref_ref(&other, prec).0;
3018 }
3019}
3020
3021impl PowAssign<&Self> for Float {
3022 /// Raises a [`Float`] to a [`Float`] power in place, rounding the result to the nearest value.
3023 /// The [`Float`] on the right-hand side is taken by reference.
3024 ///
3025 /// If the output has a precision, it is the maximum of the precisions of the inputs. If the
3026 /// power is equidistant from two [`Float`]s with the specified precision, the [`Float`] with
3027 /// fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of the
3028 /// `Nearest` rounding mode.
3029 ///
3030 /// $$
3031 /// f(x,y) = x^y+\varepsilon.
3032 /// $$
3033 /// - If $x^y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
3034 /// - If $x^y$ is finite and nonzero, then $|\varepsilon| \leq 2^{\lfloor\log_2
3035 /// |x^y|\rfloor-p}$, where $p$ is the maximum precision of the inputs.
3036 ///
3037 /// See the [`Float::pow_prec_round`] documentation for information on special cases, overflow,
3038 /// and underflow.
3039 ///
3040 /// If you want to specify an output precision, consider using [`Float::pow_prec`] instead. If
3041 /// you want both of these things, consider using [`Float::pow_prec_round`] instead.
3042 ///
3043 /// # Worst-case complexity
3044 /// $T(n) = O(n^{3/2} \log n \log\log n)$
3045 ///
3046 /// $M(n) = O(n (\log n)^2)$
3047 ///
3048 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
3049 /// other.significant_bits())`.
3050 ///
3051 /// # Examples
3052 /// ```
3053 /// use malachite_base::num::arithmetic::traits::PowAssign;
3054 /// use malachite_float::Float;
3055 ///
3056 /// let mut x = Float::from(3);
3057 /// x.pow_assign(&Float::from(2.5));
3058 /// assert_eq!(x.to_string(), "16.0");
3059 /// ```
3060 fn pow_assign(&mut self, other: &Self) {
3061 let prec = self.significant_bits().max(other.significant_bits());
3062 *self = self.pow_prec_ref_ref(other, prec).0;
3063 }
3064}
3065
3066// Represents an `Integer` exactly as a `Float`, at just enough precision. Routes a `Float ^
3067// Integer` power through the `Float ^ Float` power, which dispatches to `pow_integer`.
3068fn integer_to_exact_float(z: Integer) -> Float {
3069 let prec = z.significant_bits().max(1);
3070 Float::from_integer_prec_round(z, prec, Exact).0
3071}
3072
3073impl Float {
3074 /// Raises a [`Float`] to the power of an [`Integer`], rounding the result to the specified
3075 /// precision and with the specified rounding mode. Both are taken by value. An [`Ordering`] is
3076 /// also returned, indicating whether the rounded power is less than, equal to, or greater than
3077 /// the exact power. Although `NaN`s are not comparable to any [`Float`], whenever this function
3078 /// returns a `NaN` it also returns `Equal`.
3079 ///
3080 /// See [`RoundingMode`] for a description of the possible rounding modes.
3081 ///
3082 /// $$
3083 /// f(x,n,p,m) = x^n+\varepsilon.
3084 /// $$
3085 /// - If $x^n$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
3086 /// - If $x^n$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
3087 /// 2^{\lfloor\log_2 |x^n|\rfloor-p+1}$.
3088 /// - If $x^n$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
3089 /// 2^{\lfloor\log_2 |x^n|\rfloor-p}$.
3090 ///
3091 /// See the [`Float::pow_integer_prec_round_ref_ref`] documentation for information on special
3092 /// cases, overflow, and underflow.
3093 ///
3094 /// # Worst-case complexity
3095 /// $T(n) = O(n^{3/2} \log n \log\log n)$
3096 ///
3097 /// $M(n) = O(n (\log n)^2)$
3098 ///
3099 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
3100 /// other.significant_bits())`.
3101 ///
3102 /// # Panics
3103 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the given
3104 /// precision.
3105 ///
3106 /// # Examples
3107 /// ```
3108 /// use malachite_base::rounding_modes::RoundingMode::*;
3109 /// use malachite_float::Float;
3110 /// use malachite_nz::integer::Integer;
3111 /// use std::cmp::Ordering::*;
3112 ///
3113 /// let (p, o) = Float::from(3).pow_integer_prec_round(Integer::from(5), 20, Floor);
3114 /// assert_eq!(p.to_string(), "243.00000");
3115 /// assert_eq!(o, Equal);
3116 ///
3117 /// let (p, o) = Float::from(3).pow_integer_prec_round(Integer::from(-2), 10, Ceiling);
3118 /// assert_eq!(p.to_string(), "0.11121");
3119 /// assert_eq!(o, Greater);
3120 /// ```
3121 #[inline]
3122 pub fn pow_integer_prec_round(
3123 self,
3124 other: Integer,
3125 prec: u64,
3126 rm: RoundingMode,
3127 ) -> (Self, Ordering) {
3128 self.pow_prec_round(integer_to_exact_float(other), prec, rm)
3129 }
3130
3131 /// Raises a [`Float`] to the power of an [`Integer`], rounding the result to the specified
3132 /// precision and with the specified rounding mode. The [`Float`] is taken by value and the
3133 /// [`Integer`] by reference. An [`Ordering`] is also returned, indicating whether the rounded
3134 /// power is less than, equal to, or greater than the exact power. Although `NaN`s are not
3135 /// comparable to any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
3136 ///
3137 /// See [`RoundingMode`] for a description of the possible rounding modes.
3138 ///
3139 /// $$
3140 /// f(x,n,p,m) = x^n+\varepsilon.
3141 /// $$
3142 /// - If $x^n$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
3143 /// - If $x^n$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
3144 /// 2^{\lfloor\log_2 |x^n|\rfloor-p+1}$.
3145 /// - If $x^n$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
3146 /// 2^{\lfloor\log_2 |x^n|\rfloor-p}$.
3147 ///
3148 /// See the [`Float::pow_integer_prec_round_ref_ref`] documentation for information on special
3149 /// cases, overflow, and underflow.
3150 ///
3151 /// # Worst-case complexity
3152 /// $T(n) = O(n^{3/2} \log n \log\log n)$
3153 ///
3154 /// $M(n) = O(n (\log n)^2)$
3155 ///
3156 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
3157 /// other.significant_bits())`.
3158 ///
3159 /// # Panics
3160 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the given
3161 /// precision.
3162 ///
3163 /// # Examples
3164 /// ```
3165 /// use malachite_base::rounding_modes::RoundingMode::*;
3166 /// use malachite_float::Float;
3167 /// use malachite_nz::integer::Integer;
3168 /// use std::cmp::Ordering::*;
3169 ///
3170 /// let (p, o) = Float::from(3).pow_integer_prec_round_val_ref(&Integer::from(5), 20, Floor);
3171 /// assert_eq!(p.to_string(), "243.00000");
3172 /// assert_eq!(o, Equal);
3173 ///
3174 /// let (p, o) = Float::from(3).pow_integer_prec_round_val_ref(&Integer::from(-2), 10, Ceiling);
3175 /// assert_eq!(p.to_string(), "0.11121");
3176 /// assert_eq!(o, Greater);
3177 /// ```
3178 #[inline]
3179 pub fn pow_integer_prec_round_val_ref(
3180 self,
3181 other: &Integer,
3182 prec: u64,
3183 rm: RoundingMode,
3184 ) -> (Self, Ordering) {
3185 self.pow_prec_round(integer_to_exact_float(other.clone()), prec, rm)
3186 }
3187
3188 /// Raises a [`Float`] to the power of an [`Integer`], rounding the result to the specified
3189 /// precision and with the specified rounding mode. The [`Float`] is taken by reference and the
3190 /// [`Integer`] by value. An [`Ordering`] is also returned, indicating whether the rounded power
3191 /// is less than, equal to, or greater than the exact power. Although `NaN`s are not comparable
3192 /// to any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
3193 ///
3194 /// See [`RoundingMode`] for a description of the possible rounding modes.
3195 ///
3196 /// $$
3197 /// f(x,n,p,m) = x^n+\varepsilon.
3198 /// $$
3199 /// - If $x^n$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
3200 /// - If $x^n$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
3201 /// 2^{\lfloor\log_2 |x^n|\rfloor-p+1}$.
3202 /// - If $x^n$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
3203 /// 2^{\lfloor\log_2 |x^n|\rfloor-p}$.
3204 ///
3205 /// See the [`Float::pow_integer_prec_round_ref_ref`] documentation for information on special
3206 /// cases, overflow, and underflow.
3207 ///
3208 /// # Worst-case complexity
3209 /// $T(n) = O(n^{3/2} \log n \log\log n)$
3210 ///
3211 /// $M(n) = O(n (\log n)^2)$
3212 ///
3213 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
3214 /// other.significant_bits())`.
3215 ///
3216 /// # Panics
3217 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the given
3218 /// precision.
3219 ///
3220 /// # Examples
3221 /// ```
3222 /// use malachite_base::rounding_modes::RoundingMode::*;
3223 /// use malachite_float::Float;
3224 /// use malachite_nz::integer::Integer;
3225 /// use std::cmp::Ordering::*;
3226 ///
3227 /// let (p, o) = (&Float::from(3)).pow_integer_prec_round_ref_val(Integer::from(5), 20, Floor);
3228 /// assert_eq!(p.to_string(), "243.00000");
3229 /// assert_eq!(o, Equal);
3230 ///
3231 /// let x = Float::from(3);
3232 /// let (p, o) = (&x).pow_integer_prec_round_ref_val(Integer::from(-2), 10, Ceiling);
3233 /// assert_eq!(p.to_string(), "0.11121");
3234 /// assert_eq!(o, Greater);
3235 /// ```
3236 #[inline]
3237 pub fn pow_integer_prec_round_ref_val(
3238 &self,
3239 other: Integer,
3240 prec: u64,
3241 rm: RoundingMode,
3242 ) -> (Self, Ordering) {
3243 self.pow_prec_round_ref_val(integer_to_exact_float(other), prec, rm)
3244 }
3245
3246 /// Raises a [`Float`] to the power of an [`Integer`], rounding the result to the specified
3247 /// precision and with the specified rounding mode. Both are taken by reference. An [`Ordering`]
3248 /// is also returned, indicating whether the rounded power is less than, equal to, or greater
3249 /// than the exact power. Although `NaN`s are not comparable to any [`Float`], whenever this
3250 /// function returns a `NaN` it also returns `Equal`.
3251 ///
3252 /// See [`RoundingMode`] for a description of the possible rounding modes.
3253 ///
3254 /// $$
3255 /// f(x,n,p,m) = x^n+\varepsilon.
3256 /// $$
3257 /// - If $x^n$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
3258 /// - If $x^n$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
3259 /// 2^{\lfloor\log_2 |x^n|\rfloor-p+1}$.
3260 /// - If $x^n$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
3261 /// 2^{\lfloor\log_2 |x^n|\rfloor-p}$.
3262 ///
3263 /// Special cases:
3264 /// - $f(x,0)=1.0$ for any $x$, even `NaN`
3265 /// - $f(1.0,n)=1.0$
3266 /// - $f(\text{NaN},n)=\text{NaN}$ if $n \neq 0$
3267 /// - $f(-1.0,n)=1.0$ if $n$ is even, and $-1.0$ if $n$ is odd
3268 /// - $f(\infty,n)=\infty$ if $n>0$, and $0.0$ if $n<0$
3269 /// - $f(-\infty,n)=-\infty$ if $n$ is positive and odd, $\infty$ if $n$ is positive and even,
3270 /// $-0.0$ if $n$ is negative and odd, and $0.0$ if $n$ is negative and even
3271 /// - $f(0.0,n)=0.0$ if $n>0$, and $\infty$ if $n<0$
3272 /// - $f(-0.0,n)=-0.0$ if $n$ is positive and odd, $0.0$ if $n$ is positive and even, $-\infty$
3273 /// if $n$ is negative and odd, and $\infty$ if $n$ is negative and even
3274 ///
3275 /// Overflow and underflow:
3276 /// - If $f(x,n,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
3277 /// returned instead.
3278 /// - If $f(x,n,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor` or `Down`, $(1-(1/2)^p)2^{2^{30}-1}$
3279 /// is returned instead.
3280 /// - If $0<f(x,n,p,m)<2^{-2^{30}}$ and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
3281 /// - If $0<f(x,n,p,m)<2^{-2^{30}}$ and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
3282 /// instead.
3283 /// - If $0<f(x,n,p,m)\leq2^{-2^{30}-1}$ and $m$ is `Nearest`, $0.0$ is returned instead.
3284 /// - If $2^{-2^{30}-1}<f(x,n,p,m)<2^{-2^{30}}$ and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
3285 /// instead.
3286 /// - Negative results (from negative $x$ and odd $n$) mirror the bullets above, with the
3287 /// rounding directions reflected.
3288 ///
3289 /// # Worst-case complexity
3290 /// $T(n) = O(n^{3/2} \log n \log\log n)$
3291 ///
3292 /// $M(n) = O(n (\log n)^2)$
3293 ///
3294 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
3295 /// other.significant_bits())`.
3296 ///
3297 /// # Panics
3298 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the given
3299 /// precision.
3300 ///
3301 /// # Examples
3302 /// ```
3303 /// use malachite_base::rounding_modes::RoundingMode::*;
3304 /// use malachite_float::Float;
3305 /// use malachite_nz::integer::Integer;
3306 /// use std::cmp::Ordering::*;
3307 ///
3308 /// let (p, o) = (&Float::from(3)).pow_integer_prec_round_ref_ref(&Integer::from(5), 20, Floor);
3309 /// assert_eq!(p.to_string(), "243.00000");
3310 /// assert_eq!(o, Equal);
3311 ///
3312 /// let x = Float::from(3);
3313 /// let (p, o) = (&x).pow_integer_prec_round_ref_ref(&Integer::from(-2), 10, Ceiling);
3314 /// assert_eq!(p.to_string(), "0.11121");
3315 /// assert_eq!(o, Greater);
3316 /// ```
3317 #[inline]
3318 pub fn pow_integer_prec_round_ref_ref(
3319 &self,
3320 other: &Integer,
3321 prec: u64,
3322 rm: RoundingMode,
3323 ) -> (Self, Ordering) {
3324 self.pow_prec_round_ref_val(integer_to_exact_float(other.clone()), prec, rm)
3325 }
3326
3327 /// Raises a [`Float`] to the power of an [`Integer`], rounding the result to the specified
3328 /// precision and to the nearest value. Both are taken by value. An [`Ordering`] is also
3329 /// returned, indicating whether the rounded power is less than, equal to, or greater than the
3330 /// exact power. Although `NaN`s are not comparable to any [`Float`], whenever this function
3331 /// returns a `NaN` it also returns `Equal`.
3332 ///
3333 /// If the power is equidistant from two [`Float`]s with the specified precision, the [`Float`]
3334 /// with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of
3335 /// the `Nearest` rounding mode.
3336 ///
3337 /// $$
3338 /// f(x,n,p) = x^n+\varepsilon.
3339 /// $$
3340 /// - If $x^n$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
3341 /// - If $x^n$ is finite and nonzero, then $|\varepsilon| \leq 2^{\lfloor\log_2
3342 /// |x^n|\rfloor-p}$.
3343 ///
3344 /// See the [`Float::pow_integer_prec_round_ref_ref`] documentation for information on special
3345 /// cases, overflow, and underflow.
3346 ///
3347 /// If you want to use a rounding mode other than `Nearest`, consider using
3348 /// [`Float::pow_integer_prec_round`] instead.
3349 ///
3350 /// # Worst-case complexity
3351 /// $T(n) = O(n^{3/2} \log n \log\log n)$
3352 ///
3353 /// $M(n) = O(n (\log n)^2)$
3354 ///
3355 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
3356 /// other.significant_bits())`.
3357 ///
3358 /// # Examples
3359 /// ```
3360 /// use malachite_float::Float;
3361 /// use malachite_nz::integer::Integer;
3362 /// use std::cmp::Ordering::*;
3363 ///
3364 /// let (p, o) = Float::from(3).pow_integer_prec(Integer::from(5), 20);
3365 /// assert_eq!(p.to_string(), "243.00000");
3366 /// assert_eq!(o, Equal);
3367 ///
3368 /// let (p, o) = Float::from(3).pow_integer_prec(Integer::from(-2), 10);
3369 /// assert_eq!(p.to_string(), "0.11108");
3370 /// assert_eq!(o, Less);
3371 /// ```
3372 #[inline]
3373 pub fn pow_integer_prec(self, other: Integer, prec: u64) -> (Self, Ordering) {
3374 self.pow_integer_prec_round(other, prec, Nearest)
3375 }
3376
3377 /// Raises a [`Float`] to the power of an [`Integer`], rounding the result to the specified
3378 /// precision and to the nearest value. The [`Float`] is taken by value and the [`Integer`] by
3379 /// reference. An [`Ordering`] is also returned, indicating whether the rounded power is less
3380 /// than, equal to, or greater than the exact power. Although `NaN`s are not comparable to any
3381 /// [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
3382 ///
3383 /// If the power is equidistant from two [`Float`]s with the specified precision, the [`Float`]
3384 /// with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of
3385 /// the `Nearest` rounding mode.
3386 ///
3387 /// $$
3388 /// f(x,n,p) = x^n+\varepsilon.
3389 /// $$
3390 /// - If $x^n$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
3391 /// - If $x^n$ is finite and nonzero, then $|\varepsilon| \leq 2^{\lfloor\log_2
3392 /// |x^n|\rfloor-p}$.
3393 ///
3394 /// See the [`Float::pow_integer_prec_round_ref_ref`] documentation for information on special
3395 /// cases, overflow, and underflow.
3396 ///
3397 /// If you want to use a rounding mode other than `Nearest`, consider using
3398 /// [`Float::pow_integer_prec_round_val_ref`] instead.
3399 ///
3400 /// # Worst-case complexity
3401 /// $T(n) = O(n^{3/2} \log n \log\log n)$
3402 ///
3403 /// $M(n) = O(n (\log n)^2)$
3404 ///
3405 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
3406 /// other.significant_bits())`.
3407 ///
3408 /// # Examples
3409 /// ```
3410 /// use malachite_float::Float;
3411 /// use malachite_nz::integer::Integer;
3412 /// use std::cmp::Ordering::*;
3413 ///
3414 /// let (p, o) = Float::from(3).pow_integer_prec_val_ref(&Integer::from(5), 20);
3415 /// assert_eq!(p.to_string(), "243.00000");
3416 /// assert_eq!(o, Equal);
3417 ///
3418 /// let (p, o) = Float::from(3).pow_integer_prec_val_ref(&Integer::from(-2), 10);
3419 /// assert_eq!(p.to_string(), "0.11108");
3420 /// assert_eq!(o, Less);
3421 /// ```
3422 #[inline]
3423 pub fn pow_integer_prec_val_ref(self, other: &Integer, prec: u64) -> (Self, Ordering) {
3424 self.pow_integer_prec_round_val_ref(other, prec, Nearest)
3425 }
3426
3427 /// Raises a [`Float`] to the power of an [`Integer`], rounding the result to the specified
3428 /// precision and to the nearest value. The [`Float`] is taken by reference and the [`Integer`]
3429 /// by value. An [`Ordering`] is also returned, indicating whether the rounded power is less
3430 /// than, equal to, or greater than the exact power. Although `NaN`s are not comparable to any
3431 /// [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
3432 ///
3433 /// If the power is equidistant from two [`Float`]s with the specified precision, the [`Float`]
3434 /// with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of
3435 /// the `Nearest` rounding mode.
3436 ///
3437 /// $$
3438 /// f(x,n,p) = x^n+\varepsilon.
3439 /// $$
3440 /// - If $x^n$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
3441 /// - If $x^n$ is finite and nonzero, then $|\varepsilon| \leq 2^{\lfloor\log_2
3442 /// |x^n|\rfloor-p}$.
3443 ///
3444 /// See the [`Float::pow_integer_prec_round_ref_ref`] documentation for information on special
3445 /// cases, overflow, and underflow.
3446 ///
3447 /// If you want to use a rounding mode other than `Nearest`, consider using
3448 /// [`Float::pow_integer_prec_round_ref_val`] instead.
3449 ///
3450 /// # Worst-case complexity
3451 /// $T(n) = O(n^{3/2} \log n \log\log n)$
3452 ///
3453 /// $M(n) = O(n (\log n)^2)$
3454 ///
3455 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
3456 /// other.significant_bits())`.
3457 ///
3458 /// # Examples
3459 /// ```
3460 /// use malachite_float::Float;
3461 /// use malachite_nz::integer::Integer;
3462 /// use std::cmp::Ordering::*;
3463 ///
3464 /// let (p, o) = (&Float::from(3)).pow_integer_prec_ref_val(Integer::from(5), 20);
3465 /// assert_eq!(p.to_string(), "243.00000");
3466 /// assert_eq!(o, Equal);
3467 ///
3468 /// let (p, o) = (&Float::from(3)).pow_integer_prec_ref_val(Integer::from(-2), 10);
3469 /// assert_eq!(p.to_string(), "0.11108");
3470 /// assert_eq!(o, Less);
3471 /// ```
3472 #[inline]
3473 pub fn pow_integer_prec_ref_val(&self, other: Integer, prec: u64) -> (Self, Ordering) {
3474 self.pow_integer_prec_round_ref_val(other, prec, Nearest)
3475 }
3476
3477 /// Raises a [`Float`] to the power of an [`Integer`], rounding the result to the specified
3478 /// precision and to the nearest value. Both are taken by reference. An [`Ordering`] is also
3479 /// returned, indicating whether the rounded power is less than, equal to, or greater than the
3480 /// exact power. Although `NaN`s are not comparable to any [`Float`], whenever this function
3481 /// returns a `NaN` it also returns `Equal`.
3482 ///
3483 /// If the power is equidistant from two [`Float`]s with the specified precision, the [`Float`]
3484 /// with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of
3485 /// the `Nearest` rounding mode.
3486 ///
3487 /// $$
3488 /// f(x,n,p) = x^n+\varepsilon.
3489 /// $$
3490 /// - If $x^n$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
3491 /// - If $x^n$ is finite and nonzero, then $|\varepsilon| \leq 2^{\lfloor\log_2
3492 /// |x^n|\rfloor-p}$.
3493 ///
3494 /// See the [`Float::pow_integer_prec_round_ref_ref`] documentation for information on special
3495 /// cases, overflow, and underflow.
3496 ///
3497 /// If you want to use a rounding mode other than `Nearest`, consider using
3498 /// [`Float::pow_integer_prec_round_ref_ref`] instead.
3499 ///
3500 /// # Worst-case complexity
3501 /// $T(n) = O(n^{3/2} \log n \log\log n)$
3502 ///
3503 /// $M(n) = O(n (\log n)^2)$
3504 ///
3505 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
3506 /// other.significant_bits())`.
3507 ///
3508 /// # Examples
3509 /// ```
3510 /// use malachite_float::Float;
3511 /// use malachite_nz::integer::Integer;
3512 /// use std::cmp::Ordering::*;
3513 ///
3514 /// let (p, o) = (&Float::from(3)).pow_integer_prec_ref_ref(&Integer::from(5), 20);
3515 /// assert_eq!(p.to_string(), "243.00000");
3516 /// assert_eq!(o, Equal);
3517 ///
3518 /// let (p, o) = (&Float::from(3)).pow_integer_prec_ref_ref(&Integer::from(-2), 10);
3519 /// assert_eq!(p.to_string(), "0.11108");
3520 /// assert_eq!(o, Less);
3521 /// ```
3522 #[inline]
3523 pub fn pow_integer_prec_ref_ref(&self, other: &Integer, prec: u64) -> (Self, Ordering) {
3524 self.pow_integer_prec_round_ref_ref(other, prec, Nearest)
3525 }
3526
3527 /// Raises a [`Float`] to the power of an [`Integer`], rounding the result to the precision of
3528 /// the base and with the specified rounding mode. Both are taken by value. An [`Ordering`] is
3529 /// also returned, indicating whether the rounded power is less than, equal to, or greater than
3530 /// the exact power. Although `NaN`s are not comparable to any [`Float`], whenever this function
3531 /// returns a `NaN` it also returns `Equal`.
3532 ///
3533 /// The output precision is the precision of `self`. See [`RoundingMode`] for a description of
3534 /// the possible rounding modes.
3535 ///
3536 /// $$
3537 /// f(x,n,p,m) = x^n+\varepsilon.
3538 /// $$
3539 /// - If $x^n$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
3540 /// - If $x^n$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
3541 /// 2^{\lfloor\log_2 |x^n|\rfloor-p+1}$.
3542 /// - If $x^n$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
3543 /// 2^{\lfloor\log_2 |x^n|\rfloor-p}$.
3544 ///
3545 /// See the [`Float::pow_integer_prec_round_ref_ref`] documentation for information on special
3546 /// cases, overflow, and underflow.
3547 ///
3548 /// If you want to specify an output precision, consider using [`Float::pow_integer_prec_round`]
3549 /// instead.
3550 ///
3551 /// # Worst-case complexity
3552 /// $T(n) = O(n^{3/2} \log n \log\log n)$
3553 ///
3554 /// $M(n) = O(n (\log n)^2)$
3555 ///
3556 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
3557 /// other.significant_bits())`.
3558 ///
3559 /// # Panics
3560 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the base's
3561 /// precision.
3562 ///
3563 /// # Examples
3564 /// ```
3565 /// use malachite_base::rounding_modes::RoundingMode::*;
3566 /// use malachite_float::Float;
3567 /// use malachite_nz::integer::Integer;
3568 /// use std::cmp::Ordering::*;
3569 ///
3570 /// let (p, o) = Float::from(3).pow_integer_round(Integer::from(5), Floor);
3571 /// assert_eq!(p.to_string(), "1.9e2");
3572 /// assert_eq!(o, Less);
3573 ///
3574 /// let (p, o) = Float::from(3).pow_integer_round(Integer::from(5), Ceiling);
3575 /// assert_eq!(p.to_string(), "2.6e2");
3576 /// assert_eq!(o, Greater);
3577 /// ```
3578 #[inline]
3579 pub fn pow_integer_round(self, other: Integer, rm: RoundingMode) -> (Self, Ordering) {
3580 let prec = self.significant_bits();
3581 self.pow_integer_prec_round(other, prec, rm)
3582 }
3583
3584 /// Raises a [`Float`] to the power of an [`Integer`], rounding the result to the precision of
3585 /// the base and with the specified rounding mode. The [`Float`] is taken by value and the
3586 /// [`Integer`] by reference. An [`Ordering`] is also returned, indicating whether the rounded
3587 /// power is less than, equal to, or greater than the exact power. Although `NaN`s are not
3588 /// comparable to any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
3589 ///
3590 /// The output precision is the precision of `self`. See [`RoundingMode`] for a description of
3591 /// the possible rounding modes.
3592 ///
3593 /// $$
3594 /// f(x,n,p,m) = x^n+\varepsilon.
3595 /// $$
3596 /// - If $x^n$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
3597 /// - If $x^n$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
3598 /// 2^{\lfloor\log_2 |x^n|\rfloor-p+1}$.
3599 /// - If $x^n$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
3600 /// 2^{\lfloor\log_2 |x^n|\rfloor-p}$.
3601 ///
3602 /// See the [`Float::pow_integer_prec_round_ref_ref`] documentation for information on special
3603 /// cases, overflow, and underflow.
3604 ///
3605 /// If you want to specify an output precision, consider using
3606 /// [`Float::pow_integer_prec_round_val_ref`] instead.
3607 ///
3608 /// # Worst-case complexity
3609 /// $T(n) = O(n^{3/2} \log n \log\log n)$
3610 ///
3611 /// $M(n) = O(n (\log n)^2)$
3612 ///
3613 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
3614 /// other.significant_bits())`.
3615 ///
3616 /// # Panics
3617 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the base's
3618 /// precision.
3619 ///
3620 /// # Examples
3621 /// ```
3622 /// use malachite_base::rounding_modes::RoundingMode::*;
3623 /// use malachite_float::Float;
3624 /// use malachite_nz::integer::Integer;
3625 /// use std::cmp::Ordering::*;
3626 ///
3627 /// let (p, o) = Float::from(3).pow_integer_round_val_ref(&Integer::from(5), Floor);
3628 /// assert_eq!(p.to_string(), "1.9e2");
3629 /// assert_eq!(o, Less);
3630 ///
3631 /// let (p, o) = Float::from(3).pow_integer_round_val_ref(&Integer::from(5), Ceiling);
3632 /// assert_eq!(p.to_string(), "2.6e2");
3633 /// assert_eq!(o, Greater);
3634 /// ```
3635 #[inline]
3636 pub fn pow_integer_round_val_ref(self, other: &Integer, rm: RoundingMode) -> (Self, Ordering) {
3637 let prec = self.significant_bits();
3638 self.pow_integer_prec_round_val_ref(other, prec, rm)
3639 }
3640
3641 /// Raises a [`Float`] to the power of an [`Integer`], rounding the result to the precision of
3642 /// the base and with the specified rounding mode. The [`Float`] is taken by reference and the
3643 /// [`Integer`] by value. An [`Ordering`] is also returned, indicating whether the rounded power
3644 /// is less than, equal to, or greater than the exact power. Although `NaN`s are not comparable
3645 /// to any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
3646 ///
3647 /// The output precision is the precision of `self`. See [`RoundingMode`] for a description of
3648 /// the possible rounding modes.
3649 ///
3650 /// $$
3651 /// f(x,n,p,m) = x^n+\varepsilon.
3652 /// $$
3653 /// - If $x^n$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
3654 /// - If $x^n$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
3655 /// 2^{\lfloor\log_2 |x^n|\rfloor-p+1}$.
3656 /// - If $x^n$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
3657 /// 2^{\lfloor\log_2 |x^n|\rfloor-p}$.
3658 ///
3659 /// See the [`Float::pow_integer_prec_round_ref_ref`] documentation for information on special
3660 /// cases, overflow, and underflow.
3661 ///
3662 /// If you want to specify an output precision, consider using
3663 /// [`Float::pow_integer_prec_round_ref_val`] instead.
3664 ///
3665 /// # Worst-case complexity
3666 /// $T(n) = O(n^{3/2} \log n \log\log n)$
3667 ///
3668 /// $M(n) = O(n (\log n)^2)$
3669 ///
3670 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
3671 /// other.significant_bits())`.
3672 ///
3673 /// # Panics
3674 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the base's
3675 /// precision.
3676 ///
3677 /// # Examples
3678 /// ```
3679 /// use malachite_base::rounding_modes::RoundingMode::*;
3680 /// use malachite_float::Float;
3681 /// use malachite_nz::integer::Integer;
3682 /// use std::cmp::Ordering::*;
3683 ///
3684 /// let (p, o) = (&Float::from(3)).pow_integer_round_ref_val(Integer::from(5), Floor);
3685 /// assert_eq!(p.to_string(), "1.9e2");
3686 /// assert_eq!(o, Less);
3687 ///
3688 /// let (p, o) = (&Float::from(3)).pow_integer_round_ref_val(Integer::from(5), Ceiling);
3689 /// assert_eq!(p.to_string(), "2.6e2");
3690 /// assert_eq!(o, Greater);
3691 /// ```
3692 #[inline]
3693 pub fn pow_integer_round_ref_val(&self, other: Integer, rm: RoundingMode) -> (Self, Ordering) {
3694 let prec = self.significant_bits();
3695 self.pow_integer_prec_round_ref_val(other, prec, rm)
3696 }
3697
3698 /// Raises a [`Float`] to the power of an [`Integer`], rounding the result to the precision of
3699 /// the base and with the specified rounding mode. Both are taken by reference. An [`Ordering`]
3700 /// is also returned, indicating whether the rounded power is less than, equal to, or greater
3701 /// than the exact power. Although `NaN`s are not comparable to any [`Float`], whenever this
3702 /// function returns a `NaN` it also returns `Equal`.
3703 ///
3704 /// The output precision is the precision of `self`. See [`RoundingMode`] for a description of
3705 /// the possible rounding modes.
3706 ///
3707 /// $$
3708 /// f(x,n,p,m) = x^n+\varepsilon.
3709 /// $$
3710 /// - If $x^n$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
3711 /// - If $x^n$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
3712 /// 2^{\lfloor\log_2 |x^n|\rfloor-p+1}$.
3713 /// - If $x^n$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
3714 /// 2^{\lfloor\log_2 |x^n|\rfloor-p}$.
3715 ///
3716 /// See the [`Float::pow_integer_prec_round_ref_ref`] documentation for information on special
3717 /// cases, overflow, and underflow.
3718 ///
3719 /// If you want to specify an output precision, consider using
3720 /// [`Float::pow_integer_prec_round_ref_ref`] instead.
3721 ///
3722 /// # Worst-case complexity
3723 /// $T(n) = O(n^{3/2} \log n \log\log n)$
3724 ///
3725 /// $M(n) = O(n (\log n)^2)$
3726 ///
3727 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
3728 /// other.significant_bits())`.
3729 ///
3730 /// # Panics
3731 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the base's
3732 /// precision.
3733 ///
3734 /// # Examples
3735 /// ```
3736 /// use malachite_base::rounding_modes::RoundingMode::*;
3737 /// use malachite_float::Float;
3738 /// use malachite_nz::integer::Integer;
3739 /// use std::cmp::Ordering::*;
3740 ///
3741 /// let (p, o) = (&Float::from(3)).pow_integer_round_ref_ref(&Integer::from(5), Floor);
3742 /// assert_eq!(p.to_string(), "1.9e2");
3743 /// assert_eq!(o, Less);
3744 ///
3745 /// let (p, o) = (&Float::from(3)).pow_integer_round_ref_ref(&Integer::from(5), Ceiling);
3746 /// assert_eq!(p.to_string(), "2.6e2");
3747 /// assert_eq!(o, Greater);
3748 /// ```
3749 #[inline]
3750 pub fn pow_integer_round_ref_ref(&self, other: &Integer, rm: RoundingMode) -> (Self, Ordering) {
3751 let prec = self.significant_bits();
3752 self.pow_integer_prec_round_ref_ref(other, prec, rm)
3753 }
3754
3755 /// Raises a [`Float`] to the power of an [`Integer`] in place, taking the [`Integer`] by value.
3756 ///
3757 /// See the [`Float::pow_integer_prec_round_ref_ref`] documentation for information on special
3758 /// cases, overflow, and underflow.
3759 ///
3760 /// # Worst-case complexity
3761 /// $T(n) = O(n^{3/2} \log n \log\log n)$
3762 ///
3763 /// $M(n) = O(n (\log n)^2)$
3764 ///
3765 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
3766 /// other.significant_bits())`.
3767 ///
3768 /// # Panics
3769 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the given
3770 /// precision.
3771 ///
3772 /// # Examples
3773 /// ```
3774 /// use malachite_base::rounding_modes::RoundingMode::*;
3775 /// use malachite_float::Float;
3776 /// use malachite_nz::integer::Integer;
3777 /// use std::cmp::Ordering::*;
3778 ///
3779 /// let mut x = Float::from(3);
3780 /// let o = x.pow_integer_prec_round_assign(Integer::from(5), 20, Floor);
3781 /// assert_eq!(x.to_string(), "243.00000");
3782 /// assert_eq!(o, Equal);
3783 /// ```
3784 #[inline]
3785 pub fn pow_integer_prec_round_assign(
3786 &mut self,
3787 other: Integer,
3788 prec: u64,
3789 rm: RoundingMode,
3790 ) -> Ordering {
3791 self.pow_prec_round_assign(integer_to_exact_float(other), prec, rm)
3792 }
3793
3794 /// Raises a [`Float`] to the power of an [`Integer`] in place, taking the [`Integer`] by
3795 /// reference.
3796 ///
3797 /// See the [`Float::pow_integer_prec_round_ref_ref`] documentation for information on special
3798 /// cases, overflow, and underflow.
3799 ///
3800 /// # Worst-case complexity
3801 /// $T(n) = O(n^{3/2} \log n \log\log n)$
3802 ///
3803 /// $M(n) = O(n (\log n)^2)$
3804 ///
3805 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
3806 /// other.significant_bits())`.
3807 ///
3808 /// # Panics
3809 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the given
3810 /// precision.
3811 ///
3812 /// # Examples
3813 /// ```
3814 /// use malachite_base::rounding_modes::RoundingMode::*;
3815 /// use malachite_float::Float;
3816 /// use malachite_nz::integer::Integer;
3817 /// use std::cmp::Ordering::*;
3818 ///
3819 /// let mut x = Float::from(3);
3820 /// let o = x.pow_integer_prec_round_assign_ref(&Integer::from(5), 20, Floor);
3821 /// assert_eq!(x.to_string(), "243.00000");
3822 /// assert_eq!(o, Equal);
3823 /// ```
3824 #[inline]
3825 pub fn pow_integer_prec_round_assign_ref(
3826 &mut self,
3827 other: &Integer,
3828 prec: u64,
3829 rm: RoundingMode,
3830 ) -> Ordering {
3831 self.pow_prec_round_assign(integer_to_exact_float(other.clone()), prec, rm)
3832 }
3833
3834 /// Raises a [`Float`] to the power of an [`Integer`] in place, taking the [`Integer`] by value.
3835 ///
3836 /// See the [`Float::pow_integer_prec_round_ref_ref`] documentation for information on special
3837 /// cases, overflow, and underflow.
3838 ///
3839 /// # Worst-case complexity
3840 /// $T(n) = O(n^{3/2} \log n \log\log n)$
3841 ///
3842 /// $M(n) = O(n (\log n)^2)$
3843 ///
3844 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
3845 /// other.significant_bits())`.
3846 ///
3847 /// # Examples
3848 /// ```
3849 /// use malachite_float::Float;
3850 /// use malachite_nz::integer::Integer;
3851 /// use std::cmp::Ordering::*;
3852 ///
3853 /// let mut x = Float::from(3);
3854 /// let o = x.pow_integer_prec_assign(Integer::from(5), 20);
3855 /// assert_eq!(x.to_string(), "243.00000");
3856 /// assert_eq!(o, Equal);
3857 /// ```
3858 #[inline]
3859 pub fn pow_integer_prec_assign(&mut self, other: Integer, prec: u64) -> Ordering {
3860 self.pow_prec_assign(integer_to_exact_float(other), prec)
3861 }
3862
3863 /// Raises a [`Float`] to the power of an [`Integer`] in place, taking the [`Integer`] by
3864 /// reference.
3865 ///
3866 /// See the [`Float::pow_integer_prec_round_ref_ref`] documentation for information on special
3867 /// cases, overflow, and underflow.
3868 ///
3869 /// # Worst-case complexity
3870 /// $T(n) = O(n^{3/2} \log n \log\log n)$
3871 ///
3872 /// $M(n) = O(n (\log n)^2)$
3873 ///
3874 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
3875 /// other.significant_bits())`.
3876 ///
3877 /// # Examples
3878 /// ```
3879 /// use malachite_float::Float;
3880 /// use malachite_nz::integer::Integer;
3881 /// use std::cmp::Ordering::*;
3882 ///
3883 /// let mut x = Float::from(3);
3884 /// let o = x.pow_integer_prec_assign_ref(&Integer::from(5), 20);
3885 /// assert_eq!(x.to_string(), "243.00000");
3886 /// assert_eq!(o, Equal);
3887 /// ```
3888 #[inline]
3889 pub fn pow_integer_prec_assign_ref(&mut self, other: &Integer, prec: u64) -> Ordering {
3890 self.pow_prec_assign(integer_to_exact_float(other.clone()), prec)
3891 }
3892
3893 /// Raises a [`Float`] to the power of an [`Integer`] in place, taking the [`Integer`] by value.
3894 ///
3895 /// See the [`Float::pow_integer_prec_round_ref_ref`] documentation for information on special
3896 /// cases, overflow, and underflow.
3897 ///
3898 /// # Worst-case complexity
3899 /// $T(n) = O(n^{3/2} \log n \log\log n)$
3900 ///
3901 /// $M(n) = O(n (\log n)^2)$
3902 ///
3903 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
3904 /// other.significant_bits())`.
3905 ///
3906 /// # Panics
3907 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the base's
3908 /// precision.
3909 ///
3910 /// # Examples
3911 /// ```
3912 /// use malachite_base::rounding_modes::RoundingMode::*;
3913 /// use malachite_float::Float;
3914 /// use malachite_nz::integer::Integer;
3915 /// use std::cmp::Ordering::*;
3916 ///
3917 /// let mut x = Float::from(3);
3918 /// let o = x.pow_integer_round_assign(Integer::from(5), Floor);
3919 /// assert_eq!(x.to_string(), "1.9e2");
3920 /// assert_eq!(o, Less);
3921 /// ```
3922 pub fn pow_integer_round_assign(&mut self, other: Integer, rm: RoundingMode) -> Ordering {
3923 let prec = self.significant_bits();
3924 self.pow_prec_round_assign(integer_to_exact_float(other), prec, rm)
3925 }
3926
3927 /// Raises a [`Float`] to the power of an [`Integer`] in place, taking the [`Integer`] by
3928 /// reference.
3929 ///
3930 /// See the [`Float::pow_integer_prec_round_ref_ref`] documentation for information on special
3931 /// cases, overflow, and underflow.
3932 ///
3933 /// # Worst-case complexity
3934 /// $T(n) = O(n^{3/2} \log n \log\log n)$
3935 ///
3936 /// $M(n) = O(n (\log n)^2)$
3937 ///
3938 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
3939 /// other.significant_bits())`.
3940 ///
3941 /// # Panics
3942 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the base's
3943 /// precision.
3944 ///
3945 /// # Examples
3946 /// ```
3947 /// use malachite_base::rounding_modes::RoundingMode::*;
3948 /// use malachite_float::Float;
3949 /// use malachite_nz::integer::Integer;
3950 /// use std::cmp::Ordering::*;
3951 ///
3952 /// let mut x = Float::from(3);
3953 /// let o = x.pow_integer_round_assign_ref(&Integer::from(5), Floor);
3954 /// assert_eq!(x.to_string(), "1.9e2");
3955 /// assert_eq!(o, Less);
3956 /// ```
3957 pub fn pow_integer_round_assign_ref(&mut self, other: &Integer, rm: RoundingMode) -> Ordering {
3958 let prec = self.significant_bits();
3959 self.pow_prec_round_assign(integer_to_exact_float(other.clone()), prec, rm)
3960 }
3961}
3962
3963impl Pow<Integer> for Float {
3964 type Output = Self;
3965
3966 /// Raises a [`Float`] to the power of an [`Integer`], rounding the result to the nearest value.
3967 /// Both are taken by value.
3968 ///
3969 /// The output precision is the precision of the base. If the power is equidistant from two
3970 /// [`Float`]s with that precision, the [`Float`] with fewer 1s in its binary expansion is
3971 /// chosen. See [`RoundingMode`] for a description of the `Nearest` rounding mode.
3972 ///
3973 /// $$
3974 /// f(x,n) = x^n+\varepsilon.
3975 /// $$
3976 /// - If $x^n$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
3977 /// - If $x^n$ is finite and nonzero, then $|\varepsilon| \leq 2^{\lfloor\log_2
3978 /// |x^n|\rfloor-p}$, where $p$ is the precision of the base.
3979 ///
3980 /// See the [`Float::pow_integer_prec_round_ref_ref`] documentation for information on special
3981 /// cases, overflow, and underflow.
3982 ///
3983 /// If you want to specify an output precision, consider using [`Float::pow_integer_prec`]
3984 /// instead. If you want to specify the output precision and the rounding mode, consider using
3985 /// [`Float::pow_integer_prec_round`] instead.
3986 ///
3987 /// # Worst-case complexity
3988 /// $T(n) = O(n^{3/2} \log n \log\log n)$
3989 ///
3990 /// $M(n) = O(n (\log n)^2)$
3991 ///
3992 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
3993 /// other.significant_bits())`.
3994 ///
3995 /// # Examples
3996 /// ```
3997 /// use malachite_base::num::arithmetic::traits::Pow;
3998 /// use malachite_float::Float;
3999 /// use malachite_nz::integer::Integer;
4000 ///
4001 /// assert_eq!(Float::from(2).pow(Integer::from(10)).to_string(), "1.0e3");
4002 /// assert_eq!(Float::from(2).pow(Integer::from(-3)).to_string(), "0.12");
4003 /// ```
4004 #[inline]
4005 fn pow(self, other: Integer) -> Self {
4006 let prec = self.significant_bits();
4007 self.pow_integer_prec(other, prec).0
4008 }
4009}
4010
4011impl Pow<&Integer> for Float {
4012 type Output = Self;
4013
4014 /// Raises a [`Float`] to the power of an [`Integer`], rounding the result to the nearest value.
4015 /// The [`Float`] is taken by value and the [`Integer`] by reference.
4016 ///
4017 /// The output precision is the precision of the base. If the power is equidistant from two
4018 /// [`Float`]s with that precision, the [`Float`] with fewer 1s in its binary expansion is
4019 /// chosen. See [`RoundingMode`] for a description of the `Nearest` rounding mode.
4020 ///
4021 /// $$
4022 /// f(x,n) = x^n+\varepsilon.
4023 /// $$
4024 /// - If $x^n$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
4025 /// - If $x^n$ is finite and nonzero, then $|\varepsilon| \leq 2^{\lfloor\log_2
4026 /// |x^n|\rfloor-p}$, where $p$ is the precision of the base.
4027 ///
4028 /// See the [`Float::pow_integer_prec_round_ref_ref`] documentation for information on special
4029 /// cases, overflow, and underflow.
4030 ///
4031 /// If you want to specify an output precision, consider using [`Float::pow_integer_prec`]
4032 /// instead. If you want to specify the output precision and the rounding mode, consider using
4033 /// [`Float::pow_integer_prec_round`] instead.
4034 ///
4035 /// # Worst-case complexity
4036 /// $T(n) = O(n^{3/2} \log n \log\log n)$
4037 ///
4038 /// $M(n) = O(n (\log n)^2)$
4039 ///
4040 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
4041 /// other.significant_bits())`.
4042 ///
4043 /// # Examples
4044 /// ```
4045 /// use malachite_base::num::arithmetic::traits::Pow;
4046 /// use malachite_float::Float;
4047 /// use malachite_nz::integer::Integer;
4048 ///
4049 /// assert_eq!(Float::from(2).pow(&Integer::from(10)).to_string(), "1.0e3");
4050 /// assert_eq!(Float::from(2).pow(&Integer::from(-3)).to_string(), "0.12");
4051 /// ```
4052 #[inline]
4053 fn pow(self, other: &Integer) -> Self {
4054 let prec = self.significant_bits();
4055 self.pow_integer_prec_val_ref(other, prec).0
4056 }
4057}
4058
4059impl Pow<Integer> for &Float {
4060 type Output = Float;
4061
4062 /// Raises a [`Float`] to the power of an [`Integer`], rounding the result to the nearest value.
4063 /// The [`Float`] is taken by reference and the [`Integer`] by value.
4064 ///
4065 /// The output precision is the precision of the base. If the power is equidistant from two
4066 /// [`Float`]s with that precision, the [`Float`] with fewer 1s in its binary expansion is
4067 /// chosen. See [`RoundingMode`] for a description of the `Nearest` rounding mode.
4068 ///
4069 /// $$
4070 /// f(x,n) = x^n+\varepsilon.
4071 /// $$
4072 /// - If $x^n$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
4073 /// - If $x^n$ is finite and nonzero, then $|\varepsilon| \leq 2^{\lfloor\log_2
4074 /// |x^n|\rfloor-p}$, where $p$ is the precision of the base.
4075 ///
4076 /// See the [`Float::pow_integer_prec_round_ref_ref`] documentation for information on special
4077 /// cases, overflow, and underflow.
4078 ///
4079 /// If you want to specify an output precision, consider using [`Float::pow_integer_prec`]
4080 /// instead. If you want to specify the output precision and the rounding mode, consider using
4081 /// [`Float::pow_integer_prec_round`] instead.
4082 ///
4083 /// # Worst-case complexity
4084 /// $T(n) = O(n^{3/2} \log n \log\log n)$
4085 ///
4086 /// $M(n) = O(n (\log n)^2)$
4087 ///
4088 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
4089 /// other.significant_bits())`.
4090 ///
4091 /// # Examples
4092 /// ```
4093 /// use malachite_base::num::arithmetic::traits::Pow;
4094 /// use malachite_float::Float;
4095 /// use malachite_nz::integer::Integer;
4096 ///
4097 /// assert_eq!(
4098 /// (&Float::from(2)).pow(Integer::from(10)).to_string(),
4099 /// "1.0e3"
4100 /// );
4101 /// assert_eq!((&Float::from(2)).pow(Integer::from(-3)).to_string(), "0.12");
4102 /// ```
4103 #[inline]
4104 fn pow(self, other: Integer) -> Float {
4105 let prec = self.significant_bits();
4106 self.pow_integer_prec_ref_val(other, prec).0
4107 }
4108}
4109
4110impl Pow<&Integer> for &Float {
4111 type Output = Float;
4112
4113 /// Raises a [`Float`] to the power of an [`Integer`], rounding the result to the nearest value.
4114 /// Both are taken by reference.
4115 ///
4116 /// The output precision is the precision of the base. If the power is equidistant from two
4117 /// [`Float`]s with that precision, the [`Float`] with fewer 1s in its binary expansion is
4118 /// chosen. See [`RoundingMode`] for a description of the `Nearest` rounding mode.
4119 ///
4120 /// $$
4121 /// f(x,n) = x^n+\varepsilon.
4122 /// $$
4123 /// - If $x^n$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
4124 /// - If $x^n$ is finite and nonzero, then $|\varepsilon| \leq 2^{\lfloor\log_2
4125 /// |x^n|\rfloor-p}$, where $p$ is the precision of the base.
4126 ///
4127 /// See the [`Float::pow_integer_prec_round_ref_ref`] documentation for information on special
4128 /// cases, overflow, and underflow.
4129 ///
4130 /// If you want to specify an output precision, consider using [`Float::pow_integer_prec`]
4131 /// instead. If you want to specify the output precision and the rounding mode, consider using
4132 /// [`Float::pow_integer_prec_round`] instead.
4133 ///
4134 /// # Worst-case complexity
4135 /// $T(n) = O(n^{3/2} \log n \log\log n)$
4136 ///
4137 /// $M(n) = O(n (\log n)^2)$
4138 ///
4139 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
4140 /// other.significant_bits())`.
4141 ///
4142 /// # Examples
4143 /// ```
4144 /// use malachite_base::num::arithmetic::traits::Pow;
4145 /// use malachite_float::Float;
4146 /// use malachite_nz::integer::Integer;
4147 ///
4148 /// assert_eq!(
4149 /// (&Float::from(2)).pow(&Integer::from(10)).to_string(),
4150 /// "1.0e3"
4151 /// );
4152 /// assert_eq!(
4153 /// (&Float::from(2)).pow(&Integer::from(-3)).to_string(),
4154 /// "0.12"
4155 /// );
4156 /// ```
4157 #[inline]
4158 fn pow(self, other: &Integer) -> Float {
4159 let prec = self.significant_bits();
4160 self.pow_integer_prec_ref_ref(other, prec).0
4161 }
4162}
4163
4164impl PowAssign<Integer> for Float {
4165 /// Raises a [`Float`] to the power of an [`Integer`] in place, taking the [`Integer`] by value,
4166 /// and rounding the result to the nearest value.
4167 ///
4168 /// The output precision is the precision of the base. See the
4169 /// [`Float::pow_integer_prec_round_ref_ref`] documentation for information on special cases,
4170 /// overflow, and underflow.
4171 ///
4172 /// # Worst-case complexity
4173 /// $T(n) = O(n^{3/2} \log n \log\log n)$
4174 ///
4175 /// $M(n) = O(n (\log n)^2)$
4176 ///
4177 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
4178 /// other.significant_bits())`.
4179 ///
4180 /// # Examples
4181 /// ```
4182 /// use malachite_base::num::arithmetic::traits::PowAssign;
4183 /// use malachite_float::Float;
4184 /// use malachite_nz::integer::Integer;
4185 ///
4186 /// let mut x = Float::from(2);
4187 /// x.pow_assign(Integer::from(10));
4188 /// assert_eq!(x.to_string(), "1.0e3");
4189 /// ```
4190 #[inline]
4191 fn pow_assign(&mut self, other: Integer) {
4192 let prec = self.significant_bits();
4193 self.pow_integer_prec_assign(other, prec);
4194 }
4195}
4196
4197impl PowAssign<&Integer> for Float {
4198 /// Raises a [`Float`] to the power of an [`Integer`] in place, taking the [`Integer`] by
4199 /// reference, and rounding the result to the nearest value.
4200 ///
4201 /// The output precision is the precision of the base. See the
4202 /// [`Float::pow_integer_prec_round_ref_ref`] documentation for information on special cases,
4203 /// overflow, and underflow.
4204 ///
4205 /// # Worst-case complexity
4206 /// $T(n) = O(n^{3/2} \log n \log\log n)$
4207 ///
4208 /// $M(n) = O(n (\log n)^2)$
4209 ///
4210 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
4211 /// other.significant_bits())`.
4212 ///
4213 /// # Examples
4214 /// ```
4215 /// use malachite_base::num::arithmetic::traits::PowAssign;
4216 /// use malachite_float::Float;
4217 /// use malachite_nz::integer::Integer;
4218 ///
4219 /// let mut x = Float::from(2);
4220 /// x.pow_assign(&Integer::from(10));
4221 /// assert_eq!(x.to_string(), "1.0e3");
4222 /// ```
4223 #[inline]
4224 fn pow_assign(&mut self, other: &Integer) {
4225 let prec = self.significant_bits();
4226 self.pow_integer_prec_assign_ref(other, prec);
4227 }
4228}
4229
4230impl Float {
4231 /// Raises a [`Float`] to the power of a [`u64`], rounding the result to the specified precision
4232 /// and with the specified rounding mode. The [`Float`] is taken by value. An [`Ordering`] is
4233 /// also returned, indicating whether the rounded power is less than, equal to, or greater than
4234 /// the exact power. Although `NaN`s are not comparable to any [`Float`], whenever this function
4235 /// returns a `NaN` it also returns `Equal`.
4236 ///
4237 /// See [`RoundingMode`] for a description of the possible rounding modes.
4238 ///
4239 /// $$
4240 /// f(x,n,p,m) = x^n+\varepsilon.
4241 /// $$
4242 /// - If $x^n$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
4243 /// - If $x^n$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
4244 /// 2^{\lfloor\log_2 |x^n|\rfloor-p+1}$.
4245 /// - If $x^n$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
4246 /// 2^{\lfloor\log_2 |x^n|\rfloor-p}$.
4247 ///
4248 /// Special cases:
4249 /// - $f(x,0)=1.0$ for any $x$, even `NaN`
4250 /// - $f(1.0,n)=1.0$
4251 /// - $f(\text{NaN},n)=\text{NaN}$ if $n \neq 0$
4252 /// - $f(-1.0,n)=1.0$ if $n$ is even, and $-1.0$ if $n$ is odd
4253 /// - $f(\infty,n)=\infty$ if $n>0$
4254 /// - $f(-\infty,n)=\infty$ if $n$ is positive and even, and $-\infty$ if $n$ is odd
4255 /// - $f(0.0,n)=0.0$ if $n>0$
4256 /// - $f(-0.0,n)=0.0$ if $n$ is positive and even, and $-0.0$ if $n$ is odd
4257 ///
4258 /// Overflow and underflow:
4259 /// - If $f(x,n,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
4260 /// returned instead.
4261 /// - If $f(x,n,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor` or `Down`, $(1-(1/2)^p)2^{2^{30}-1}$
4262 /// is returned instead.
4263 /// - If $0<f(x,n,p,m)<2^{-2^{30}}$ and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
4264 /// - If $0<f(x,n,p,m)<2^{-2^{30}}$ and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
4265 /// instead.
4266 /// - If $0<f(x,n,p,m)\leq2^{-2^{30}-1}$ and $m$ is `Nearest`, $0.0$ is returned instead.
4267 /// - If $2^{-2^{30}-1}<f(x,n,p,m)<2^{-2^{30}}$ and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
4268 /// instead.
4269 /// - Negative results (from negative $x$ and odd $n$) mirror the bullets above, with the
4270 /// rounding directions reflected.
4271 ///
4272 /// # Worst-case complexity
4273 /// $T(n) = O(n \log n \log\log n)$
4274 ///
4275 /// $M(n) = O(n \log n)$
4276 ///
4277 /// where $T$ is time, $M$ is additional memory, and $n$ is `prec`.
4278 ///
4279 /// # Panics
4280 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the given
4281 /// precision.
4282 ///
4283 /// # Examples
4284 /// ```
4285 /// use malachite_base::rounding_modes::RoundingMode::*;
4286 /// use malachite_float::Float;
4287 /// use std::cmp::Ordering::*;
4288 ///
4289 /// let (p, o) = Float::from(3).pow_u_prec_round(5, 20, Floor);
4290 /// assert_eq!(p.to_string(), "243.00000");
4291 /// assert_eq!(o, Equal);
4292 ///
4293 /// let (p, o) = Float::from(3).pow_u_prec_round(5, 2, Ceiling);
4294 /// assert_eq!(p.to_string(), "2.6e2");
4295 /// assert_eq!(o, Greater);
4296 /// ```
4297 #[inline]
4298 pub fn pow_u_prec_round(self, n: u64, prec: u64, rm: RoundingMode) -> (Self, Ordering) {
4299 pow_u(self, n, prec, rm)
4300 }
4301
4302 /// Raises a [`Float`] to the power of a [`u64`], rounding the result to the specified precision
4303 /// and with the specified rounding mode. The [`Float`] is taken by reference. An [`Ordering`]
4304 /// is also returned, indicating whether the rounded power is less than, equal to, or greater
4305 /// than the exact power. Although `NaN`s are not comparable to any [`Float`], whenever this
4306 /// function returns a `NaN` it also returns `Equal`.
4307 ///
4308 /// See [`RoundingMode`] for a description of the possible rounding modes.
4309 ///
4310 /// $$
4311 /// f(x,n,p,m) = x^n+\varepsilon.
4312 /// $$
4313 /// - If $x^n$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
4314 /// - If $x^n$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
4315 /// 2^{\lfloor\log_2 |x^n|\rfloor-p+1}$.
4316 /// - If $x^n$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
4317 /// 2^{\lfloor\log_2 |x^n|\rfloor-p}$.
4318 ///
4319 /// See the [`Float::pow_u_prec_round`] documentation for information on special cases,
4320 /// overflow, and underflow.
4321 ///
4322 /// # Worst-case complexity
4323 /// $T(n) = O(n \log n \log\log n)$
4324 ///
4325 /// $M(n) = O(n \log n)$
4326 ///
4327 /// where $T$ is time, $M$ is additional memory, and $n$ is `prec`.
4328 ///
4329 /// # Panics
4330 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the given
4331 /// precision.
4332 ///
4333 /// # Examples
4334 /// ```
4335 /// use malachite_base::rounding_modes::RoundingMode::*;
4336 /// use malachite_float::Float;
4337 /// use std::cmp::Ordering::*;
4338 ///
4339 /// let (p, o) = (&Float::from(3)).pow_u_prec_round_ref(5, 20, Floor);
4340 /// assert_eq!(p.to_string(), "243.00000");
4341 /// assert_eq!(o, Equal);
4342 ///
4343 /// let (p, o) = (&Float::from(3)).pow_u_prec_round_ref(5, 2, Ceiling);
4344 /// assert_eq!(p.to_string(), "2.6e2");
4345 /// assert_eq!(o, Greater);
4346 /// ```
4347 #[inline]
4348 pub fn pow_u_prec_round_ref(&self, n: u64, prec: u64, rm: RoundingMode) -> (Self, Ordering) {
4349 pow_u_ref(self, n, prec, rm)
4350 }
4351
4352 /// Raises a [`Float`] to the power of a [`u64`], rounding the result to the specified precision
4353 /// and to the nearest value. The [`Float`] is taken by value. An [`Ordering`] is also returned,
4354 /// indicating whether the rounded power is less than, equal to, or greater than the exact
4355 /// power. Although `NaN`s are not comparable to any [`Float`], whenever this function returns a
4356 /// `NaN` it also returns `Equal`.
4357 ///
4358 /// If the power is equidistant from two [`Float`]s with the specified precision, the [`Float`]
4359 /// with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of
4360 /// the `Nearest` rounding mode.
4361 ///
4362 /// $$
4363 /// f(x,n,p) = x^n+\varepsilon.
4364 /// $$
4365 /// - If $x^n$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
4366 /// - If $x^n$ is finite and nonzero, then $|\varepsilon| \leq 2^{\lfloor\log_2
4367 /// |x^n|\rfloor-p}$.
4368 ///
4369 /// See the [`Float::pow_u_prec_round`] documentation for information on special cases,
4370 /// overflow, and underflow.
4371 ///
4372 /// If you want to use a rounding mode other than `Nearest`, consider using
4373 /// [`Float::pow_u_prec_round`] instead.
4374 ///
4375 /// # Worst-case complexity
4376 /// $T(n) = O(n \log n \log\log n)$
4377 ///
4378 /// $M(n) = O(n \log n)$
4379 ///
4380 /// where $T$ is time, $M$ is additional memory, and $n$ is `prec`.
4381 ///
4382 /// # Examples
4383 /// ```
4384 /// use malachite_float::Float;
4385 /// use std::cmp::Ordering::*;
4386 ///
4387 /// let (p, o) = Float::from(3).pow_u_prec(5, 20);
4388 /// assert_eq!(p.to_string(), "243.00000");
4389 /// assert_eq!(o, Equal);
4390 ///
4391 /// let (p, o) = Float::from(3).pow_u_prec(5, 2);
4392 /// assert_eq!(p.to_string(), "2.6e2");
4393 /// assert_eq!(o, Greater);
4394 /// ```
4395 #[inline]
4396 pub fn pow_u_prec(self, n: u64, prec: u64) -> (Self, Ordering) {
4397 pow_u(self, n, prec, Nearest)
4398 }
4399
4400 /// Raises a [`Float`] to the power of a [`u64`], rounding the result to the specified precision
4401 /// and to the nearest value. The [`Float`] is taken by reference. An [`Ordering`] is also
4402 /// returned, indicating whether the rounded power is less than, equal to, or greater than the
4403 /// exact power. Although `NaN`s are not comparable to any [`Float`], whenever this function
4404 /// returns a `NaN` it also returns `Equal`.
4405 ///
4406 /// If the power is equidistant from two [`Float`]s with the specified precision, the [`Float`]
4407 /// with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of
4408 /// the `Nearest` rounding mode.
4409 ///
4410 /// $$
4411 /// f(x,n,p) = x^n+\varepsilon.
4412 /// $$
4413 /// - If $x^n$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
4414 /// - If $x^n$ is finite and nonzero, then $|\varepsilon| \leq 2^{\lfloor\log_2
4415 /// |x^n|\rfloor-p}$.
4416 ///
4417 /// See the [`Float::pow_u_prec_round`] documentation for information on special cases,
4418 /// overflow, and underflow.
4419 ///
4420 /// If you want to use a rounding mode other than `Nearest`, consider using
4421 /// [`Float::pow_u_prec_round_ref`] instead.
4422 ///
4423 /// # Worst-case complexity
4424 /// $T(n) = O(n \log n \log\log n)$
4425 ///
4426 /// $M(n) = O(n \log n)$
4427 ///
4428 /// where $T$ is time, $M$ is additional memory, and $n$ is `prec`.
4429 ///
4430 /// # Examples
4431 /// ```
4432 /// use malachite_float::Float;
4433 /// use std::cmp::Ordering::*;
4434 ///
4435 /// let (p, o) = (&Float::from(3)).pow_u_prec_ref(5, 20);
4436 /// assert_eq!(p.to_string(), "243.00000");
4437 /// assert_eq!(o, Equal);
4438 ///
4439 /// let (p, o) = (&Float::from(3)).pow_u_prec_ref(5, 2);
4440 /// assert_eq!(p.to_string(), "2.6e2");
4441 /// assert_eq!(o, Greater);
4442 /// ```
4443 #[inline]
4444 pub fn pow_u_prec_ref(&self, n: u64, prec: u64) -> (Self, Ordering) {
4445 pow_u_ref(self, n, prec, Nearest)
4446 }
4447
4448 /// Raises a [`Float`] to the power of a [`u64`], rounding the result to the precision of the
4449 /// base and with the specified rounding mode. The [`Float`] is taken by value. An [`Ordering`]
4450 /// is also returned, indicating whether the rounded power is less than, equal to, or greater
4451 /// than the exact power. Although `NaN`s are not comparable to any [`Float`], whenever this
4452 /// function returns a `NaN` it also returns `Equal`.
4453 ///
4454 /// The output precision is the precision of `self`. See [`RoundingMode`] for a description of
4455 /// the possible rounding modes.
4456 ///
4457 /// $$
4458 /// f(x,n,p,m) = x^n+\varepsilon.
4459 /// $$
4460 /// - If $x^n$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
4461 /// - If $x^n$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
4462 /// 2^{\lfloor\log_2 |x^n|\rfloor-p+1}$.
4463 /// - If $x^n$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
4464 /// 2^{\lfloor\log_2 |x^n|\rfloor-p}$.
4465 ///
4466 /// See the [`Float::pow_u_prec_round`] documentation for information on special cases,
4467 /// overflow, and underflow.
4468 ///
4469 /// If you want to specify an output precision, consider using [`Float::pow_u_prec_round`]
4470 /// instead.
4471 ///
4472 /// # Worst-case complexity
4473 /// $T(n) = O(n \log n \log\log n)$
4474 ///
4475 /// $M(n) = O(n \log n)$
4476 ///
4477 /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
4478 ///
4479 /// # Panics
4480 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the base's
4481 /// precision.
4482 ///
4483 /// # Examples
4484 /// ```
4485 /// use malachite_base::rounding_modes::RoundingMode::*;
4486 /// use malachite_float::Float;
4487 /// use std::cmp::Ordering::*;
4488 ///
4489 /// let (p, o) = Float::from(3).pow_u_round(5, Floor);
4490 /// assert_eq!(p.to_string(), "1.9e2");
4491 /// assert_eq!(o, Less);
4492 ///
4493 /// let (p, o) = Float::from(3).pow_u_round(5, Ceiling);
4494 /// assert_eq!(p.to_string(), "2.6e2");
4495 /// assert_eq!(o, Greater);
4496 /// ```
4497 #[inline]
4498 pub fn pow_u_round(self, n: u64, rm: RoundingMode) -> (Self, Ordering) {
4499 let prec = self.significant_bits();
4500 pow_u(self, n, prec, rm)
4501 }
4502
4503 /// Raises a [`Float`] to the power of a [`u64`], rounding the result to the precision of the
4504 /// base and with the specified rounding mode. The [`Float`] is taken by reference. An
4505 /// [`Ordering`] is also returned, indicating whether the rounded power is less than, equal to,
4506 /// or greater than the exact power. Although `NaN`s are not comparable to any [`Float`],
4507 /// whenever this function returns a `NaN` it also returns `Equal`.
4508 ///
4509 /// The output precision is the precision of `self`. See [`RoundingMode`] for a description of
4510 /// the possible rounding modes.
4511 ///
4512 /// $$
4513 /// f(x,n,p,m) = x^n+\varepsilon.
4514 /// $$
4515 /// - If $x^n$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
4516 /// - If $x^n$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
4517 /// 2^{\lfloor\log_2 |x^n|\rfloor-p+1}$.
4518 /// - If $x^n$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
4519 /// 2^{\lfloor\log_2 |x^n|\rfloor-p}$.
4520 ///
4521 /// See the [`Float::pow_u_prec_round`] documentation for information on special cases,
4522 /// overflow, and underflow.
4523 ///
4524 /// If you want to specify an output precision, consider using [`Float::pow_u_prec_round_ref`]
4525 /// instead.
4526 ///
4527 /// # Worst-case complexity
4528 /// $T(n) = O(n \log n \log\log n)$
4529 ///
4530 /// $M(n) = O(n \log n)$
4531 ///
4532 /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
4533 ///
4534 /// # Panics
4535 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the base's
4536 /// precision.
4537 ///
4538 /// # Examples
4539 /// ```
4540 /// use malachite_base::rounding_modes::RoundingMode::*;
4541 /// use malachite_float::Float;
4542 /// use std::cmp::Ordering::*;
4543 ///
4544 /// let (p, o) = (&Float::from(3)).pow_u_round_ref(5, Floor);
4545 /// assert_eq!(p.to_string(), "1.9e2");
4546 /// assert_eq!(o, Less);
4547 ///
4548 /// let (p, o) = (&Float::from(3)).pow_u_round_ref(5, Ceiling);
4549 /// assert_eq!(p.to_string(), "2.6e2");
4550 /// assert_eq!(o, Greater);
4551 /// ```
4552 #[inline]
4553 pub fn pow_u_round_ref(&self, n: u64, rm: RoundingMode) -> (Self, Ordering) {
4554 pow_u_ref(self, n, self.significant_bits(), rm)
4555 }
4556
4557 /// Raises a [`Float`] to the power of a [`u64`] in place, rounding the result to the specified
4558 /// precision and with the specified rounding mode.
4559 ///
4560 /// See the [`Float::pow_u_prec_round`] documentation for information on special cases,
4561 /// overflow, and underflow.
4562 ///
4563 /// # Worst-case complexity
4564 /// $T(n) = O(n \log n \log\log n)$
4565 ///
4566 /// $M(n) = O(n \log n)$
4567 ///
4568 /// where $T$ is time, $M$ is additional memory, and $n$ is `prec`.
4569 ///
4570 /// # Panics
4571 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the given
4572 /// precision.
4573 ///
4574 /// # Examples
4575 /// ```
4576 /// use malachite_base::rounding_modes::RoundingMode::*;
4577 /// use malachite_float::Float;
4578 /// use std::cmp::Ordering::*;
4579 ///
4580 /// let mut x = Float::from(3);
4581 /// let o = x.pow_u_prec_round_assign(5, 20, Floor);
4582 /// assert_eq!(x.to_string(), "243.00000");
4583 /// assert_eq!(o, Equal);
4584 /// ```
4585 pub fn pow_u_prec_round_assign(&mut self, n: u64, prec: u64, rm: RoundingMode) -> Ordering {
4586 let mut x = Self::ZERO;
4587 swap(self, &mut x);
4588 let (result, o) = pow_u(x, n, prec, rm);
4589 *self = result;
4590 o
4591 }
4592
4593 /// Raises a [`Float`] to the power of a [`u64`] in place, rounding the result to the specified
4594 /// precision and to the nearest value.
4595 ///
4596 /// See the [`Float::pow_u_prec_round`] documentation for information on special cases,
4597 /// overflow, and underflow.
4598 ///
4599 /// # Worst-case complexity
4600 /// $T(n) = O(n \log n \log\log n)$
4601 ///
4602 /// $M(n) = O(n \log n)$
4603 ///
4604 /// where $T$ is time, $M$ is additional memory, and $n$ is `prec`.
4605 ///
4606 /// # Examples
4607 /// ```
4608 /// use malachite_float::Float;
4609 /// use std::cmp::Ordering::*;
4610 ///
4611 /// let mut x = Float::from(3);
4612 /// let o = x.pow_u_prec_assign(5, 20);
4613 /// assert_eq!(x.to_string(), "243.00000");
4614 /// assert_eq!(o, Equal);
4615 /// ```
4616 #[inline]
4617 pub fn pow_u_prec_assign(&mut self, n: u64, prec: u64) -> Ordering {
4618 self.pow_u_prec_round_assign(n, prec, Nearest)
4619 }
4620
4621 /// Raises a [`Float`] to the power of a [`u64`] in place, rounding the result to the precision
4622 /// of the base and with the specified rounding mode.
4623 ///
4624 /// See the [`Float::pow_u_prec_round`] documentation for information on special cases,
4625 /// overflow, and underflow.
4626 ///
4627 /// # Worst-case complexity
4628 /// $T(n) = O(n \log n \log\log n)$
4629 ///
4630 /// $M(n) = O(n \log n)$
4631 ///
4632 /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
4633 ///
4634 /// # Panics
4635 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the base's
4636 /// precision.
4637 ///
4638 /// # Examples
4639 /// ```
4640 /// use malachite_base::rounding_modes::RoundingMode::*;
4641 /// use malachite_float::Float;
4642 /// use std::cmp::Ordering::*;
4643 ///
4644 /// let mut x = Float::from(3);
4645 /// let o = x.pow_u_round_assign(5, Floor);
4646 /// assert_eq!(x.to_string(), "1.9e2");
4647 /// assert_eq!(o, Less);
4648 /// ```
4649 #[inline]
4650 pub fn pow_u_round_assign(&mut self, n: u64, rm: RoundingMode) -> Ordering {
4651 let prec = self.significant_bits();
4652 self.pow_u_prec_round_assign(n, prec, rm)
4653 }
4654}
4655
4656impl Pow<u64> for Float {
4657 type Output = Self;
4658
4659 /// Raises a [`Float`] to the power of a [`i64`], rounding the result to the nearest value at
4660 /// the precision of the base. The [`Float`] is taken by value.
4661 ///
4662 /// If the power is equidistant from two [`Float`]s with that precision, the [`Float`] with
4663 /// fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of the
4664 /// `Nearest` rounding mode.
4665 ///
4666 /// $$
4667 /// f(x,n) = x^n+\varepsilon.
4668 /// $$
4669 /// - If $x^n$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
4670 /// - If $x^n$ is finite and nonzero, then $|\varepsilon| \leq 2^{\lfloor\log_2
4671 /// |x^n|\rfloor-p}$, where $p$ is the precision of the base.
4672 ///
4673 /// See the [`Float::pow_s_prec_round`] documentation for information on special cases,
4674 /// overflow, and underflow.
4675 ///
4676 /// If you want to specify an output precision, consider using [`Float::pow_s_prec`] instead. If
4677 /// you want to specify the output precision and the rounding mode, consider using
4678 /// [`Float::pow_s_prec_round`] instead.
4679 ///
4680 /// # Worst-case complexity
4681 /// $T(n) = O(n \log n \log\log n)$
4682 ///
4683 /// $M(n) = O(n \log n)$
4684 ///
4685 /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
4686 ///
4687 /// # Examples
4688 /// ```
4689 /// use malachite_base::num::arithmetic::traits::Pow;
4690 /// use malachite_float::Float;
4691 ///
4692 /// assert_eq!(Float::from(2).pow(10i64).to_string(), "1.0e3");
4693 /// assert_eq!(Float::from(0.5).pow(-1i64).to_string(), "2.0");
4694 /// ```
4695 #[inline]
4696 fn pow(self, n: u64) -> Self {
4697 let prec = self.significant_bits();
4698 pow_u(self, n, prec, Nearest).0
4699 }
4700}
4701
4702impl Pow<u64> for &Float {
4703 type Output = Float;
4704
4705 /// Raises a [`Float`] to the power of a [`i64`], rounding the result to the nearest value at
4706 /// the precision of the base. The [`Float`] is taken by reference.
4707 ///
4708 /// If the power is equidistant from two [`Float`]s with that precision, the [`Float`] with
4709 /// fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of the
4710 /// `Nearest` rounding mode.
4711 ///
4712 /// $$
4713 /// f(x,n) = x^n+\varepsilon.
4714 /// $$
4715 /// - If $x^n$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
4716 /// - If $x^n$ is finite and nonzero, then $|\varepsilon| \leq 2^{\lfloor\log_2
4717 /// |x^n|\rfloor-p}$, where $p$ is the precision of the base.
4718 ///
4719 /// See the [`Float::pow_s_prec_round`] documentation for information on special cases,
4720 /// overflow, and underflow.
4721 ///
4722 /// If you want to specify an output precision, consider using [`Float::pow_s_prec`] instead. If
4723 /// you want to specify the output precision and the rounding mode, consider using
4724 /// [`Float::pow_s_prec_round`] instead.
4725 ///
4726 /// # Worst-case complexity
4727 /// $T(n) = O(n \log n \log\log n)$
4728 ///
4729 /// $M(n) = O(n \log n)$
4730 ///
4731 /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
4732 ///
4733 /// # Examples
4734 /// ```
4735 /// use malachite_base::num::arithmetic::traits::Pow;
4736 /// use malachite_float::Float;
4737 ///
4738 /// assert_eq!((&Float::from(2)).pow(10i64).to_string(), "1.0e3");
4739 /// assert_eq!(Float::from(0.5).pow(-1i64).to_string(), "2.0");
4740 /// ```
4741 #[inline]
4742 fn pow(self, n: u64) -> Float {
4743 pow_u_ref(self, n, self.significant_bits(), Nearest).0
4744 }
4745}
4746
4747impl PowAssign<u64> for Float {
4748 /// Raises a [`Float`] to the power of a [`i64`] in place, rounding the result to the nearest
4749 /// value at the precision of the base.
4750 ///
4751 /// See the [`Float::pow_s_prec_round`] documentation for information on special cases,
4752 /// overflow, and underflow.
4753 ///
4754 /// # Worst-case complexity
4755 /// $T(n) = O(n \log n \log\log n)$
4756 ///
4757 /// $M(n) = O(n \log n)$
4758 ///
4759 /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
4760 ///
4761 /// # Examples
4762 /// ```
4763 /// use malachite_base::num::arithmetic::traits::PowAssign;
4764 /// use malachite_float::Float;
4765 ///
4766 /// let mut x = Float::from(2);
4767 /// x.pow_assign(10i64);
4768 /// assert_eq!(x.to_string(), "1.0e3");
4769 /// ```
4770 #[inline]
4771 fn pow_assign(&mut self, n: u64) {
4772 let prec = self.significant_bits();
4773 self.pow_u_prec_assign(n, prec);
4774 }
4775}
4776
4777impl Float {
4778 /// Raises a [`Float`] to the power of a [`i64`], rounding the result to the specified precision
4779 /// and with the specified rounding mode. The [`Float`] is taken by value. An [`Ordering`] is
4780 /// also returned, indicating whether the rounded power is less than, equal to, or greater than
4781 /// the exact power. Although `NaN`s are not comparable to any [`Float`], whenever this function
4782 /// returns a `NaN` it also returns `Equal`.
4783 ///
4784 /// See [`RoundingMode`] for a description of the possible rounding modes.
4785 ///
4786 /// $$
4787 /// f(x,n,p,m) = x^n+\varepsilon.
4788 /// $$
4789 /// - If $x^n$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
4790 /// - If $x^n$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
4791 /// 2^{\lfloor\log_2 |x^n|\rfloor-p+1}$.
4792 /// - If $x^n$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
4793 /// 2^{\lfloor\log_2 |x^n|\rfloor-p}$.
4794 ///
4795 /// Special cases:
4796 /// - $f(x,0)=1.0$ for any $x$, even `NaN`
4797 /// - $f(1.0,n)=1.0$
4798 /// - $f(\text{NaN},n)=\text{NaN}$ if $n \neq 0$
4799 /// - $f(-1.0,n)=1.0$ if $n$ is even, and $-1.0$ if $n$ is odd
4800 /// - $f(\infty,n)=\infty$ if $n>0$, and $0.0$ if $n<0$
4801 /// - $f(-\infty,n)=\infty$ if $n$ is positive and even, $-\infty$ if $n$ is positive and odd,
4802 /// $0.0$ if $n$ is negative and even, and $-0.0$ if $n$ is negative and odd
4803 /// - $f(0.0,n)=0.0$ if $n>0$, and $\infty$ if $n<0$
4804 /// - $f(-0.0,n)=0.0$ if $n$ is positive and even, $-0.0$ if $n$ is positive and odd, $\infty$
4805 /// if $n$ is negative and even, and $-\infty$ if $n$ is negative and odd
4806 ///
4807 /// Overflow and underflow:
4808 /// - If $f(x,n,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
4809 /// returned instead.
4810 /// - If $f(x,n,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor` or `Down`, $(1-(1/2)^p)2^{2^{30}-1}$
4811 /// is returned instead.
4812 /// - If $0<f(x,n,p,m)<2^{-2^{30}}$ and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
4813 /// - If $0<f(x,n,p,m)<2^{-2^{30}}$ and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
4814 /// instead.
4815 /// - If $0<f(x,n,p,m)\leq2^{-2^{30}-1}$ and $m$ is `Nearest`, $0.0$ is returned instead.
4816 /// - If $2^{-2^{30}-1}<f(x,n,p,m)<2^{-2^{30}}$ and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
4817 /// instead.
4818 /// - Negative results (from negative $x$ and odd $n$) mirror the bullets above, with the
4819 /// rounding directions reflected.
4820 ///
4821 /// # Worst-case complexity
4822 /// $T(n) = O(n \log n \log\log n)$
4823 ///
4824 /// $M(n) = O(n \log n)$
4825 ///
4826 /// where $T$ is time, $M$ is additional memory, and $n$ is `prec`.
4827 ///
4828 /// # Panics
4829 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the given
4830 /// precision.
4831 ///
4832 /// # Examples
4833 /// ```
4834 /// use malachite_base::rounding_modes::RoundingMode::*;
4835 /// use malachite_float::Float;
4836 /// use std::cmp::Ordering::*;
4837 ///
4838 /// let (p, o) = Float::from(3).pow_s_prec_round(5, 20, Floor);
4839 /// assert_eq!(p.to_string(), "243.00000");
4840 /// assert_eq!(o, Equal);
4841 ///
4842 /// let (p, o) = Float::from(3).pow_s_prec_round(-2, 10, Ceiling);
4843 /// assert_eq!(p.to_string(), "0.11121");
4844 /// assert_eq!(o, Greater);
4845 /// ```
4846 #[inline]
4847 pub fn pow_s_prec_round(self, n: i64, prec: u64, rm: RoundingMode) -> (Self, Ordering) {
4848 pow_s(self, n, prec, rm)
4849 }
4850
4851 /// Raises a [`Float`] to the power of a [`i64`], rounding the result to the specified precision
4852 /// and with the specified rounding mode. The [`Float`] is taken by reference. An [`Ordering`]
4853 /// is also returned, indicating whether the rounded power is less than, equal to, or greater
4854 /// than the exact power. Although `NaN`s are not comparable to any [`Float`], whenever this
4855 /// function returns a `NaN` it also returns `Equal`.
4856 ///
4857 /// See [`RoundingMode`] for a description of the possible rounding modes.
4858 ///
4859 /// $$
4860 /// f(x,n,p,m) = x^n+\varepsilon.
4861 /// $$
4862 /// - If $x^n$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
4863 /// - If $x^n$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
4864 /// 2^{\lfloor\log_2 |x^n|\rfloor-p+1}$.
4865 /// - If $x^n$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
4866 /// 2^{\lfloor\log_2 |x^n|\rfloor-p}$.
4867 ///
4868 /// See the [`Float::pow_s_prec_round`] documentation for information on special cases,
4869 /// overflow, and underflow.
4870 ///
4871 /// # Worst-case complexity
4872 /// $T(n) = O(n \log n \log\log n)$
4873 ///
4874 /// $M(n) = O(n \log n)$
4875 ///
4876 /// where $T$ is time, $M$ is additional memory, and $n$ is `prec`.
4877 ///
4878 /// # Panics
4879 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the given
4880 /// precision.
4881 ///
4882 /// # Examples
4883 /// ```
4884 /// use malachite_base::rounding_modes::RoundingMode::*;
4885 /// use malachite_float::Float;
4886 /// use std::cmp::Ordering::*;
4887 ///
4888 /// let (p, o) = (&Float::from(3)).pow_s_prec_round_ref(5, 20, Floor);
4889 /// assert_eq!(p.to_string(), "243.00000");
4890 /// assert_eq!(o, Equal);
4891 ///
4892 /// let (p, o) = (&Float::from(3)).pow_s_prec_round_ref(-2, 10, Ceiling);
4893 /// assert_eq!(p.to_string(), "0.11121");
4894 /// assert_eq!(o, Greater);
4895 /// ```
4896 #[inline]
4897 pub fn pow_s_prec_round_ref(&self, n: i64, prec: u64, rm: RoundingMode) -> (Self, Ordering) {
4898 pow_s_ref(self, n, prec, rm)
4899 }
4900
4901 /// Raises a [`Float`] to the power of a [`i64`], rounding the result to the specified precision
4902 /// and to the nearest value. The [`Float`] is taken by value. An [`Ordering`] is also returned,
4903 /// indicating whether the rounded power is less than, equal to, or greater than the exact
4904 /// power. Although `NaN`s are not comparable to any [`Float`], whenever this function returns a
4905 /// `NaN` it also returns `Equal`.
4906 ///
4907 /// If the power is equidistant from two [`Float`]s with the specified precision, the [`Float`]
4908 /// with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of
4909 /// the `Nearest` rounding mode.
4910 ///
4911 /// $$
4912 /// f(x,n,p) = x^n+\varepsilon.
4913 /// $$
4914 /// - If $x^n$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
4915 /// - If $x^n$ is finite and nonzero, then $|\varepsilon| \leq 2^{\lfloor\log_2
4916 /// |x^n|\rfloor-p}$.
4917 ///
4918 /// See the [`Float::pow_s_prec_round`] documentation for information on special cases,
4919 /// overflow, and underflow.
4920 ///
4921 /// If you want to use a rounding mode other than `Nearest`, consider using
4922 /// [`Float::pow_s_prec_round`] instead.
4923 ///
4924 /// # Worst-case complexity
4925 /// $T(n) = O(n \log n \log\log n)$
4926 ///
4927 /// $M(n) = O(n \log n)$
4928 ///
4929 /// where $T$ is time, $M$ is additional memory, and $n$ is `prec`.
4930 ///
4931 /// # Examples
4932 /// ```
4933 /// use malachite_float::Float;
4934 /// use std::cmp::Ordering::*;
4935 ///
4936 /// let (p, o) = Float::from(3).pow_s_prec(5, 20);
4937 /// assert_eq!(p.to_string(), "243.00000");
4938 /// assert_eq!(o, Equal);
4939 ///
4940 /// let (p, o) = Float::from(3).pow_s_prec(-2, 10);
4941 /// assert_eq!(p.to_string(), "0.11108");
4942 /// assert_eq!(o, Less);
4943 /// ```
4944 #[inline]
4945 pub fn pow_s_prec(self, n: i64, prec: u64) -> (Self, Ordering) {
4946 pow_s(self, n, prec, Nearest)
4947 }
4948
4949 /// Raises a [`Float`] to the power of a [`i64`], rounding the result to the specified precision
4950 /// and to the nearest value. The [`Float`] is taken by reference. An [`Ordering`] is also
4951 /// returned, indicating whether the rounded power is less than, equal to, or greater than the
4952 /// exact power. Although `NaN`s are not comparable to any [`Float`], whenever this function
4953 /// returns a `NaN` it also returns `Equal`.
4954 ///
4955 /// If the power is equidistant from two [`Float`]s with the specified precision, the [`Float`]
4956 /// with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of
4957 /// the `Nearest` rounding mode.
4958 ///
4959 /// $$
4960 /// f(x,n,p) = x^n+\varepsilon.
4961 /// $$
4962 /// - If $x^n$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
4963 /// - If $x^n$ is finite and nonzero, then $|\varepsilon| \leq 2^{\lfloor\log_2
4964 /// |x^n|\rfloor-p}$.
4965 ///
4966 /// See the [`Float::pow_s_prec_round`] documentation for information on special cases,
4967 /// overflow, and underflow.
4968 ///
4969 /// If you want to use a rounding mode other than `Nearest`, consider using
4970 /// [`Float::pow_s_prec_round_ref`] instead.
4971 ///
4972 /// # Worst-case complexity
4973 /// $T(n) = O(n \log n \log\log n)$
4974 ///
4975 /// $M(n) = O(n \log n)$
4976 ///
4977 /// where $T$ is time, $M$ is additional memory, and $n$ is `prec`.
4978 ///
4979 /// # Examples
4980 /// ```
4981 /// use malachite_float::Float;
4982 /// use std::cmp::Ordering::*;
4983 ///
4984 /// let (p, o) = (&Float::from(3)).pow_s_prec_ref(5, 20);
4985 /// assert_eq!(p.to_string(), "243.00000");
4986 /// assert_eq!(o, Equal);
4987 ///
4988 /// let (p, o) = (&Float::from(3)).pow_s_prec_ref(-2, 10);
4989 /// assert_eq!(p.to_string(), "0.11108");
4990 /// assert_eq!(o, Less);
4991 /// ```
4992 #[inline]
4993 pub fn pow_s_prec_ref(&self, n: i64, prec: u64) -> (Self, Ordering) {
4994 pow_s_ref(self, n, prec, Nearest)
4995 }
4996
4997 /// Raises a [`Float`] to the power of a [`i64`], rounding the result to the precision of the
4998 /// base and with the specified rounding mode. The [`Float`] is taken by value. An [`Ordering`]
4999 /// is also returned, indicating whether the rounded power is less than, equal to, or greater
5000 /// than the exact power. Although `NaN`s are not comparable to any [`Float`], whenever this
5001 /// function returns a `NaN` it also returns `Equal`.
5002 ///
5003 /// The output precision is the precision of `self`. See [`RoundingMode`] for a description of
5004 /// the possible rounding modes.
5005 ///
5006 /// $$
5007 /// f(x,n,p,m) = x^n+\varepsilon.
5008 /// $$
5009 /// - If $x^n$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
5010 /// - If $x^n$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
5011 /// 2^{\lfloor\log_2 |x^n|\rfloor-p+1}$.
5012 /// - If $x^n$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
5013 /// 2^{\lfloor\log_2 |x^n|\rfloor-p}$.
5014 ///
5015 /// See the [`Float::pow_s_prec_round`] documentation for information on special cases,
5016 /// overflow, and underflow.
5017 ///
5018 /// If you want to specify an output precision, consider using [`Float::pow_s_prec_round`]
5019 /// instead.
5020 ///
5021 /// # Worst-case complexity
5022 /// $T(n) = O(n \log n \log\log n)$
5023 ///
5024 /// $M(n) = O(n \log n)$
5025 ///
5026 /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
5027 ///
5028 /// # Panics
5029 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the base's
5030 /// precision.
5031 ///
5032 /// # Examples
5033 /// ```
5034 /// use malachite_base::rounding_modes::RoundingMode::*;
5035 /// use malachite_float::Float;
5036 /// use std::cmp::Ordering::*;
5037 ///
5038 /// let (p, o) = Float::from(3).pow_s_round(5, Floor);
5039 /// assert_eq!(p.to_string(), "1.9e2");
5040 /// assert_eq!(o, Less);
5041 ///
5042 /// let (p, o) = Float::from(3).pow_s_round(5, Ceiling);
5043 /// assert_eq!(p.to_string(), "2.6e2");
5044 /// assert_eq!(o, Greater);
5045 /// ```
5046 #[inline]
5047 pub fn pow_s_round(self, n: i64, rm: RoundingMode) -> (Self, Ordering) {
5048 let prec = self.significant_bits();
5049 pow_s(self, n, prec, rm)
5050 }
5051
5052 /// Raises a [`Float`] to the power of a [`i64`], rounding the result to the precision of the
5053 /// base and with the specified rounding mode. The [`Float`] is taken by reference. An
5054 /// [`Ordering`] is also returned, indicating whether the rounded power is less than, equal to,
5055 /// or greater than the exact power. Although `NaN`s are not comparable to any [`Float`],
5056 /// whenever this function returns a `NaN` it also returns `Equal`.
5057 ///
5058 /// The output precision is the precision of `self`. See [`RoundingMode`] for a description of
5059 /// the possible rounding modes.
5060 ///
5061 /// $$
5062 /// f(x,n,p,m) = x^n+\varepsilon.
5063 /// $$
5064 /// - If $x^n$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
5065 /// - If $x^n$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
5066 /// 2^{\lfloor\log_2 |x^n|\rfloor-p+1}$.
5067 /// - If $x^n$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
5068 /// 2^{\lfloor\log_2 |x^n|\rfloor-p}$.
5069 ///
5070 /// See the [`Float::pow_s_prec_round`] documentation for information on special cases,
5071 /// overflow, and underflow.
5072 ///
5073 /// If you want to specify an output precision, consider using [`Float::pow_s_prec_round_ref`]
5074 /// instead.
5075 ///
5076 /// # Worst-case complexity
5077 /// $T(n) = O(n \log n \log\log n)$
5078 ///
5079 /// $M(n) = O(n \log n)$
5080 ///
5081 /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
5082 ///
5083 /// # Panics
5084 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the base's
5085 /// precision.
5086 ///
5087 /// # Examples
5088 /// ```
5089 /// use malachite_base::rounding_modes::RoundingMode::*;
5090 /// use malachite_float::Float;
5091 /// use std::cmp::Ordering::*;
5092 ///
5093 /// let (p, o) = (&Float::from(3)).pow_s_round_ref(5, Floor);
5094 /// assert_eq!(p.to_string(), "1.9e2");
5095 /// assert_eq!(o, Less);
5096 ///
5097 /// let (p, o) = (&Float::from(3)).pow_s_round_ref(5, Ceiling);
5098 /// assert_eq!(p.to_string(), "2.6e2");
5099 /// assert_eq!(o, Greater);
5100 /// ```
5101 #[inline]
5102 pub fn pow_s_round_ref(&self, n: i64, rm: RoundingMode) -> (Self, Ordering) {
5103 pow_s_ref(self, n, self.significant_bits(), rm)
5104 }
5105
5106 /// Raises a [`Float`] to the power of a [`i64`] in place, rounding the result to the specified
5107 /// precision and with the specified rounding mode.
5108 ///
5109 /// See the [`Float::pow_s_prec_round`] documentation for information on special cases,
5110 /// overflow, and underflow.
5111 ///
5112 /// # Worst-case complexity
5113 /// $T(n) = O(n \log n \log\log n)$
5114 ///
5115 /// $M(n) = O(n \log n)$
5116 ///
5117 /// where $T$ is time, $M$ is additional memory, and $n$ is `prec`.
5118 ///
5119 /// # Panics
5120 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the given
5121 /// precision.
5122 ///
5123 /// # Examples
5124 /// ```
5125 /// use malachite_base::rounding_modes::RoundingMode::*;
5126 /// use malachite_float::Float;
5127 /// use std::cmp::Ordering::*;
5128 ///
5129 /// let mut x = Float::from(3);
5130 /// let o = x.pow_s_prec_round_assign(5, 20, Floor);
5131 /// assert_eq!(x.to_string(), "243.00000");
5132 /// assert_eq!(o, Equal);
5133 /// ```
5134 pub fn pow_s_prec_round_assign(&mut self, n: i64, prec: u64, rm: RoundingMode) -> Ordering {
5135 let mut x = Self::ZERO;
5136 swap(self, &mut x);
5137 let (result, o) = pow_s(x, n, prec, rm);
5138 *self = result;
5139 o
5140 }
5141
5142 /// Raises a [`Float`] to the power of a [`i64`] in place, rounding the result to the specified
5143 /// precision and to the nearest value.
5144 ///
5145 /// See the [`Float::pow_s_prec_round`] documentation for information on special cases,
5146 /// overflow, and underflow.
5147 ///
5148 /// # Worst-case complexity
5149 /// $T(n) = O(n \log n \log\log n)$
5150 ///
5151 /// $M(n) = O(n \log n)$
5152 ///
5153 /// where $T$ is time, $M$ is additional memory, and $n$ is `prec`.
5154 ///
5155 /// # Examples
5156 /// ```
5157 /// use malachite_float::Float;
5158 /// use std::cmp::Ordering::*;
5159 ///
5160 /// let mut x = Float::from(3);
5161 /// let o = x.pow_s_prec_assign(5, 20);
5162 /// assert_eq!(x.to_string(), "243.00000");
5163 /// assert_eq!(o, Equal);
5164 /// ```
5165 #[inline]
5166 pub fn pow_s_prec_assign(&mut self, n: i64, prec: u64) -> Ordering {
5167 self.pow_s_prec_round_assign(n, prec, Nearest)
5168 }
5169
5170 /// Raises a [`Float`] to the power of a [`i64`] in place, rounding the result to the precision
5171 /// of the base and with the specified rounding mode.
5172 ///
5173 /// See the [`Float::pow_s_prec_round`] documentation for information on special cases,
5174 /// overflow, and underflow.
5175 ///
5176 /// # Worst-case complexity
5177 /// $T(n) = O(n \log n \log\log n)$
5178 ///
5179 /// $M(n) = O(n \log n)$
5180 ///
5181 /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
5182 ///
5183 /// # Panics
5184 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the base's
5185 /// precision.
5186 ///
5187 /// # Examples
5188 /// ```
5189 /// use malachite_base::rounding_modes::RoundingMode::*;
5190 /// use malachite_float::Float;
5191 /// use std::cmp::Ordering::*;
5192 ///
5193 /// let mut x = Float::from(3);
5194 /// let o = x.pow_s_round_assign(5, Floor);
5195 /// assert_eq!(x.to_string(), "1.9e2");
5196 /// assert_eq!(o, Less);
5197 /// ```
5198 #[inline]
5199 pub fn pow_s_round_assign(&mut self, n: i64, rm: RoundingMode) -> Ordering {
5200 let prec = self.significant_bits();
5201 self.pow_s_prec_round_assign(n, prec, rm)
5202 }
5203}
5204
5205impl Pow<i64> for Float {
5206 type Output = Self;
5207
5208 /// Raises a [`Float`] to an [`i64`] power, rounding the result to the nearest value at the
5209 /// precision of the base. The [`Float`] is taken by value.
5210 #[inline]
5211 fn pow(self, n: i64) -> Self {
5212 let prec = self.significant_bits();
5213 pow_s(self, n, prec, Nearest).0
5214 }
5215}
5216
5217impl Pow<i64> for &Float {
5218 type Output = Float;
5219
5220 /// Raises a [`Float`] to an [`i64`] power, rounding the result to the nearest value at the
5221 /// precision of the base. The [`Float`] is taken by reference.
5222 #[inline]
5223 fn pow(self, n: i64) -> Float {
5224 pow_s_ref(self, n, self.significant_bits(), Nearest).0
5225 }
5226}
5227
5228impl PowAssign<i64> for Float {
5229 /// Raises a [`Float`] to an [`i64`] power in place, rounding the result to the nearest value at
5230 /// the precision of the base.
5231 #[inline]
5232 fn pow_assign(&mut self, n: i64) {
5233 let prec = self.significant_bits();
5234 self.pow_s_prec_assign(n, prec);
5235 }
5236}
5237
5238impl Float {
5239 /// Raises a [`u64`] to the power of a [`u64`], returning a [`Float`] rounded to the specified
5240 /// precision and with the specified rounding mode. An [`Ordering`] is also returned, indicating
5241 /// whether the rounded power is less than, equal to, or greater than the exact power.
5242 ///
5243 /// See [`RoundingMode`] for a description of the possible rounding modes.
5244 ///
5245 /// $$
5246 /// f(x,y,p,m) = x^y+\varepsilon.
5247 /// $$
5248 /// - If $x^y$ is zero, $\varepsilon$ may be ignored or assumed to be 0.
5249 /// - If $x^y$ is nonzero, and $m$ is not `Nearest`, then $|\varepsilon| < 2^{\lfloor\log_2
5250 /// x^y\rfloor-p+1}$.
5251 /// - If $x^y$ is nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq 2^{\lfloor\log_2
5252 /// x^y\rfloor-p}$.
5253 ///
5254 /// The result is always nonnegative, so it never underflows.
5255 ///
5256 /// Special cases:
5257 /// - $f(x,0,p,m)=1.0$ for any $x$
5258 /// - $f(0,y,p,m)=0.0$ if $y>0$
5259 /// - $f(1,y,p,m)=1.0$
5260 ///
5261 /// Overflow:
5262 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
5263 /// returned instead.
5264 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor` or `Down`, $(1-(1/2)^p)2^{2^{30}-1}$
5265 /// is returned instead.
5266 ///
5267 /// # Worst-case complexity
5268 /// $T(n) = O(n \log n \log\log n)$
5269 ///
5270 /// $M(n) = O(n \log n)$
5271 ///
5272 /// where $T$ is time, $M$ is additional memory, and $n$ is `prec`.
5273 ///
5274 /// # Panics
5275 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the given
5276 /// precision.
5277 ///
5278 /// # Examples
5279 /// ```
5280 /// use malachite_base::rounding_modes::RoundingMode::*;
5281 /// use malachite_float::Float;
5282 /// use std::cmp::Ordering::*;
5283 ///
5284 /// let (p, o) = Float::unsigned_pow_unsigned_prec_round(3, 5, 20, Floor);
5285 /// assert_eq!(p.to_string(), "243.00000");
5286 /// assert_eq!(o, Equal);
5287 ///
5288 /// let (p, o) = Float::unsigned_pow_unsigned_prec_round(3, 5, 2, Ceiling);
5289 /// assert_eq!(p.to_string(), "2.6e2");
5290 /// assert_eq!(o, Greater);
5291 /// ```
5292 #[inline]
5293 pub fn unsigned_pow_unsigned_prec_round(
5294 x: u64,
5295 y: u64,
5296 prec: u64,
5297 rm: RoundingMode,
5298 ) -> (Self, Ordering) {
5299 unsigned_pow_unsigned(x, y, prec, rm)
5300 }
5301
5302 /// Raises a [`u64`] to the power of a [`u64`], returning a [`Float`] rounded to the specified
5303 /// precision and to the nearest value. An [`Ordering`] is also returned, indicating whether the
5304 /// rounded power is less than, equal to, or greater than the exact power.
5305 ///
5306 /// If the power is equidistant from two [`Float`]s with the specified precision, the [`Float`]
5307 /// with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of
5308 /// the `Nearest` rounding mode.
5309 ///
5310 /// $$
5311 /// f(x,y,p) = x^y+\varepsilon.
5312 /// $$
5313 /// - If $x^y$ is zero, $\varepsilon$ may be ignored or assumed to be 0.
5314 /// - If $x^y$ is nonzero, then $|\varepsilon| \leq 2^{\lfloor\log_2 x^y\rfloor-p}$.
5315 ///
5316 /// See the [`Float::unsigned_pow_unsigned_prec_round`] documentation for information on special
5317 /// cases and overflow.
5318 ///
5319 /// If you want to use a rounding mode other than `Nearest`, consider using
5320 /// [`Float::unsigned_pow_unsigned_prec_round`] instead.
5321 ///
5322 /// # Worst-case complexity
5323 /// $T(n) = O(n \log n \log\log n)$
5324 ///
5325 /// $M(n) = O(n \log n)$
5326 ///
5327 /// where $T$ is time, $M$ is additional memory, and $n$ is `prec`.
5328 ///
5329 /// # Examples
5330 /// ```
5331 /// use malachite_float::Float;
5332 /// use std::cmp::Ordering::*;
5333 ///
5334 /// let (p, o) = Float::unsigned_pow_unsigned_prec(3, 5, 20);
5335 /// assert_eq!(p.to_string(), "243.00000");
5336 /// assert_eq!(o, Equal);
5337 ///
5338 /// let (p, o) = Float::unsigned_pow_unsigned_prec(3, 5, 2);
5339 /// assert_eq!(p.to_string(), "2.6e2");
5340 /// assert_eq!(o, Greater);
5341 /// ```
5342 #[inline]
5343 pub fn unsigned_pow_unsigned_prec(x: u64, y: u64, prec: u64) -> (Self, Ordering) {
5344 unsigned_pow_unsigned(x, y, prec, Nearest)
5345 }
5346
5347 /// Raises a [`u64`] to the power of a [`Float`], returning a [`Float`] rounded to the specified
5348 /// precision and with the specified rounding mode. The [`Float`] exponent is taken by value. An
5349 /// [`Ordering`] is also returned, indicating whether the rounded power is less than, equal to,
5350 /// or greater than the exact power. Although `NaN`s are not comparable to any [`Float`],
5351 /// whenever this function returns a `NaN` it also returns `Equal`.
5352 ///
5353 /// See [`RoundingMode`] for a description of the possible rounding modes.
5354 ///
5355 /// $$
5356 /// f(x,y,p,m) = x^y+\varepsilon.
5357 /// $$
5358 /// - If $x^y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
5359 /// - If $x^y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
5360 /// 2^{\lfloor\log_2 x^y\rfloor-p+1}$.
5361 /// - If $x^y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
5362 /// 2^{\lfloor\log_2 x^y\rfloor-p}$.
5363 ///
5364 /// Special cases:
5365 /// - $f(x,0.0,p,m)=1.0$ for any $x$
5366 /// - $f(1,y,p,m)=1.0$ for any $y$, even `NaN`
5367 /// - $f(x,\text{NaN},p,m)=\text{NaN}$ if $x \neq 1$
5368 /// - $f(x,\infty,p,m)=\infty$ if $x>1$, and $0.0$ if $x=0$
5369 /// - $f(x,-\infty,p,m)=0.0$ if $x>1$, and $\infty$ if $x=0$
5370 /// - $f(0,y,p,m)=0.0$ if $y>0$, and $\infty$ if $y<0$
5371 ///
5372 /// Overflow and underflow:
5373 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
5374 /// returned instead.
5375 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor` or `Down`, $(1-(1/2)^p)2^{2^{30}-1}$
5376 /// is returned instead.
5377 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
5378 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
5379 /// instead.
5380 /// - If $0<f(x,y,p,m)\leq2^{-2^{30}-1}$ and $m$ is `Nearest`, $0.0$ is returned instead.
5381 /// - If $2^{-2^{30}-1}<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
5382 /// instead.
5383 ///
5384 /// # Worst-case complexity
5385 /// $T(n) = O(n^{3/2} \log n \log\log n)$
5386 ///
5387 /// $M(n) = O(n (\log n)^2)$
5388 ///
5389 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(prec, y.significant_bits())`.
5390 ///
5391 /// # Panics
5392 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the given
5393 /// precision.
5394 ///
5395 /// # Examples
5396 /// ```
5397 /// use malachite_base::rounding_modes::RoundingMode::*;
5398 /// use malachite_float::Float;
5399 /// use std::cmp::Ordering::*;
5400 ///
5401 /// let (p, o) = Float::unsigned_pow_prec_round(2, Float::from(0.5), 53, Nearest);
5402 /// assert_eq!(p.to_string(), "1.4142135623730951");
5403 /// assert_eq!(o, Greater);
5404 ///
5405 /// let (p, o) = Float::unsigned_pow_prec_round(3, Float::from(2.5), 53, Floor);
5406 /// assert_eq!(p.to_string(), "15.588457268119894");
5407 /// assert_eq!(o, Less);
5408 /// ```
5409 ///
5410 /// This is equivalent to `mpfr_ui_pow` from `ui_pow.c`, MPFR 4.3.0, which likewise converts the
5411 /// integer exactly and delegates to `mpfr_pow`.
5412 #[inline]
5413 pub fn unsigned_pow_prec_round(
5414 x: u64,
5415 y: Self,
5416 prec: u64,
5417 rm: RoundingMode,
5418 ) -> (Self, Ordering) {
5419 Self::from(x).pow_prec_round(y, prec, rm)
5420 }
5421
5422 /// Raises a [`u64`] to the power of a [`Float`], returning a [`Float`] rounded to the specified
5423 /// precision and with the specified rounding mode. The [`Float`] exponent is taken by
5424 /// reference. An [`Ordering`] is also returned, indicating whether the rounded power is less
5425 /// than, equal to, or greater than the exact power. Although `NaN`s are not comparable to any
5426 /// [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
5427 ///
5428 /// See [`RoundingMode`] for a description of the possible rounding modes.
5429 ///
5430 /// $$
5431 /// f(x,y,p,m) = x^y+\varepsilon.
5432 /// $$
5433 /// - If $x^y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
5434 /// - If $x^y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
5435 /// 2^{\lfloor\log_2 x^y\rfloor-p+1}$.
5436 /// - If $x^y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
5437 /// 2^{\lfloor\log_2 x^y\rfloor-p}$.
5438 ///
5439 /// See the [`Float::unsigned_pow_prec_round`] documentation for information on special cases,
5440 /// overflow, and underflow.
5441 ///
5442 /// # Worst-case complexity
5443 /// $T(n) = O(n^{3/2} \log n \log\log n)$
5444 ///
5445 /// $M(n) = O(n (\log n)^2)$
5446 ///
5447 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(prec, y.significant_bits())`.
5448 ///
5449 /// # Panics
5450 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the given
5451 /// precision.
5452 ///
5453 /// # Examples
5454 /// ```
5455 /// use malachite_base::rounding_modes::RoundingMode::*;
5456 /// use malachite_float::Float;
5457 /// use std::cmp::Ordering::*;
5458 ///
5459 /// let (p, o) = Float::unsigned_pow_prec_round_ref(2, &Float::from(0.5), 53, Nearest);
5460 /// assert_eq!(p.to_string(), "1.4142135623730951");
5461 /// assert_eq!(o, Greater);
5462 ///
5463 /// let (p, o) = Float::unsigned_pow_prec_round_ref(3, &Float::from(2.5), 53, Floor);
5464 /// assert_eq!(p.to_string(), "15.588457268119894");
5465 /// assert_eq!(o, Less);
5466 /// ```
5467 #[inline]
5468 pub fn unsigned_pow_prec_round_ref(
5469 x: u64,
5470 y: &Self,
5471 prec: u64,
5472 rm: RoundingMode,
5473 ) -> (Self, Ordering) {
5474 Self::from(x).pow_prec_round_val_ref(y, prec, rm)
5475 }
5476
5477 /// Raises a [`u64`] to the power of a [`Float`], returning a [`Float`] rounded to the specified
5478 /// precision and to the nearest value. The [`Float`] exponent is taken by value. An
5479 /// [`Ordering`] is also returned, indicating whether the rounded power is less than, equal to,
5480 /// or greater than the exact power. Although `NaN`s are not comparable to any [`Float`],
5481 /// whenever this function returns a `NaN` it also returns `Equal`.
5482 ///
5483 /// If the power is equidistant from two [`Float`]s with the specified precision, the [`Float`]
5484 /// with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of
5485 /// the `Nearest` rounding mode.
5486 ///
5487 /// $$
5488 /// f(x,y,p) = x^y+\varepsilon.
5489 /// $$
5490 /// - If $x^y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
5491 /// - If $x^y$ is finite and nonzero, then $|\varepsilon| \leq 2^{\lfloor\log_2 x^y\rfloor-p}$.
5492 ///
5493 /// See the [`Float::unsigned_pow_prec_round`] documentation for information on special cases,
5494 /// overflow, and underflow.
5495 ///
5496 /// If you want to use a rounding mode other than `Nearest`, consider using
5497 /// [`Float::unsigned_pow_prec_round`] instead.
5498 ///
5499 /// # Worst-case complexity
5500 /// $T(n) = O(n^{3/2} \log n \log\log n)$
5501 ///
5502 /// $M(n) = O(n (\log n)^2)$
5503 ///
5504 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(prec, y.significant_bits())`.
5505 ///
5506 /// # Examples
5507 /// ```
5508 /// use malachite_float::Float;
5509 /// use std::cmp::Ordering::*;
5510 ///
5511 /// let (p, o) = Float::unsigned_pow_prec(2, Float::from(0.5), 53);
5512 /// assert_eq!(p.to_string(), "1.4142135623730951");
5513 /// assert_eq!(o, Greater);
5514 ///
5515 /// let (p, o) = Float::unsigned_pow_prec(3, Float::from(2.5), 53);
5516 /// assert_eq!(p.to_string(), "15.588457268119896");
5517 /// assert_eq!(o, Greater);
5518 /// ```
5519 #[inline]
5520 pub fn unsigned_pow_prec(x: u64, y: Self, prec: u64) -> (Self, Ordering) {
5521 Self::unsigned_pow_prec_round(x, y, prec, Nearest)
5522 }
5523
5524 /// Raises a [`u64`] to the power of a [`Float`], returning a [`Float`] rounded to the specified
5525 /// precision and to the nearest value. The [`Float`] exponent is taken by reference. An
5526 /// [`Ordering`] is also returned, indicating whether the rounded power is less than, equal to,
5527 /// or greater than the exact power. Although `NaN`s are not comparable to any [`Float`],
5528 /// whenever this function returns a `NaN` it also returns `Equal`.
5529 ///
5530 /// If the power is equidistant from two [`Float`]s with the specified precision, the [`Float`]
5531 /// with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of
5532 /// the `Nearest` rounding mode.
5533 ///
5534 /// $$
5535 /// f(x,y,p) = x^y+\varepsilon.
5536 /// $$
5537 /// - If $x^y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
5538 /// - If $x^y$ is finite and nonzero, then $|\varepsilon| \leq 2^{\lfloor\log_2 x^y\rfloor-p}$.
5539 ///
5540 /// See the [`Float::unsigned_pow_prec_round`] documentation for information on special cases,
5541 /// overflow, and underflow.
5542 ///
5543 /// If you want to use a rounding mode other than `Nearest`, consider using
5544 /// [`Float::unsigned_pow_prec_round_ref`] instead.
5545 ///
5546 /// # Worst-case complexity
5547 /// $T(n) = O(n^{3/2} \log n \log\log n)$
5548 ///
5549 /// $M(n) = O(n (\log n)^2)$
5550 ///
5551 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(prec, y.significant_bits())`.
5552 ///
5553 /// # Examples
5554 /// ```
5555 /// use malachite_float::Float;
5556 /// use std::cmp::Ordering::*;
5557 ///
5558 /// let (p, o) = Float::unsigned_pow_prec_ref(2, &Float::from(0.5), 53);
5559 /// assert_eq!(p.to_string(), "1.4142135623730951");
5560 /// assert_eq!(o, Greater);
5561 ///
5562 /// let (p, o) = Float::unsigned_pow_prec_ref(3, &Float::from(2.5), 53);
5563 /// assert_eq!(p.to_string(), "15.588457268119896");
5564 /// assert_eq!(o, Greater);
5565 /// ```
5566 #[inline]
5567 pub fn unsigned_pow_prec_ref(x: u64, y: &Self, prec: u64) -> (Self, Ordering) {
5568 Self::unsigned_pow_prec_round_ref(x, y, prec, Nearest)
5569 }
5570
5571 /// Raises a [`u64`] to the power of a [`Rational`], returning a [`Float`] rounded to the
5572 /// specified precision and with the specified rounding mode. The [`Rational`] exponent is taken
5573 /// by value. An [`Ordering`] is also returned, indicating whether the rounded power is less
5574 /// than, equal to, or greater than the exact power.
5575 ///
5576 /// See [`RoundingMode`] for a description of the possible rounding modes.
5577 ///
5578 /// $$
5579 /// f(x,y,p,m) = x^y+\varepsilon.
5580 /// $$
5581 /// - If $x^y$ is zero or infinite, $\varepsilon$ may be ignored or assumed to be 0.
5582 /// - If $x^y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
5583 /// 2^{\lfloor\log_2 x^y\rfloor-p+1}$.
5584 /// - If $x^y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
5585 /// 2^{\lfloor\log_2 x^y\rfloor-p}$.
5586 ///
5587 /// Special cases:
5588 /// - $f(x,0,p,m)=1.0$ for any $x$
5589 /// - $f(1,y,p,m)=1.0$ for any $y$
5590 /// - $f(0,y,p,m)=0.0$ if $y>0$, and $\infty$ if $y<0$
5591 ///
5592 /// Overflow and underflow:
5593 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
5594 /// returned instead.
5595 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor` or `Down`, $(1-(1/2)^p)2^{2^{30}-1}$
5596 /// is returned instead.
5597 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
5598 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
5599 /// instead.
5600 /// - If $0<f(x,y,p,m)\leq2^{-2^{30}-1}$ and $m$ is `Nearest`, $0.0$ is returned instead.
5601 /// - If $2^{-2^{30}-1}<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
5602 /// instead.
5603 ///
5604 /// # Worst-case complexity
5605 /// $T(n) = O(n^{3/2} \log n \log\log n)$
5606 ///
5607 /// $M(n) = O(n (\log n)^2)$
5608 ///
5609 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(prec, y.significant_bits())`.
5610 ///
5611 /// # Panics
5612 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the given
5613 /// precision.
5614 ///
5615 /// # Examples
5616 /// ```
5617 /// use malachite_base::rounding_modes::RoundingMode::*;
5618 /// use malachite_float::Float;
5619 /// use malachite_q::Rational;
5620 /// use std::cmp::Ordering::*;
5621 ///
5622 /// let (p, o) =
5623 /// Float::unsigned_pow_rational_prec_round(8, Rational::from_signeds(1, 3), 20, Floor);
5624 /// assert_eq!(p.to_string(), "2.0000000");
5625 /// assert_eq!(o, Equal);
5626 ///
5627 /// let (p, o) =
5628 /// Float::unsigned_pow_rational_prec_round(3, Rational::from_signeds(1, 2), 2, Floor);
5629 /// assert_eq!(p.to_string(), "1.5");
5630 /// assert_eq!(o, Less);
5631 /// ```
5632 #[allow(clippy::needless_pass_by_value)]
5633 #[inline]
5634 pub fn unsigned_pow_rational_prec_round(
5635 x: u64,
5636 y: Rational,
5637 prec: u64,
5638 rm: RoundingMode,
5639 ) -> (Self, Ordering) {
5640 unsigned_pow_rational(x, &y, prec, rm)
5641 }
5642
5643 /// Raises a [`u64`] to the power of a [`Rational`], returning a [`Float`] rounded to the
5644 /// specified precision and with the specified rounding mode. The [`Rational`] exponent is taken
5645 /// by reference. An [`Ordering`] is also returned, indicating whether the rounded power is less
5646 /// than, equal to, or greater than the exact power.
5647 ///
5648 /// See [`RoundingMode`] for a description of the possible rounding modes.
5649 ///
5650 /// $$
5651 /// f(x,y,p,m) = x^y+\varepsilon.
5652 /// $$
5653 /// - If $x^y$ is zero or infinite, $\varepsilon$ may be ignored or assumed to be 0.
5654 /// - If $x^y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
5655 /// 2^{\lfloor\log_2 x^y\rfloor-p+1}$.
5656 /// - If $x^y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
5657 /// 2^{\lfloor\log_2 x^y\rfloor-p}$.
5658 ///
5659 /// See the [`Float::unsigned_pow_rational_prec_round`] documentation for information on special
5660 /// cases, overflow, and underflow.
5661 ///
5662 /// # Worst-case complexity
5663 /// $T(n) = O(n^{3/2} \log n \log\log n)$
5664 ///
5665 /// $M(n) = O(n (\log n)^2)$
5666 ///
5667 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(prec, y.significant_bits())`.
5668 ///
5669 /// # Panics
5670 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the given
5671 /// precision.
5672 ///
5673 /// # Examples
5674 /// ```
5675 /// use malachite_base::rounding_modes::RoundingMode::*;
5676 /// use malachite_float::Float;
5677 /// use malachite_q::Rational;
5678 /// use std::cmp::Ordering::*;
5679 ///
5680 /// let (p, o) = Float::unsigned_pow_rational_prec_round_ref(
5681 /// 8,
5682 /// &Rational::from_signeds(1, 3),
5683 /// 20,
5684 /// Floor,
5685 /// );
5686 /// assert_eq!(p.to_string(), "2.0000000");
5687 /// assert_eq!(o, Equal);
5688 ///
5689 /// let (p, o) = Float::unsigned_pow_rational_prec_round_ref(
5690 /// 3,
5691 /// &Rational::from_signeds(1, 2),
5692 /// 2,
5693 /// Ceiling,
5694 /// );
5695 /// assert_eq!(p.to_string(), "2.0");
5696 /// assert_eq!(o, Greater);
5697 /// ```
5698 #[inline]
5699 pub fn unsigned_pow_rational_prec_round_ref(
5700 x: u64,
5701 y: &Rational,
5702 prec: u64,
5703 rm: RoundingMode,
5704 ) -> (Self, Ordering) {
5705 unsigned_pow_rational(x, y, prec, rm)
5706 }
5707
5708 /// Raises a [`u64`] to the power of a [`Rational`], returning a [`Float`] rounded to the
5709 /// specified precision and to the nearest value. The [`Rational`] exponent is taken by value.
5710 /// An [`Ordering`] is also returned, indicating whether the rounded power is less than, equal
5711 /// to, or greater than the exact power.
5712 ///
5713 /// If the power is equidistant from two [`Float`]s with the specified precision, the [`Float`]
5714 /// with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of
5715 /// the `Nearest` rounding mode.
5716 ///
5717 /// $$
5718 /// f(x,y,p) = x^y+\varepsilon.
5719 /// $$
5720 /// - If $x^y$ is zero or infinite, $\varepsilon$ may be ignored or assumed to be 0.
5721 /// - If $x^y$ is finite and nonzero, then $|\varepsilon| \leq 2^{\lfloor\log_2 x^y\rfloor-p}$.
5722 ///
5723 /// See the [`Float::unsigned_pow_rational_prec_round`] documentation for information on special
5724 /// cases, overflow, and underflow.
5725 ///
5726 /// If you want to use a rounding mode other than `Nearest`, consider using
5727 /// [`Float::unsigned_pow_rational_prec_round`] instead.
5728 ///
5729 /// # Worst-case complexity
5730 /// $T(n) = O(n^{3/2} \log n \log\log n)$
5731 ///
5732 /// $M(n) = O(n (\log n)^2)$
5733 ///
5734 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(prec, y.significant_bits())`.
5735 ///
5736 /// # Examples
5737 /// ```
5738 /// use malachite_float::Float;
5739 /// use malachite_q::Rational;
5740 /// use std::cmp::Ordering::*;
5741 ///
5742 /// let (p, o) = Float::unsigned_pow_rational_prec(8, Rational::from_signeds(1, 3), 20);
5743 /// assert_eq!(p.to_string(), "2.0000000");
5744 /// assert_eq!(o, Equal);
5745 ///
5746 /// let (p, o) = Float::unsigned_pow_rational_prec(3, Rational::from_signeds(1, 2), 53);
5747 /// assert_eq!(p.to_string(), "1.7320508075688772");
5748 /// assert_eq!(o, Less);
5749 /// ```
5750 #[inline]
5751 #[allow(clippy::needless_pass_by_value)]
5752 pub fn unsigned_pow_rational_prec(x: u64, y: Rational, prec: u64) -> (Self, Ordering) {
5753 unsigned_pow_rational(x, &y, prec, Nearest)
5754 }
5755
5756 /// Raises a [`u64`] to the power of a [`Rational`], returning a [`Float`] rounded to the
5757 /// specified precision and to the nearest value. The [`Rational`] exponent is taken by
5758 /// reference. An [`Ordering`] is also returned, indicating whether the rounded power is less
5759 /// than, equal to, or greater than the exact power.
5760 ///
5761 /// If the power is equidistant from two [`Float`]s with the specified precision, the [`Float`]
5762 /// with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of
5763 /// the `Nearest` rounding mode.
5764 ///
5765 /// $$
5766 /// f(x,y,p) = x^y+\varepsilon.
5767 /// $$
5768 /// - If $x^y$ is zero or infinite, $\varepsilon$ may be ignored or assumed to be 0.
5769 /// - If $x^y$ is finite and nonzero, then $|\varepsilon| \leq 2^{\lfloor\log_2 x^y\rfloor-p}$.
5770 ///
5771 /// See the [`Float::unsigned_pow_rational_prec_round`] documentation for information on special
5772 /// cases, overflow, and underflow.
5773 ///
5774 /// If you want to use a rounding mode other than `Nearest`, consider using
5775 /// [`Float::unsigned_pow_rational_prec_round_ref`] instead.
5776 ///
5777 /// # Worst-case complexity
5778 /// $T(n) = O(n^{3/2} \log n \log\log n)$
5779 ///
5780 /// $M(n) = O(n (\log n)^2)$
5781 ///
5782 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(prec, y.significant_bits())`.
5783 ///
5784 /// # Examples
5785 /// ```
5786 /// use malachite_float::Float;
5787 /// use malachite_q::Rational;
5788 /// use std::cmp::Ordering::*;
5789 ///
5790 /// let (p, o) = Float::unsigned_pow_rational_prec_ref(27, &Rational::from_signeds(1, 3), 20);
5791 /// assert_eq!(p.to_string(), "3.0000000");
5792 /// assert_eq!(o, Equal);
5793 ///
5794 /// let (p, o) = Float::unsigned_pow_rational_prec_ref(3, &Rational::from_signeds(1, 2), 53);
5795 /// assert_eq!(p.to_string(), "1.7320508075688772");
5796 /// assert_eq!(o, Less);
5797 /// ```
5798 #[inline]
5799 pub fn unsigned_pow_rational_prec_ref(x: u64, y: &Rational, prec: u64) -> (Self, Ordering) {
5800 unsigned_pow_rational(x, y, prec, Nearest)
5801 }
5802}
5803
5804// k^q for a u64 k and Rational q. Since MPFR has no rational-exponent power, this is not a port:
5805// the value is 2^(q * log2(k)). Exact-rational results (k a perfect b-th power) and a power-of-2
5806// base are peeled off first (a Ziv-style squeeze never converges on an exactly-representable
5807// result); the remaining results are irrational and are bracketed by squeezing 2^(q * log2(k))
5808// between exact Rationals.
5809fn unsigned_pow_rational(k: u64, q: &Rational, prec: u64, rm: RoundingMode) -> (Float, Ordering) {
5810 assert_ne!(prec, 0);
5811 // Exact rounding: compute with Floor and demand exactness.
5812 if rm == Exact {
5813 let (result, o) = unsigned_pow_rational(k, q, prec, Floor);
5814 assert_eq!(o, Equal, "Inexact unsigned_pow_rational");
5815 return (result, Equal);
5816 }
5817 // k^0 = 1 for any k, even 0; 1^q = 1 for any q
5818 if *q == 0u32 || k == 1 {
5819 return (Float::one_prec(prec), Equal);
5820 }
5821 // 0^q = 0 for q > 0, and +Inf for q < 0
5822 if k == 0 {
5823 return if *q > 0u32 {
5824 (Float::ZERO, Equal)
5825 } else {
5826 (Float::INFINITY, Equal)
5827 };
5828 }
5829 // k = 2^s: k^q = 2^(s * q), and `power_of_2_rational_prec_round` handles all exactness,
5830 // overflow, and underflow.
5831 if k.is_power_of_2() {
5832 return Float::power_of_2_rational_prec_round(
5833 Rational::from(k.trailing_zeros()) * q,
5834 prec,
5835 rm,
5836 );
5837 }
5838 // k = j^b (with q = a / b in lowest terms): k^q = j^a is an exact rational, obtained by raising
5839 // the exact Float j to the integer power a.
5840 if let Ok(b) = u64::try_from(q.denominator_ref())
5841 && let Some(j) = k.checked_root(b)
5842 {
5843 let a = Integer::from_sign_and_abs_ref(*q >= 0, q.numerator_ref());
5844 return Float::from(j).pow_integer_prec_round(a, prec, rm);
5845 }
5846 // The remaining results are irrational. When `q` is tiny enough that `k ^ q` is within a few
5847 // ulps of 1, evaluating it as `2 ^ (q * log2(k))` would compute `log2(k)` to nearly `prec` bits
5848 // needlessly; a dedicated near-1 path handles that case far more cheaply.
5849 if let Some(result) = unsigned_pow_rational_near_one(k, q, prec, rm) {
5850 return result;
5851 }
5852 // Otherwise squeeze 2^(q * log2(k)) between exact Rationals. Since k >= 2, log2(k) >= 1, so
5853 // there is no sub-`MIN_EXPONENT` logarithm to contend with.
5854 pow_squeeze_t(&Rational::from(k), 0, q, prec, rm)
5855}
5856
5857/// Raises a primitive float to a primitive float power, returning a primitive float.
5858///
5859/// The result is correctly rounded to the nearest value, unlike [`f32::powf`] and [`f64::powf`],
5860/// which are not guaranteed to be correctly rounded.
5861///
5862/// $$
5863/// f(x,y) = x^y+\varepsilon.
5864/// $$
5865/// - If $x^y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
5866/// - If $x^y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x^y|\rfloor-p}$, where
5867/// $p$ is the precision of the output (typically 24 if `T` is a [`f32`] and 53 if `T` is a
5868/// [`f64`], but less if the output is subnormal).
5869///
5870/// Special cases:
5871/// - $f(x,\pm0.0)=1.0$ for any $x$, even `NaN`
5872/// - $f(1.0,y)=1.0$ for any $y$, even `NaN`
5873/// - $f(\text{NaN},y)=f(x,\text{NaN})=\text{NaN}$ otherwise
5874/// - $f(x,\infty)=\infty$ if $|x|>1$, and $0.0$ if $|x|<1$
5875/// - $f(x,-\infty)=0.0$ if $|x|>1$, and $\infty$ if $|x|<1$
5876/// - $f(-1.0,\pm\infty)=1.0$
5877/// - $f(-1.0,y)=1.0$ if $y$ is an even integer, and $-1.0$ if $y$ is an odd integer
5878/// - $f(\infty,y)=\infty$ if $y>0$, and $0.0$ if $y<0$
5879/// - $f(-\infty,y)=-\infty$ if $y$ is a positive odd integer, $\infty$ if $y$ is positive and not
5880/// an odd integer, $-0.0$ if $y$ is a negative odd integer, and $0.0$ if $y$ is negative and not
5881/// an odd integer
5882/// - $f(0.0,y)=0.0$ if $y>0$, and $\infty$ if $y<0$
5883/// - $f(-0.0,y)=-0.0$ if $y$ is a positive odd integer, $0.0$ if $y$ is positive and not an odd
5884/// integer, $-\infty$ if $y$ is a negative odd integer, and $\infty$ if $y$ is negative and not
5885/// an odd integer
5886/// - $f(x,y)=\text{NaN}$ if $x$ is finite and negative and $y$ is finite and not an integer
5887///
5888/// If the result overflows, $\pm\infty$ is returned, and if it underflows, $\pm0.0$ is returned.
5889///
5890/// # Worst-case complexity
5891/// Constant time and additional memory.
5892///
5893/// # Examples
5894/// ```
5895/// use malachite_base::num::float::NiceFloat;
5896/// use malachite_float::float::arithmetic::pow::primitive_float_pow;
5897///
5898/// assert_eq!(
5899/// NiceFloat(primitive_float_pow(3.0, 2.5)),
5900/// NiceFloat(15.588457268119896)
5901/// );
5902/// assert_eq!(
5903/// NiceFloat(primitive_float_pow(2.0, 0.5)),
5904/// NiceFloat(1.4142135623730951)
5905/// );
5906/// assert_eq!(
5907/// NiceFloat(primitive_float_pow(10.0, -0.5)),
5908/// NiceFloat(0.31622776601683794)
5909/// );
5910/// ```
5911#[allow(clippy::type_repetition_in_bounds)]
5912#[inline]
5913pub fn primitive_float_pow<T: PrimitiveFloat>(x: T, y: T) -> T
5914where
5915 Float: From<T> + PartialOrd<T>,
5916 for<'a> T: ExactFrom<&'a Float> + RoundingFrom<&'a Float>,
5917{
5918 emulate_float_float_to_float_fn(Float::pow_prec, x, y)
5919}
5920
5921/// Raises a [`Rational`] to a primitive float power, returning a primitive float.
5922///
5923/// The result is correctly rounded to the nearest value. Unlike a primitive-float base, a
5924/// [`Rational`] base may lie outside the primitive float's exponent range or so close to 1 that its
5925/// logarithm is unrepresentable; both are handled exactly, by working with the base as an exact
5926/// [`Rational`].
5927///
5928/// $$
5929/// f(x,y) = x^y+\varepsilon.
5930/// $$
5931/// - If $x^y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
5932/// - If $x^y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x^y|\rfloor-p}$, where
5933/// $p$ is the precision of the output (typically 24 if `T` is a [`f32`] and 53 if `T` is a
5934/// [`f64`], but less if the output is subnormal).
5935///
5936/// Special cases:
5937/// - $f(x,\pm0.0)=1.0$ for any $x$
5938/// - $f(1,y)=1.0$ for any $y$, even `NaN`
5939/// - $f(x,\text{NaN})=\text{NaN}$ otherwise
5940/// - $f(x,\infty)=\infty$ if $|x|>1$, and $0.0$ if $|x|<1$
5941/// - $f(x,-\infty)=0.0$ if $|x|>1$, and $\infty$ if $|x|<1$
5942/// - $f(\pm1,\pm\infty)=1.0$
5943/// - $f(-1,y)=1.0$ if $y$ is an even integer, and $-1.0$ if $y$ is an odd integer
5944/// - $f(0,y)=0.0$ if $y>0$, and $\infty$ if $y<0$; a [`Rational`] zero is unsigned, so the results
5945/// take positive signs
5946/// - $f(x,y)=\text{NaN}$ if $x<0$ and $y$ is finite and not an integer
5947///
5948/// If the result overflows, $\pm\infty$ is returned, and if it underflows, $\pm0.0$ is returned.
5949///
5950/// # Worst-case complexity
5951/// Constant time and additional memory.
5952///
5953/// # Examples
5954/// ```
5955/// use malachite_base::num::float::NiceFloat;
5956/// use malachite_float::float::arithmetic::pow::primitive_float_rational_pow;
5957/// use malachite_q::Rational;
5958///
5959/// assert_eq!(
5960/// NiceFloat(primitive_float_rational_pow(
5961/// &Rational::from_unsigneds(3u32, 2u32),
5962/// 2.5
5963/// )),
5964/// NiceFloat(2.7556759606310752)
5965/// );
5966/// assert_eq!(
5967/// NiceFloat(primitive_float_rational_pow(
5968/// &Rational::from_unsigneds(9u32, 4u32),
5969/// 0.5
5970/// )),
5971/// NiceFloat(1.5)
5972/// );
5973/// assert!(
5974/// primitive_float_rational_pow::<f64>(&-Rational::from_unsigneds(3u32, 2u32), 0.5).is_nan()
5975/// );
5976/// ```
5977#[allow(clippy::type_repetition_in_bounds)]
5978#[inline]
5979pub fn primitive_float_rational_pow<T: PrimitiveFloat>(x: &Rational, y: T) -> T
5980where
5981 Float: From<T> + PartialOrd<T>,
5982 for<'a> T: ExactFrom<&'a Float> + RoundingFrom<&'a Float>,
5983{
5984 emulate_float_to_float_fn(|y2, prec| Float::rational_pow_prec_ref_val(x, y2, prec), y)
5985}
5986
5987/// Raises a primitive float to a [`Rational`] power, returning a primitive float.
5988///
5989/// The result is correctly rounded to the nearest value. Unlike a primitive-float exponent, the
5990/// exact [`Rational`] exponent selects a definite branch of the power, so results that are exactly
5991/// representable (such as roots of perfect powers) come out exactly.
5992///
5993/// $$
5994/// f(x,y) = x^y+\varepsilon.
5995/// $$
5996/// - If $x^y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
5997/// - If $x^y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x^y|\rfloor-p}$, where
5998/// $p$ is the precision of the output (typically 24 if `T` is a [`f32`] and 53 if `T` is a
5999/// [`f64`], but less if the output is subnormal).
6000///
6001/// Special cases:
6002/// - $f(x,0)=1.0$ for any $x$, even `NaN`
6003/// - $f(1.0,y)=1.0$
6004/// - $f(\text{NaN},y)=\text{NaN}$ if $y \neq 0$
6005/// - $f(x,y)=\text{NaN}$ if $x<0$ and $y$ is not an integer
6006/// - $f(-1.0,y)=1.0$ if $y$ is an even integer, and $-1.0$ if $y$ is an odd integer
6007/// - $f(\infty,y)=\infty$ if $y>0$, and $0.0$ if $y<0$
6008/// - $f(-\infty,y)=-\infty$ if $y$ is a positive odd integer, $\infty$ if $y$ is positive and not
6009/// an odd integer, $-0.0$ if $y$ is a negative odd integer, and $0.0$ if $y$ is negative and not
6010/// an odd integer
6011/// - $f(0.0,y)=0.0$ if $y>0$, and $\infty$ if $y<0$
6012/// - $f(-0.0,y)=-0.0$ if $y$ is a positive odd integer, $0.0$ if $y$ is positive and not an odd
6013/// integer, $-\infty$ if $y$ is a negative odd integer, and $\infty$ if $y$ is negative and not
6014/// an odd integer
6015///
6016/// If the result overflows, $\pm\infty$ is returned, and if it underflows, $\pm0.0$ is returned.
6017///
6018/// # Worst-case complexity
6019/// Constant time and additional memory.
6020///
6021/// # Examples
6022/// ```
6023/// use malachite_base::num::float::NiceFloat;
6024/// use malachite_float::float::arithmetic::pow::primitive_float_pow_rational;
6025/// use malachite_q::Rational;
6026///
6027/// assert_eq!(
6028/// NiceFloat(primitive_float_pow_rational(
6029/// 4.0,
6030/// &Rational::from_signeds(1, 2)
6031/// )),
6032/// NiceFloat(2.0)
6033/// );
6034/// assert_eq!(
6035/// NiceFloat(primitive_float_pow_rational(
6036/// 2.0,
6037/// &Rational::from_signeds(3, 2)
6038/// )),
6039/// NiceFloat(2.8284271247461903)
6040/// );
6041/// assert_eq!(
6042/// NiceFloat(primitive_float_pow_rational(
6043/// 4.0,
6044/// &Rational::from_signeds(-1, 2)
6045/// )),
6046/// NiceFloat(0.5)
6047/// );
6048/// assert!(primitive_float_pow_rational::<f64>(-8.0, &Rational::from_signeds(1, 3)).is_nan());
6049/// ```
6050#[allow(clippy::type_repetition_in_bounds)]
6051#[inline]
6052pub fn primitive_float_pow_rational<T: PrimitiveFloat>(x: T, y: &Rational) -> T
6053where
6054 Float: From<T> + PartialOrd<T>,
6055 for<'a> T: ExactFrom<&'a Float> + RoundingFrom<&'a Float>,
6056{
6057 emulate_float_to_float_fn(|x, prec| Float::pow_rational_prec_val_ref(x, y, prec), x)
6058}
6059
6060/// Raises a primitive float to the power of an [`Integer`], returning a primitive float.
6061///
6062/// The result is correctly rounded to the nearest value. Unlike a primitive-float exponent, an
6063/// arbitrarily large [`Integer`] exponent is handled exactly.
6064///
6065/// $$
6066/// f(x,n) = x^n+\varepsilon.
6067/// $$
6068/// - If $x^n$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
6069/// - If $x^n$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x^n|\rfloor-p}$, where
6070/// $p$ is the precision of the output (typically 24 if `T` is a [`f32`] and 53 if `T` is a
6071/// [`f64`], but less if the output is subnormal).
6072///
6073/// Special cases:
6074/// - $f(x,0)=1.0$ for any $x$, even `NaN`
6075/// - $f(1,n)=1.0$
6076/// - $f(\text{NaN},n)=\text{NaN}$ if $n \neq 0$
6077/// - $f(-1,n)=1.0$ if $n$ is even, and $-1.0$ if $n$ is odd
6078/// - $f(\infty,n)=\infty$ if $n>0$, and $0.0$ if $n<0$
6079/// - $f(-\infty,n)=-\infty$ if $n$ is positive and odd, $\infty$ if $n$ is positive and even,
6080/// $-0.0$ if $n$ is negative and odd, and $0.0$ if $n$ is negative and even
6081/// - $f(0.0,n)=0.0$ if $n>0$, and $\infty$ if $n<0$
6082/// - $f(-0.0,n)=-0.0$ if $n$ is positive and odd, $0.0$ if $n$ is positive and even, $-\infty$ if
6083/// $n$ is negative and odd, and $\infty$ if $n$ is negative and even
6084///
6085/// If the result overflows, $\pm\infty$ is returned, and if it underflows, $\pm0.0$ is returned.
6086///
6087/// # Worst-case complexity
6088/// Constant time and additional memory.
6089///
6090/// # Examples
6091/// ```
6092/// use malachite_base::num::float::NiceFloat;
6093/// use malachite_float::float::arithmetic::pow::primitive_float_pow_integer;
6094/// use malachite_nz::integer::Integer;
6095///
6096/// assert_eq!(
6097/// NiceFloat(primitive_float_pow_integer(3.0, &Integer::from(5))),
6098/// NiceFloat(243.0)
6099/// );
6100/// assert_eq!(
6101/// NiceFloat(primitive_float_pow_integer(2.0, &Integer::from(-3))),
6102/// NiceFloat(0.125)
6103/// );
6104/// assert_eq!(
6105/// NiceFloat(primitive_float_pow_integer(-2.0, &Integer::from(3))),
6106/// NiceFloat(-8.0)
6107/// );
6108/// ```
6109#[allow(clippy::type_repetition_in_bounds)]
6110#[inline]
6111pub fn primitive_float_pow_integer<T: PrimitiveFloat>(x: T, y: &Integer) -> T
6112where
6113 Float: From<T> + PartialOrd<T>,
6114 for<'a> T: ExactFrom<&'a Float> + RoundingFrom<&'a Float>,
6115{
6116 emulate_float_to_float_fn(|x, prec| Float::pow_integer_prec_val_ref(x, y, prec), x)
6117}
6118
6119/// Raises a primitive float to the power of a [`u64`], returning a primitive float.
6120///
6121/// The result is correctly rounded to the nearest value.
6122///
6123/// $$
6124/// f(x,n) = x^n+\varepsilon.
6125/// $$
6126/// - If $x^n$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
6127/// - If $x^n$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x^n|\rfloor-p}$, where
6128/// $p$ is the precision of the output (typically 24 if `T` is a [`f32`] and 53 if `T` is a
6129/// [`f64`], but less if the output is subnormal).
6130///
6131/// Special cases:
6132/// - $f(x,0)=1.0$ for any $x$, even `NaN`
6133/// - $f(1.0,n)=1.0$
6134/// - $f(\text{NaN},n)=\text{NaN}$ if $n \neq 0$
6135/// - $f(-1.0,n)=1.0$ if $n$ is even, and $-1.0$ if $n$ is odd
6136/// - $f(\infty,n)=\infty$ if $n>0$
6137/// - $f(-\infty,n)=\infty$ if $n$ is positive and even, and $-\infty$ if $n$ is odd
6138/// - $f(0.0,n)=0.0$ if $n>0$
6139/// - $f(-0.0,n)=0.0$ if $n$ is positive and even, and $-0.0$ if $n$ is odd
6140///
6141/// If the result overflows, $\pm\infty$ is returned, and if it underflows, $\pm0.0$ is returned.
6142///
6143/// # Worst-case complexity
6144/// Constant time and additional memory.
6145///
6146/// # Examples
6147/// ```
6148/// use malachite_base::num::float::NiceFloat;
6149/// use malachite_float::float::arithmetic::pow::primitive_float_pow_u;
6150///
6151/// assert_eq!(NiceFloat(primitive_float_pow_u(3.0, 5)), NiceFloat(243.0));
6152/// assert_eq!(NiceFloat(primitive_float_pow_u(2.0, 10)), NiceFloat(1024.0));
6153/// assert_eq!(NiceFloat(primitive_float_pow_u(-2.0, 3)), NiceFloat(-8.0));
6154/// ```
6155#[allow(clippy::type_repetition_in_bounds)]
6156#[inline]
6157pub fn primitive_float_pow_u<T: PrimitiveFloat>(x: T, n: u64) -> T
6158where
6159 Float: From<T> + PartialOrd<T>,
6160 for<'a> T: ExactFrom<&'a Float> + RoundingFrom<&'a Float>,
6161{
6162 emulate_float_to_float_fn(|x, prec| x.pow_u_prec(n, prec), x)
6163}
6164
6165/// Raises a [`u64`] to the power of a primitive float, returning a primitive float.
6166///
6167/// The result is correctly rounded to the nearest value.
6168///
6169/// $$
6170/// f(x,y) = x^y+\varepsilon.
6171/// $$
6172/// - If $x^y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
6173/// - If $x^y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 x^y\rfloor-p}$, where
6174/// $p$ is the precision of the output (typically 24 if `T` is a [`f32`] and 53 if `T` is a
6175/// [`f64`], but less if the output is subnormal).
6176///
6177/// Special cases:
6178/// - $f(x,0.0)=1.0$ for any $x$
6179/// - $f(1,y)=1.0$ for any $y$, even `NaN`
6180/// - $f(x,\text{NaN})=\text{NaN}$ if $x \neq 1$
6181/// - $f(x,\infty)=\infty$ if $x>1$, and $0.0$ if $x=0$
6182/// - $f(x,-\infty)=0.0$ if $x>1$, and $\infty$ if $x=0$
6183/// - $f(0,y)=0.0$ if $y>0$, and $\infty$ if $y<0$
6184///
6185/// If the result overflows, $\infty$ is returned, and if it underflows, $0.0$ is returned.
6186///
6187/// # Worst-case complexity
6188/// Constant time and additional memory.
6189///
6190/// # Examples
6191/// ```
6192/// use malachite_base::num::float::NiceFloat;
6193/// use malachite_float::float::arithmetic::pow::primitive_float_unsigned_pow;
6194///
6195/// assert_eq!(
6196/// NiceFloat(primitive_float_unsigned_pow(2, 0.5)),
6197/// NiceFloat(1.4142135623730951)
6198/// );
6199/// assert_eq!(
6200/// NiceFloat(primitive_float_unsigned_pow(3, 2.5)),
6201/// NiceFloat(15.588457268119896)
6202/// );
6203/// assert_eq!(
6204/// NiceFloat(primitive_float_unsigned_pow(2, -1.0)),
6205/// NiceFloat(0.5)
6206/// );
6207/// ```
6208#[allow(clippy::type_repetition_in_bounds)]
6209#[inline]
6210pub fn primitive_float_unsigned_pow<T: PrimitiveFloat>(x: u64, y: T) -> T
6211where
6212 Float: From<T> + PartialOrd<T>,
6213 for<'a> T: ExactFrom<&'a Float> + RoundingFrom<&'a Float>,
6214{
6215 emulate_float_to_float_fn(|y2, prec| Float::unsigned_pow_prec(x, y2, prec), y)
6216}
6217
6218// Brackets of ln(1 + e) for an exact nonzero Rational e with |e| < 1/2, as exact Rationals, to a
6219// relative accuracy of about 2^-wprec. Uses the atanh series ln(1 + e) = 2 atanh(u) with u = e / (2
6220// + e) and |u| < 1/3: atanh(u) = sum_{k>=0} u^(2k+1)/(2k+1), whose tail after the term in u^(2k+1)
6221// is bounded in magnitude by that term times u^2 / (1 - u^2) < that term * 9/8. The partial sum and
6222// the tail both have the sign of e, so the exact value lies between the partial sum and (partial
6223// sum + tail). Splits a positive Rational x as x' * 2^g with g the nearest integer to log2(x) and
6224// x' in [1/sqrt(2), sqrt(2)), so that x' is close to 1 (never near 2, where a Float log would
6225// collapse).
6226fn rational_mantissa_nearest_power_of_2(x: &Rational) -> (Rational, i64) {
6227 let fl = x.floor_log_base_2_abs();
6228 let mant = x >> fl;
6229 let g = if (&mant).square() < 2u32 { fl } else { fl + 1 };
6230 (x >> g, g)
6231}
6232
6233// Whether x^y is a dyadic rational (and therefore possibly exactly representable), for a positive
6234// non-power-of-2 Rational x = (a / b) * 2^e with a, b odd and coprime, and a finite nonzero
6235// non-singular Float y = c * 2^d with c an odd Integer. If so, returns (m, z, pow) such that x^y =
6236// m^z * 2^pow with m an odd Natural and z a positive Integer; otherwise returns None. Since x is
6237// not a power of 2, a Ziv-style squeeze on an exact x^y would never terminate, and a nearest-mode
6238// tie is possible only in the dyadic case, so this decides when the direct route is required.
6239fn rational_pow_exact_decomposition(
6240 a: &Natural,
6241 b: &Natural,
6242 e: i64,
6243 y: &Float,
6244) -> Option<(Natural, Integer, Integer)> {
6245 let (c, d) = float_to_odd_mantissa_and_exponent(y);
6246 let (mut a, mut b) = (a.clone(), b.clone());
6247 let mut e = Integer::from(e);
6248 // Descend the negative powers of 2 in the exponent: x must be a perfect 2^|d|-th power.
6249 if d < 0 {
6250 for _ in 0..-d {
6251 if a != 1u32 {
6252 a = a.checked_sqrt()?;
6253 }
6254 if b != 1u32 {
6255 b = b.checked_sqrt()?;
6256 }
6257 if e.odd() {
6258 return None;
6259 }
6260 e >>= 1;
6261 }
6262 } else {
6263 e <<= d;
6264 }
6265 // Now x^y = (a / b)^(c * 2^max(d, 0)) * 2^(e * c), with the power of 2 in the exponent already
6266 // scaled into e. Dyadic requires the denominator (after accounting for c's sign) to be 1.
6267 let pow = e * &c;
6268 let m = if c > 0u32 {
6269 if b != 1u32 {
6270 return None;
6271 }
6272 a
6273 } else {
6274 if a != 1u32 {
6275 return None;
6276 }
6277 b
6278 };
6279 let mut z = Integer::from(c.unsigned_abs());
6280 if d > 0 {
6281 z <<= d;
6282 }
6283 Some((m, z, pow))
6284}
6285
6286// The in-range squeeze: x is positive, not a dyadic rational, and comfortably within the Float
6287// exponent range, and y is finite, nonzero, and not a small integer. Brackets x between dyadic
6288// Floats at growing precision and applies `Float::pow` to both ends, tightening until both ends
6289// round identically. Since x has an odd prime factor in its denominator, x^y is never exactly
6290// representable and never a nearest-mode tie, so the squeeze terminates.
6291fn rational_pow_squeeze_x(
6292 x: &Rational,
6293 y: &Float,
6294 prec: u64,
6295 rm: RoundingMode,
6296) -> (Float, Ordering) {
6297 let mut wprec = prec.saturating_add(TWICE_WIDTH);
6298 let mut increment = Limb::WIDTH;
6299 loop {
6300 let x_lo = Float::from_rational_prec_round_ref(x, wprec, Floor).0;
6301 let x_hi = Float::from_rational_prec_round_ref(x, wprec, Ceiling).0;
6302 let (p_lo, mut o_lo) = x_lo.pow_prec_round_val_ref(y, prec, rm);
6303 let (p_hi, mut o_hi) = x_hi.pow_prec_round_val_ref(y, prec, rm);
6304 // A bracket end that lands exactly on a representable power rounds with `Equal`; the true
6305 // value lies strictly between the ends, so the other end's ordering is the true one.
6306 if o_lo == Equal {
6307 o_lo = o_hi;
6308 }
6309 if o_hi == Equal {
6310 o_hi = o_lo;
6311 }
6312 // `x` is positive, so `Float::pow` yields a positive value at precision `prec` (or `+inf`
6313 // on overflow, `+0.0` on underflow), never `NaN` or `-0.0`, and a plain value comparison
6314 // suffices.
6315 if o_lo == o_hi && p_lo == p_hi {
6316 return (p_lo, o_lo);
6317 }
6318 wprec += increment;
6319 increment = wprec >> 1;
6320 }
6321}
6322
6323// The shared rational-exponent squeeze: computes (x' * 2^e)^y for an exact Rational x' whose binary
6324// logarithm `log_2_rational_brackets` can bracket, an integer e, and an exact Rational exponent y
6325// (finite and nonzero), assuming the true result is irrational. Brackets t = y * (e + log2(x'))
6326// between exact Rationals -- Rationals have no exponent range, so no underflow or overflow can
6327// occur here -- and applies `Float::power_of_2_rational_prec_round` to both ends, which itself
6328// handles results at or beyond the exponent boundaries, growing the working precision until the
6329// ends agree. `rational_pow` reaches this in its extreme regime with x' in [1/sqrt(2), sqrt(2));
6330// `unsigned_pow_rational` reaches it with x' = k and e = 0. Growth past the initial precision is
6331// rare but constructible: 6^(1 + 2^-300) lies within 2^-300 of the rounding boundary 6.0, so the
6332// first bracket straddles it at any target precision below ~300. Fast path for `k ^ q` when the
6333// result is extremely close to 1 (`q` so tiny that `k ^ q = exp(q * ln k)` differs from 1 by at
6334// most a handful of ulps). The general squeeze in `pow_squeeze_t` evaluates `log2(k)` to about
6335// `prec` bits, which is wasteful here; instead bracket `ln(k)` between two `Rational`s from a
6336// single modest-precision `ln(k)` and apply `exp_rational_near_one` to the tiny products `q *
6337// ln(k)`. Returns `None` when the result is not close enough to 1 for this to help (the caller then
6338// squeezes). Mirrors `power_of_2_rational_near_one`, replacing the constant `ln(2)` with `ln(k)`.
6339// `k >= 2` and `q` is a nonzero non-integer, so `k ^ q` is irrational.
6340fn unsigned_pow_rational_near_one(
6341 k: u64,
6342 q: &Rational,
6343 prec: u64,
6344 rm: RoundingMode,
6345) -> Option<(Float, Ordering)> {
6346 // `2 ^ ql <= |q| < 2 ^ (ql + 1)` and `log2(k) < kb <= 2 ^ kbb` (kbb the bit length of kb), so
6347 // `|q * log2(k)| < 2 ^ (ql + 1 + kbb)`. Take this path only when that bound puts `k ^ q` within
6348 // roughly a machine word's worth of ulps of 1: then `exp_rational_near_one` converges in O(1)
6349 // terms and `ln(k)` is needed to only about `prec + t_exp_ub` bits. The `t_exp_ub >= 0` guard
6350 // also keeps `|q * ln(k)| < 1`, which `exp_rational_near_one` requires.
6351 let ql = q.floor_log_base_2_abs();
6352 let kbb = i64::exact_from(k.significant_bits().significant_bits());
6353 let t_exp_ub = ql + 1 + kbb;
6354 if t_exp_ub >= 0 || t_exp_ub > -i64::exact_from(prec) + i64::exact_from(Limb::WIDTH) {
6355 return None;
6356 }
6357 // `k > 1`, so `k ^ q > 1` exactly when `q > 0`. Because `q * ln(k)` is tiny, `ln(k)` needs only
6358 // about `prec + t_exp_ub` bits to separate the two exp brackets at the target precision -- far
6359 // below `prec`. Start a little above that and let the Ziv loop grow it.
6360 let above = *q > 0u32;
6361 let mut working_prec = u64::saturating_from(i64::exact_from(prec) + t_exp_ub) + Limb::WIDTH;
6362 let mut increment = Limb::WIDTH;
6363 let kf = Float::from(k);
6364 loop {
6365 // `ln_k_lo <= ln(k) <= ln_k_hi`, as exact Rationals, from a single `ln(k)` computation.
6366 let (ln_k_lo, ln_k_hi) = floor_and_ceiling(kf.ln_prec_round_ref(working_prec, Floor));
6367 let ln_k_lo = Rational::exact_from(&ln_k_lo);
6368 let ln_k_hi = Rational::exact_from(&ln_k_hi);
6369 // `q * ln(k)` lies between these two products (which end is smaller depends on the sign of
6370 // `q`), and exp is increasing, so `k ^ q` lies between the exps of the two products.
6371 let (p_lo, p_hi) = if above {
6372 (q * ln_k_lo, q * ln_k_hi)
6373 } else {
6374 (q * ln_k_hi, q * ln_k_lo)
6375 };
6376 let (lo, o_lo) = exp_rational_near_one(&p_lo, prec, rm);
6377 let (hi, o_hi) = exp_rational_near_one(&p_hi, prec, rm);
6378 if o_lo == o_hi && lo == hi {
6379 return Some((lo, o_lo));
6380 }
6381 working_prec += increment;
6382 increment = working_prec >> 1;
6383 }
6384}
6385
6386fn pow_squeeze_t(
6387 xp: &Rational,
6388 e: i64,
6389 y: &Rational,
6390 prec: u64,
6391 rm: RoundingMode,
6392) -> (Float, Ordering) {
6393 let er = Rational::from(e);
6394 let mut wprec = prec.saturating_add(TWICE_WIDTH);
6395 let mut increment = Limb::WIDTH;
6396 loop {
6397 let (l_lo, l_hi) = log_2_rational_brackets(xp, wprec);
6398 let (t_lo, t_hi) = if *y > 0u32 {
6399 (y * (&er + l_lo), y * (&er + l_hi))
6400 } else {
6401 (y * (&er + l_hi), y * (&er + l_lo))
6402 };
6403 let (p_lo, mut o_lo) = Float::power_of_2_rational_prec_round(t_lo, prec, rm);
6404 let (p_hi, mut o_hi) = Float::power_of_2_rational_prec_round(t_hi, prec, rm);
6405 // A bracket end landing exactly on a representable power rounds with `Equal`; the true
6406 // value lies strictly between the ends, so the other end's ordering is the true one.
6407 if o_lo == Equal {
6408 fail_on_untested_path(
6409 "pow_squeeze_t, lo_eq: exact results (t an integer) are caught by each caller's \
6410 exact decomposition before the squeeze, so t is never an integer here; a bracket \
6411 end equalling an integer is a measure-zero coincidence of the log brackets",
6412 );
6413 o_lo = o_hi;
6414 }
6415 if o_hi == Equal {
6416 fail_on_untested_path(
6417 "pow_squeeze_t, hi_eq: as lo_eq -- t is never an integer in the squeeze, so a \
6418 bracket end equalling one is a measure-zero coincidence",
6419 );
6420 o_hi = o_lo;
6421 }
6422 // `power_of_2_rational_prec_round` yields a positive value at precision `prec` (or `+inf`
6423 // on overflow, `+0.0` on underflow), never `NaN` or `-0.0`, so a plain value comparison
6424 // suffices -- no need for `ComparableFloatRef` to force equal precisions or to make `NaN`s
6425 // compare equal.
6426 if o_lo == o_hi && p_lo == p_hi {
6427 return (p_lo, o_lo);
6428 }
6429 wprec += increment;
6430 increment = wprec >> 1;
6431 }
6432}
6433
6434// The exact-dyadic route: x^y = m^z * 2^pow with m odd. If the result's odd part is small enough to
6435// affect prec-bit rounding (or to be a nearest-mode tie), materialize it; otherwise the value is
6436// neither representable nor a tie and the caller may squeeze safely.
6437fn rational_pow_exact(
6438 m: &Natural,
6439 z: &Integer,
6440 pow: &Integer,
6441 prec: u64,
6442 rm: RoundingMode,
6443) -> Option<(Float, Ordering)> {
6444 let zu = u64::try_from(z).ok()?;
6445 // The rejection must use a *lower* bound on the significant bits of m^z: returning `None`
6446 // asserts that the result is neither representable at `prec` nor a `Nearest` tie (both need at
6447 // most prec + 2 significant bits), and the caller then squeezes -- which never terminates on a
6448 // representable value or a tie. Since m >= 2^(sb(m) - 1), m^z >= 2^(z * (sb(m) - 1)), so
6449 // sb(m^z) >= z * (sb(m) - 1) + 1. (An upper bound like z * sb(m) is unsound here: it
6450 // overestimates sb(m^z) by up to z - 1 bits, letting exactly-representable results and ties
6451 // leak into the squeeze.) The materialization below stays cheap: the caller has peeled
6452 // power-of-2 bases, so m is odd and m >= 3, hence sb(m) >= 2 and any admitted z satisfies z <=
6453 // z * (sb(m) - 1) <= prec + 1, giving sb(m^z) <= z * sb(m) <= 2 * prec + 2.
6454 debug_assert!(*m > 1u32 && m.odd());
6455 let bits_lower = (m.significant_bits() - 1).checked_mul(zu)?.checked_add(1)?;
6456 if bits_lower > prec + 2 {
6457 return None;
6458 }
6459 let value = m.clone().pow(zu);
6460 let (result, o) = Float::from_natural_prec_round(value, prec, rm);
6461 // Scale by 2^pow. An exponent beyond i64 with a prec-bit odd part is a definite overflow or
6462 // underflow.
6463 let Ok(shift) = i64::try_from(pow) else {
6464 return Some(if *pow > 0u32 {
6465 fail_on_untested_path(
6466 "rational_pow, ex_pow_overflow: reachable only with a base whose 2-adic \
6467 valuation exceeds i64::MAX / prec while its odd part fits in prec + 2 bits -- \
6468 simultaneously a ~512-MB base and a ~2^31 precision, beyond practical test \
6469 sizes",
6470 );
6471 exp_overflow(prec, rm)
6472 } else {
6473 fail_on_untested_path(
6474 "rational_pow, ex_pow_underflow: as ex_pow_overflow, in the negative-exponent \
6475 direction",
6476 );
6477 exp_underflow(prec, if rm == Nearest { Down } else { rm })
6478 });
6479 };
6480 let (shifted, oo) = result.shl_prec_round(shift, prec, rm);
6481 Some((shifted, if oo == Equal { o } else { oo }))
6482}
6483
6484// Whether the Rational y is an odd integer.
6485fn rational_odd_integer(y: &Rational) -> bool {
6486 *y.denominator_ref() == 1u32 && y.numerator_ref().odd()
6487}
6488
6489// Raises a finite, positive Float x to the power of a finite, nonzero, non-integer Rational y = a /
6490// b (in lowest terms, so b >= 2), returning the result rounded to `prec` bits with `rm`.
6491fn positive_float_pow_rational(
6492 x: &Float,
6493 y: &Rational,
6494 prec: u64,
6495 rm: RoundingMode,
6496) -> (Float, Ordering) {
6497 // x = c * 2^d with c odd (c >= 1).
6498 let (c, d) = float_to_odd_mantissa_and_exponent_natural(x);
6499 // x = 2^d: x^y = 2^(d * y), an exact-Rational exponent that `power_of_2_rational_prec_round`
6500 // handles completely (exactness, overflow, and underflow).
6501 if c == 1u32 {
6502 return Float::power_of_2_rational_prec_round(Rational::from(d) * y, prec, rm);
6503 }
6504 // x^(a/b) is rational exactly when x is a perfect b-th power of a Float, i.e. b | d and the odd
6505 // part c is a perfect b-th power j^b. Then x^(1/b) = j * 2^(d/b) is an exact Float `base`, and
6506 // x^(a/b) = base^a is delegated to `pow_integer`, which correctly rounds the (possibly
6507 // non-dyadic, for a < 0) result and handles overflow and underflow. Otherwise x^(a/b) is
6508 // irrational.
6509 if let Ok(b) = u64::try_from(y.denominator_ref())
6510 && d.unsigned_abs().divisible_by(b)
6511 && let Some(j) = (&c).checked_root(b)
6512 {
6513 let base = Float::exact_from(j) << (d / i64::exact_from(b));
6514 let a = Integer::from_sign_and_abs_ref(*y > 0u32, y.numerator_ref());
6515 return base.pow_integer_prec_round(a, prec, rm);
6516 }
6517 // The result is irrational. First a tiny-result shortcut: if |y * log2(x)| is far below 1, then
6518 // x^y rounds to 1 +/- ulp, sparing the (possibly huge) log2 bracketing. Since |y| < 2^ey and
6519 // |log2(x)| < 2^expb, one has |y * log2(x)| < 2^(ey + expb).
6520 let ex = i64::from(x.get_exponent().unwrap());
6521 let ey = y.floor_log_base_2_abs() + 1;
6522 let above = (*y > 0u32) == (*x > 1u32);
6523 let expb = if ex == 0 || ex == 1 {
6524 // x is in (1/2, 2), close to 1 (and x != 1, since |x| = 1 was handled by the caller): with
6525 // fld = floor(log2|x - 1|), one has |log2(x)| < 2^(fld + 2).
6526 (Rational::exact_from(x) - Rational::ONE).floor_log_base_2_abs() + 2
6527 } else {
6528 // x is bounded away from 1: |log2(x)| <= expx = max(ex, 1 - ex) < 2^ceil(log2(expx)).
6529 let expx = if ex > 1 { ex } else { 1 - ex };
6530 i64::exact_from(u64::exact_from(expx).ceiling_log_base_2())
6531 };
6532 if ey + expb < -i64::exact_from(prec) - 1 {
6533 return float_one_plus_tiny(prec, rm, above);
6534 }
6535 // General squeeze: bracket log2(x) = d + log2(c) between exact Rationals and apply 2^(y * (d +
6536 // log2(c))). Working in the exponent (t-space) stays correct even when x is a sliver of 1,
6537 // where a Float-based log2(x) would underflow below the smallest positive Float.
6538 pow_squeeze_t(&Rational::from(c), d, y, prec, rm)
6539}
6540
6541// Raises a Float to the power of a Rational, returning a Float rounded to `prec` bits with `rm`.
6542fn float_rational_pow(x: &Float, y: &Rational, prec: u64, rm: RoundingMode) -> (Float, Ordering) {
6543 assert_ne!(prec, 0);
6544 // Exact rounding: compute with Nearest and demand exactness.
6545 if rm == Exact {
6546 let (result, o) = float_rational_pow(x, y, prec, Nearest);
6547 assert_eq!(o, Equal, "Inexact pow");
6548 return (result, Equal);
6549 }
6550 // x^0 = 1 for any x, even NaN.
6551 if *y == 0u32 {
6552 return (Float::one_prec(prec), Equal);
6553 }
6554 // Singular x; see Section F.9.4.4 of the C standard. y is a finite nonzero Rational, so the
6555 // singular-y cases (0, NaN, +/-Inf) do not arise.
6556 match x {
6557 float_nan!() => return (Float::NAN, Equal),
6558 Float(Infinity { sign }) => {
6559 let negative = !*sign && rational_odd_integer(y);
6560 return (
6561 match (*y > 0u32, negative) {
6562 (true, false) => Float::INFINITY,
6563 (true, true) => Float::NEGATIVE_INFINITY,
6564 (false, false) => Float::ZERO,
6565 (false, true) => Float::NEGATIVE_ZERO,
6566 },
6567 Equal,
6568 );
6569 }
6570 Float(Zero { sign }) => {
6571 let negative = !*sign && rational_odd_integer(y);
6572 return (
6573 match (*y < 0u32, negative) {
6574 (true, false) => Float::INFINITY,
6575 (true, true) => Float::NEGATIVE_INFINITY,
6576 (false, false) => Float::ZERO,
6577 (false, true) => Float::NEGATIVE_ZERO,
6578 },
6579 Equal,
6580 );
6581 }
6582 _ => {}
6583 }
6584 // x finite and nonzero.
6585 let y_is_integer = *y.denominator_ref() == 1u32;
6586 // x^y for x < 0 and y not an integer is not defined.
6587 if x.is_sign_negative() && !y_is_integer {
6588 return (Float::NAN, Equal);
6589 }
6590 // |x| = 1: (+/-1)^y = +/-1 (the sign is negative only for x = -1 and odd y).
6591 if x.partial_cmp_abs(&Float::ONE).unwrap() == Equal {
6592 let negative = x.is_sign_negative() && rational_odd_integer(y);
6593 return Float::from_float_prec_round(
6594 if negative { -Float::ONE } else { Float::ONE },
6595 prec,
6596 rm,
6597 );
6598 }
6599 // Integer y: the multiplication-based `pow_integer` handles negative x (via parity), overflow,
6600 // and underflow.
6601 if y_is_integer {
6602 return pow_integer(x, &Integer::rounding_from(y, Exact).0, prec, rm);
6603 }
6604 // x > 0 (negative x with non-integer y was rejected above), y = a / b with b >= 2.
6605 positive_float_pow_rational(x, y, prec, rm)
6606}
6607
6608// Whether x^y is a dyadic rational (hence possibly exactly representable), for a positive
6609// non-power-of-2 Rational x = (a / b) * 2^e (a, b odd and coprime) and a finite nonzero non-integer
6610// Rational y = a_y / b_y (in lowest terms, b_y >= 2). If so, returns (m, z, pow) such that x^y =
6611// m^z * 2^pow with m an odd Natural (> 1) and z a positive Integer; otherwise returns None. Since x
6612// is not a power of 2, a Ziv-style squeeze on an exact x^y would never terminate, and a
6613// nearest-mode tie is possible only in the dyadic case, so this decides when the direct route is
6614// required.
6615fn rational_rational_pow_exact_decomposition(
6616 a: &Natural,
6617 b: &Natural,
6618 e: i64,
6619 y: &Rational,
6620) -> Option<(Natural, Integer, Integer)> {
6621 let b_y = u64::try_from(y.denominator_ref()).ok()?;
6622 // 2^(e * a_y / b_y) is dyadic exactly when b_y | e (since gcd(a_y, b_y) = 1).
6623 if !e.unsigned_abs().divisible_by(b_y) {
6624 return None;
6625 }
6626 // (a / b)^(a_y / b_y) is dyadic only if a and b are each perfect b_y-th powers.
6627 let p = a.checked_root(b_y)?;
6628 let q = b.checked_root(b_y)?;
6629 let a_y_abs = y.numerator_ref();
6630 // pow = e * a_y / b_y = (e / b_y) * a_y, an exact integer.
6631 let pow = Integer::from(e / i64::exact_from(b_y))
6632 * Integer::from_sign_and_abs_ref(*y > 0u32, a_y_abs);
6633 if *y > 0u32 {
6634 // p^a_y / q^a_y is dyadic (q odd) only when q = 1, i.e. b = 1. Then m = p (> 1, since x is
6635 // not a power of 2, so a > 1 here).
6636 if q != 1u32 {
6637 return None;
6638 }
6639 Some((p, Integer::from(a_y_abs), pow))
6640 } else {
6641 // q^|a_y| / p^|a_y| is dyadic only when p = 1, i.e. a = 1. Then m = q (> 1).
6642 if p != 1u32 {
6643 return None;
6644 }
6645 Some((q, Integer::from(a_y_abs), pow))
6646 }
6647}
6648
6649// Raises a Rational to a Rational power, returning a Float rounded to `prec` bits with `rm`.
6650fn rational_rational_pow(
6651 x: &Rational,
6652 y: &Rational,
6653 prec: u64,
6654 rm: RoundingMode,
6655) -> (Float, Ordering) {
6656 assert_ne!(prec, 0);
6657 // Exact rounding: compute with Nearest and demand exactness.
6658 if rm == Exact {
6659 let (result, o) = rational_rational_pow(x, y, prec, Nearest);
6660 assert_eq!(o, Equal, "Inexact rational_rational_pow");
6661 return (result, Equal);
6662 }
6663 // x^0 = 1 for any x, even 0.
6664 if *y == 0u32 {
6665 return (Float::one_prec(prec), Equal);
6666 }
6667 // x = 0: a Rational zero is unsigned, so the results take positive signs.
6668 if *x == 0u32 {
6669 return if *y > 0u32 {
6670 (Float::ZERO, Equal)
6671 } else {
6672 (Float::INFINITY, Equal)
6673 };
6674 }
6675 let y_is_integer = *y.denominator_ref() == 1u32;
6676 // Negative x: only an integer y is defined; the sign is that of (-1)^y.
6677 if *x < 0u32 {
6678 if !y_is_integer {
6679 return (Float::NAN, Equal);
6680 }
6681 let negative = rational_odd_integer(y);
6682 let (result, o) = rational_rational_pow(&(-x), y, prec, if negative { -rm } else { rm });
6683 return if negative {
6684 (-result, o.reverse())
6685 } else {
6686 (result, o)
6687 };
6688 }
6689 if *x == 1u32 {
6690 return (Float::one_prec(prec), Equal);
6691 }
6692 // x = 2^e exactly: x^y = 2^(e * y) with e * y an exact Rational;
6693 // `power_of_2_rational_prec_round` handles all exactness, overflow, and underflow.
6694 if let Some(e) = x.checked_log_base_2() {
6695 let t = Rational::from(e) * y;
6696 return Float::power_of_2_rational_prec_round(t, prec, rm);
6697 }
6698 // Small integer y with a small base: materialize x^y as an exact Rational;
6699 // `from_rational_prec_round` handles all rounding, including at the range boundaries.
6700 let nbits = x.significant_bits();
6701 if y_is_integer
6702 && let Ok(z) = i64::try_from(y.numerator_ref())
6703 && z.unsigned_abs().saturating_mul(nbits) <= max(65536, prec << 2)
6704 {
6705 let z = if *y > 0u32 { z } else { -z };
6706 return Float::from_rational_prec_round(x.pow(z), prec, rm);
6707 }
6708 let fl = x.floor_log_base_2_abs();
6709 let in_range =
6710 fl > i64::from(Float::MIN_EXPONENT) + 2 && fl < i64::from(Float::MAX_EXPONENT) - 2;
6711 // A base within a few binades of 1 is a sliver whose logarithm is at or below the smallest
6712 // positive Float; it must go through the exact-Rational t-space squeeze (which brackets log2
6713 // over Rationals) rather than any Float-based route, which would underflow the logarithm. `x`
6714 // is a sliver only when it lies in `(1/2, 2)`, i.e. `fl` is 0 or -1.
6715 let sliver_fld = if fl == 0 || fl == -1 {
6716 Some((x - Rational::ONE).floor_log_base_2_abs())
6717 } else {
6718 None
6719 };
6720 let sliver_of_one = sliver_fld.is_some_and(|fld| fld < i64::from(Float::MIN_EXPONENT) + 8);
6721 // A dyadic in-range non-sliver base is exactly convertible to a Float; `Float::pow_rational`
6722 // does the rest, exactness and boundary behavior included.
6723 if in_range && !sliver_of_one && x.denominator_ref().is_power_of_2() {
6724 let xf = Float::from_rational_prec_round_ref(x, nbits, Floor).0;
6725 return xf.pow_rational_prec_round_val_ref(y, prec, rm);
6726 }
6727 // Possible exact dyadic results must be handled directly: a Ziv squeeze never terminates on an
6728 // exactly-representable value and can stall on a nearest-mode tie.
6729 let n = x.numerator_ref();
6730 let d = x.denominator_ref();
6731 let alpha = i64::exact_from(n.trailing_zeros().unwrap());
6732 let beta = i64::exact_from(d.trailing_zeros().unwrap());
6733 let a = n >> alpha;
6734 let b = d >> beta;
6735 if let Some((m, z, pow)) = rational_rational_pow_exact_decomposition(&a, &b, alpha - beta, y)
6736 && let Some(result) = rational_pow_exact(&m, &z, &pow, prec, rm)
6737 {
6738 return result;
6739 }
6740 // Tiny-result shortcut for a sliver of 1: if |y * log2(x)| is far below 1, x^y rounds to 1 +/-
6741 // ulp, avoiding the (up to 128-MB) log2 brackets. With fld = floor_log2|x - 1|, one has
6742 // |log2(x)| < 2^(fld + 2), so |y * log2(x)| < 2^(ey + fld + 2).
6743 if let Some(fld) = sliver_fld {
6744 let ey = y.floor_log_base_2_abs() + 1;
6745 if ey + fld + 2 < -i64::exact_from(prec) - 1 {
6746 let above = (*y > 0u32) == (*x > 1u32);
6747 return float_one_plus_tiny(prec, rm, above);
6748 }
6749 }
6750 // The result is irrational (or a non-dyadic rational): squeeze 2^(y * log2(x)) in the exponent
6751 // (t-space) over exact Rationals. Splitting off the odd part keeps the log2 bracketing exact
6752 // for extreme or sliver bases, where a Float logarithm would underflow.
6753 let xp = Rational::from(a) / Rational::from(b);
6754 pow_squeeze_t(&xp, alpha - beta, y, prec, rm)
6755}
6756
6757impl Float {
6758 /// Raises a [`Rational`] to a [`Rational`] power, returning the result as a [`Float`] rounded
6759 /// to the specified precision and with the specified rounding mode. Both [`Rational`]s are
6760 /// taken by value. An [`Ordering`] is also returned, indicating whether the rounded power is
6761 /// less than, equal to, or greater than the exact power. Although `NaN`s are not comparable to
6762 /// any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
6763 ///
6764 /// See [`RoundingMode`] for a description of the possible rounding modes.
6765 ///
6766 /// $$
6767 /// f(x,y,p,m) = x^y+\varepsilon.
6768 /// $$
6769 /// - If $x^y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
6770 /// - If $x^y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
6771 /// 2^{\lfloor\log_2 |x^y|\rfloor-p+1}$.
6772 /// - If $x^y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
6773 /// 2^{\lfloor\log_2 |x^y|\rfloor-p}$.
6774 ///
6775 /// If the output has a precision, it is `prec`.
6776 ///
6777 /// Special cases:
6778 /// - $f(x,0,p,m)=1.0$ for any $x$, even $0$
6779 /// - $f(0,y,p,m)=0.0$ if $y>0$, and $\infty$ if $y<0$; a [`Rational`] zero is unsigned, so the
6780 /// results take positive signs
6781 /// - $f(1,y,p,m)=1.0$
6782 /// - $f(-1,y,p,m)=1.0$ if $y$ is an even integer, and $-1.0$ if $y$ is an odd integer
6783 /// - $f(x,y,p,m)=\text{NaN}$ if $x<0$ and $y$ is not an integer
6784 ///
6785 /// Both operands are exact [`Rational`]s, so the exact [`Rational`] exponent selects a definite
6786 /// branch of the power, and results that are exactly representable (such as roots of perfect
6787 /// powers) are detected and rounded exactly.
6788 ///
6789 /// Overflow and underflow:
6790 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
6791 /// returned instead.
6792 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor` or `Down`, $(1-(1/2)^p)2^{2^{30}-1}$
6793 /// is returned instead.
6794 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
6795 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
6796 /// instead.
6797 /// - If $0<f(x,y,p,m)\leq2^{-2^{30}-1}$ and $m$ is `Nearest`, $0.0$ is returned instead.
6798 /// - If $2^{-2^{30}-1}<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
6799 /// instead.
6800 /// - Negative results (from negative $x$ and odd integer $y$) mirror the bullets above, with
6801 /// the rounding directions reflected.
6802 ///
6803 /// # Worst-case complexity
6804 /// $T(n) = O(n^{3/2} \log n \log\log n)$
6805 ///
6806 /// $M(n) = O(n (\log n)^2)$
6807 ///
6808 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(prec, x.significant_bits(),
6809 /// y.significant_bits())`.
6810 ///
6811 /// # Panics
6812 /// Panics if `prec` is zero, or if `rm` is `Exact` but the result cannot be represented exactly
6813 /// with the given precision.
6814 ///
6815 /// # Examples
6816 /// ```
6817 /// use malachite_base::rounding_modes::RoundingMode::*;
6818 /// use malachite_float::Float;
6819 /// use malachite_q::Rational;
6820 /// use std::cmp::Ordering::*;
6821 ///
6822 /// let (p, o) = Float::rational_pow_rational_prec_round(
6823 /// Rational::from_signeds(3, 2),
6824 /// Rational::from_signeds(5, 2),
6825 /// 20,
6826 /// Floor,
6827 /// );
6828 /// assert_eq!(p.to_string(), "2.7556725");
6829 /// assert_eq!(o, Less);
6830 ///
6831 /// let (p, o) = Float::rational_pow_rational_prec_round(
6832 /// Rational::from_signeds(3, 2),
6833 /// Rational::from_signeds(5, 2),
6834 /// 20,
6835 /// Ceiling,
6836 /// );
6837 /// assert_eq!(p.to_string(), "2.7556763");
6838 /// assert_eq!(o, Greater);
6839 ///
6840 /// // (9/4)^(1/2) = 3/2 is exact.
6841 /// let (p, o) = Float::rational_pow_rational_prec_round(
6842 /// Rational::from_signeds(9, 4),
6843 /// Rational::from_signeds(1, 2),
6844 /// 10,
6845 /// Floor,
6846 /// );
6847 /// assert_eq!(p.to_string(), "1.5000");
6848 /// assert_eq!(o, Equal);
6849 /// ```
6850 #[inline]
6851 #[allow(clippy::needless_pass_by_value)]
6852 pub fn rational_pow_rational_prec_round(
6853 x: Rational,
6854 y: Rational,
6855 prec: u64,
6856 rm: RoundingMode,
6857 ) -> (Self, Ordering) {
6858 rational_rational_pow(&x, &y, prec, rm)
6859 }
6860
6861 /// Raises a [`Rational`] to a [`Rational`] power, returning the result as a [`Float`] rounded
6862 /// to the specified precision and with the specified rounding mode. Both [`Rational`]s are
6863 /// taken by reference. An [`Ordering`] is also returned, indicating whether the rounded power
6864 /// is less than, equal to, or greater than the exact power. Although `NaN`s are not comparable
6865 /// to any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
6866 ///
6867 /// See [`Float::rational_pow_rational_prec_round`] for special cases, overflow, and underflow.
6868 ///
6869 /// # Worst-case complexity
6870 /// $T(n) = O(n^{3/2} \log n \log\log n)$
6871 ///
6872 /// $M(n) = O(n (\log n)^2)$
6873 ///
6874 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(prec, x.significant_bits(),
6875 /// y.significant_bits())`.
6876 ///
6877 /// # Panics
6878 /// Panics if `prec` is zero, or if `rm` is `Exact` but the result cannot be represented exactly
6879 /// with the given precision.
6880 ///
6881 /// # Examples
6882 /// ```
6883 /// use malachite_base::rounding_modes::RoundingMode::*;
6884 /// use malachite_float::Float;
6885 /// use malachite_q::Rational;
6886 /// use std::cmp::Ordering::*;
6887 ///
6888 /// let (p, o) = Float::rational_pow_rational_prec_round_ref(
6889 /// &Rational::from_signeds(2, 3),
6890 /// &Rational::from_signeds(-1, 2),
6891 /// 20,
6892 /// Ceiling,
6893 /// );
6894 /// assert_eq!(p.to_string(), "1.2247467");
6895 /// assert_eq!(o, Greater);
6896 /// ```
6897 #[inline]
6898 pub fn rational_pow_rational_prec_round_ref(
6899 x: &Rational,
6900 y: &Rational,
6901 prec: u64,
6902 rm: RoundingMode,
6903 ) -> (Self, Ordering) {
6904 rational_rational_pow(x, y, prec, rm)
6905 }
6906
6907 /// Raises a [`Rational`] to a [`Rational`] power, returning the result as a [`Float`] rounded
6908 /// to the specified precision and to the nearest value. Both [`Rational`]s are taken by value.
6909 /// An [`Ordering`] is also returned, indicating whether the rounded power is less than, equal
6910 /// to, or greater than the exact power. Although `NaN`s are not comparable to any [`Float`],
6911 /// whenever this function returns a `NaN` it also returns `Equal`.
6912 ///
6913 /// If the power is equidistant from two [`Float`]s with the specified precision, the [`Float`]
6914 /// with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of
6915 /// the `Nearest` rounding mode.
6916 ///
6917 /// See [`Float::rational_pow_rational_prec_round`] for special cases, overflow, and underflow.
6918 ///
6919 /// # Worst-case complexity
6920 /// $T(n) = O(n^{3/2} \log n \log\log n)$
6921 ///
6922 /// $M(n) = O(n (\log n)^2)$
6923 ///
6924 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(prec, x.significant_bits(),
6925 /// y.significant_bits())`.
6926 ///
6927 /// # Panics
6928 /// Panics if `prec` is zero.
6929 ///
6930 /// # Examples
6931 /// ```
6932 /// use malachite_float::Float;
6933 /// use malachite_q::Rational;
6934 /// use std::cmp::Ordering::*;
6935 ///
6936 /// let (p, o) = Float::rational_pow_rational_prec(
6937 /// Rational::from_signeds(3, 2),
6938 /// Rational::from_signeds(5, 2),
6939 /// 20,
6940 /// );
6941 /// assert_eq!(p.to_string(), "2.7556763");
6942 /// assert_eq!(o, Greater);
6943 ///
6944 /// let (p, o) =
6945 /// Float::rational_pow_rational_prec(Rational::from(8), Rational::from_signeds(1, 3), 10);
6946 /// assert_eq!(p.to_string(), "2.0000");
6947 /// assert_eq!(o, Equal);
6948 /// ```
6949 #[inline]
6950 #[allow(clippy::needless_pass_by_value)]
6951 pub fn rational_pow_rational_prec(x: Rational, y: Rational, prec: u64) -> (Self, Ordering) {
6952 rational_rational_pow(&x, &y, prec, Nearest)
6953 }
6954
6955 /// Raises a [`Rational`] to a [`Rational`] power, returning the result as a [`Float`] rounded
6956 /// to the specified precision and to the nearest value. Both [`Rational`]s are taken by
6957 /// reference. An [`Ordering`] is also returned, indicating whether the rounded power is less
6958 /// than, equal to, or greater than the exact power. Although `NaN`s are not comparable to any
6959 /// [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
6960 ///
6961 /// If the power is equidistant from two [`Float`]s with the specified precision, the [`Float`]
6962 /// with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of
6963 /// the `Nearest` rounding mode.
6964 ///
6965 /// See [`Float::rational_pow_rational_prec_round`] for special cases, overflow, and underflow.
6966 ///
6967 /// # Worst-case complexity
6968 /// $T(n) = O(n^{3/2} \log n \log\log n)$
6969 ///
6970 /// $M(n) = O(n (\log n)^2)$
6971 ///
6972 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(prec, x.significant_bits(),
6973 /// y.significant_bits())`.
6974 ///
6975 /// # Panics
6976 /// Panics if `prec` is zero.
6977 ///
6978 /// # Examples
6979 /// ```
6980 /// use malachite_float::Float;
6981 /// use malachite_q::Rational;
6982 /// use std::cmp::Ordering::*;
6983 ///
6984 /// let (p, o) = Float::rational_pow_rational_prec_ref(
6985 /// &Rational::from_signeds(3, 2),
6986 /// &Rational::from_signeds(5, 2),
6987 /// 20,
6988 /// );
6989 /// assert_eq!(p.to_string(), "2.7556763");
6990 /// assert_eq!(o, Greater);
6991 /// ```
6992 #[inline]
6993 pub fn rational_pow_rational_prec_ref(
6994 x: &Rational,
6995 y: &Rational,
6996 prec: u64,
6997 ) -> (Self, Ordering) {
6998 rational_rational_pow(x, y, prec, Nearest)
6999 }
7000}
7001
7002impl Float {
7003 // Raises a Rational to a Float power, returning a Float rounded to the specified precision with
7004 // the specified rounding mode.
7005
7006 /// Raises a [`Rational`] to a [`Float`] power, returning the result as a [`Float`] rounded to
7007 /// the specified precision and with the specified rounding mode. The [`Rational`] and the
7008 /// [`Float`] are both taken by reference. An [`Ordering`] is also returned, indicating whether
7009 /// the rounded power is less than, equal to, or greater than the exact power. Although `NaN`s
7010 /// are not comparable to any [`Float`], whenever this function returns a `NaN` it also returns
7011 /// `Equal`.
7012 ///
7013 /// See [`RoundingMode`] for a description of the possible rounding modes.
7014 ///
7015 /// $$
7016 /// f(x,y,p,m) = x^y+\varepsilon.
7017 /// $$
7018 /// - If $x^y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
7019 /// - If $x^y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
7020 /// 2^{\lfloor\log_2 |x^y|\rfloor-p+1}$.
7021 /// - If $x^y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
7022 /// 2^{\lfloor\log_2 |x^y|\rfloor-p}$.
7023 ///
7024 /// If the output has a precision, it is `prec`.
7025 ///
7026 /// Special cases:
7027 /// - $f(x,\pm0.0,p,m)=1.0$ for any $x$, even $0$
7028 /// - $f(1,y,p,m)=1.0$ for any $y$, even `NaN`
7029 /// - $f(x,\text{NaN},p,m)=\text{NaN}$ otherwise
7030 /// - $f(x,\infty,p,m)=\infty$ if $|x|>1$, and $0.0$ if $|x|<1$
7031 /// - $f(x,-\infty,p,m)=0.0$ if $|x|>1$, and $\infty$ if $|x|<1$
7032 /// - $f(\pm1,\pm\infty,p,m)=1.0$
7033 /// - $f(-1,y,p,m)=1.0$ if $y$ is an even integer, and $-1.0$ if $y$ is an odd integer
7034 /// - $f(0,y,p,m)=0.0$ if $y>0$, and $\infty$ if $y<0$; a [`Rational`] zero is unsigned, so the
7035 /// results take positive signs
7036 /// - $f(x,y,p,m)=\text{NaN}$ if $x<0$ and $y$ is finite and not an integer
7037 ///
7038 /// Unlike a [`Float`] base, a [`Rational`] base may lie outside the [`Float`] exponent range or
7039 /// so close to 1 that no [`Float`] can represent its logarithm; both cases are handled exactly,
7040 /// by working with the base as an exact [`Rational`] throughout.
7041 ///
7042 /// Overflow and underflow:
7043 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
7044 /// returned instead.
7045 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor` or `Down`, $(1-(1/2)^p)2^{2^{30}-1}$
7046 /// is returned instead.
7047 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
7048 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
7049 /// instead.
7050 /// - If $0<f(x,y,p,m)\leq2^{-2^{30}-1}$ and $m$ is `Nearest`, $0.0$ is returned instead.
7051 /// - If $2^{-2^{30}-1}<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
7052 /// instead.
7053 /// - Negative results (from negative $x$ and odd integer $y$) mirror the bullets above, with
7054 /// the rounding directions reflected.
7055 ///
7056 /// If you know you'll be using `Nearest`, consider using [`Float::rational_pow_prec_ref_ref`]
7057 /// instead.
7058 ///
7059 /// # Worst-case complexity
7060 /// $T(n) = O(n^{3/2} \log n \log\log n)$
7061 ///
7062 /// $M(n) = O(n (\log n)^2)$
7063 ///
7064 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(prec, x.significant_bits(),
7065 /// y.significant_bits())`.
7066 ///
7067 /// # Panics
7068 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the given
7069 /// precision.
7070 ///
7071 /// # Examples
7072 /// ```
7073 /// use malachite_base::rounding_modes::RoundingMode::*;
7074 /// use malachite_float::Float;
7075 /// use malachite_q::Rational;
7076 /// use std::cmp::Ordering::*;
7077 ///
7078 /// let (p, o) = Float::rational_pow_prec_round_ref_ref(
7079 /// &Rational::from_unsigneds(3u32, 2u32),
7080 /// &Float::from(2.5),
7081 /// 5,
7082 /// Floor,
7083 /// );
7084 /// assert_eq!(p.to_string(), "2.75");
7085 /// assert_eq!(o, Less);
7086 ///
7087 /// let (p, o) = Float::rational_pow_prec_round_ref_ref(
7088 /// &Rational::from_unsigneds(3u32, 2u32),
7089 /// &Float::from(2.5),
7090 /// 5,
7091 /// Ceiling,
7092 /// );
7093 /// assert_eq!(p.to_string(), "2.88");
7094 /// assert_eq!(o, Greater);
7095 ///
7096 /// let (p, o) = Float::rational_pow_prec_round_ref_ref(
7097 /// &Rational::from_unsigneds(3u32, 2u32),
7098 /// &Float::from(2.5),
7099 /// 5,
7100 /// Nearest,
7101 /// );
7102 /// assert_eq!(p.to_string(), "2.75");
7103 /// assert_eq!(o, Less);
7104 ///
7105 /// let (p, o) = Float::rational_pow_prec_round_ref_ref(
7106 /// &Rational::from_unsigneds(3u32, 2u32),
7107 /// &Float::from(2.5),
7108 /// 20,
7109 /// Floor,
7110 /// );
7111 /// assert_eq!(p.to_string(), "2.7556725");
7112 /// assert_eq!(o, Less);
7113 ///
7114 /// let (p, o) = Float::rational_pow_prec_round_ref_ref(
7115 /// &Rational::from_unsigneds(3u32, 2u32),
7116 /// &Float::from(2.5),
7117 /// 20,
7118 /// Ceiling,
7119 /// );
7120 /// assert_eq!(p.to_string(), "2.7556763");
7121 /// assert_eq!(o, Greater);
7122 ///
7123 /// let (p, o) = Float::rational_pow_prec_round_ref_ref(
7124 /// &Rational::from_unsigneds(3u32, 2u32),
7125 /// &Float::from(2.5),
7126 /// 20,
7127 /// Nearest,
7128 /// );
7129 /// assert_eq!(p.to_string(), "2.7556763");
7130 /// assert_eq!(o, Greater);
7131 /// ```
7132 pub fn rational_pow_prec_round_ref_ref(
7133 x: &Rational,
7134 y: &Self,
7135 prec: u64,
7136 rm: RoundingMode,
7137 ) -> (Self, Ordering) {
7138 assert_ne!(prec, 0);
7139 // Exact rounding: compute with Nearest and demand exactness.
7140 if rm == Exact {
7141 let (result, o) = Self::rational_pow_prec_ref_ref(x, y, prec);
7142 assert_eq!(o, Equal, "Inexact rational_pow");
7143 return (result, Equal);
7144 }
7145 // Singular y; see Section F.9.4.4 of the C standard.
7146 match y {
7147 // x^0 = 1 for any x, even 0
7148 float_either_zero!() => {
7149 return (Self::one_prec(prec), Equal);
7150 }
7151 // 1^y = 1 for any y, even NaN
7152 float_nan!() => {
7153 return if *x == 1u32 {
7154 (Self::one_prec(prec), Equal)
7155 } else {
7156 (Self::NAN, Equal)
7157 };
7158 }
7159 Self(Infinity { sign }) => {
7160 let mut cmp = x.cmp_abs(&Rational::ONE);
7161 if !*sign {
7162 cmp = cmp.reverse();
7163 }
7164 return match cmp {
7165 Greater => (Self::INFINITY, Equal),
7166 Less => (Self::ZERO, Equal),
7167 Equal => (Self::one_prec(prec), Equal),
7168 };
7169 }
7170 _ => {}
7171 }
7172 // x = 0: Rational zero is unsigned, so the results take positive signs.
7173 if *x == 0u32 {
7174 return if *y > 0u32 {
7175 (Self::ZERO, Equal)
7176 } else {
7177 (Self::INFINITY, Equal)
7178 };
7179 }
7180 let y_is_integer = y.is_integer();
7181 // Negative x: only integer y is defined; the sign is that of (-1)^y.
7182 if *x < 0u32 {
7183 if !y_is_integer {
7184 return (Self::NAN, Equal);
7185 }
7186 let negative = float_odd_integer(y);
7187 let (result, o) = Self::rational_pow_prec_round_ref_ref(
7188 &(-x),
7189 y,
7190 prec,
7191 if negative { -rm } else { rm },
7192 );
7193 return if negative {
7194 (-result, o.reverse())
7195 } else {
7196 (result, o)
7197 };
7198 }
7199 if *x == 1u32 {
7200 return (Self::one_prec(prec), Equal);
7201 }
7202 // x = 2^e exactly: x^y = 2^(e * y) with e * y an exact Rational;
7203 // `power_of_2_rational_prec_round` handles all exactness, overflow, and underflow.
7204 if let Some(e) = x.checked_log_base_2() {
7205 let t = Rational::from(e) * Rational::exact_from(y);
7206 return Self::power_of_2_rational_prec_round(t, prec, rm);
7207 }
7208 // Small integer y with a small base: materialize x^y as an exact Rational;
7209 // `from_rational_prec_round` handles all rounding, including at the range boundaries.
7210 let nbits = x.significant_bits();
7211 if y_is_integer && y.get_exponent().unwrap() <= 32 {
7212 let z = i64::rounding_from(y, Nearest).0;
7213 if z.unsigned_abs().saturating_mul(nbits) <= max(65536, prec << 2) {
7214 return Self::from_rational_prec_round(x.pow(z), prec, rm);
7215 }
7216 }
7217 let fl = x.floor_log_base_2_abs();
7218 let in_range =
7219 fl > i64::from(Self::MIN_EXPONENT) + 2 && fl < i64::from(Self::MAX_EXPONENT) - 2;
7220 // A base within a few binades of 1 (from either side) has a logarithm at or below the
7221 // smallest positive Float, where any Float-based power -- the dyadic shortcut or the
7222 // x-space squeeze below, both of which call `Float::pow` -- would underflow internally
7223 // (`ln` cannot represent the sub-`MIN_EXPONENT` result). Such a base goes through the
7224 // exact-Rational t-space squeeze, which brackets `log2` with the atanh series over
7225 // `Rational`s and never materializes a sub-`MIN_EXPONENT` Float logarithm. `x` is a sliver
7226 // of 1 only when it lies in `(1/2, 2)`, i.e. `fl` is 0 or -1; the exact subtraction is
7227 // skipped otherwise.
7228 let sliver_fld = if fl == 0 || fl == -1 {
7229 if *x == 1u32 {
7230 None
7231 } else {
7232 Some((x - Rational::ONE).floor_log_base_2_abs())
7233 }
7234 } else {
7235 None
7236 };
7237 let sliver_of_one = sliver_fld.is_some_and(|fld| fld < i64::from(Self::MIN_EXPONENT) + 8);
7238 // A dyadic in-range non-sliver x is exactly convertible; Float::pow does the rest,
7239 // exactness and boundary behavior included.
7240 if in_range && !sliver_of_one && x.denominator_ref().is_power_of_2() {
7241 let xf = Self::from_rational_prec_round_ref(x, nbits, Floor).0;
7242 return xf.pow_prec_round_val_ref(y, prec, rm);
7243 }
7244 // Possible exact dyadic results must be handled directly: a Ziv squeeze never terminates on
7245 // an exactly-representable value and can stall on a nearest-mode tie.
7246 let n = x.numerator_ref();
7247 let d = x.denominator_ref();
7248 let alpha = i64::exact_from(n.trailing_zeros().unwrap());
7249 let beta = i64::exact_from(d.trailing_zeros().unwrap());
7250 let a = n >> alpha;
7251 let b = d >> beta;
7252 if let Some((m, z, pow)) = rational_pow_exact_decomposition(&a, &b, alpha - beta, y)
7253 && let Some(result) = rational_pow_exact(&m, &z, &pow, prec, rm)
7254 {
7255 return result;
7256 }
7257 if in_range && !sliver_of_one {
7258 rational_pow_squeeze_x(x, y, prec, rm)
7259 } else {
7260 // Tiny-result shortcut for a sliver of 1: if |y * log2(x)| is far below 1, x^y rounds
7261 // to 1 +/- ulp, avoiding the (up to 128-MB) log2 brackets. With fld = floor_log2|x -
7262 // 1|, one has |log2(x)| < 2^(fld + 2), so |y * log2(x)| < 2^(ey + fld + 2); when that
7263 // is below 2^(-prec - 1) the result is within half an ulp of 1.
7264 if let Some(fld) = sliver_fld {
7265 let ey = i64::from(y.get_exponent().unwrap());
7266 if ey + fld + 2 < -i64::exact_from(prec) - 1 {
7267 let above = (*y > 0u32) == (*x > 1u32);
7268 return float_one_plus_tiny(prec, rm, above);
7269 }
7270 }
7271 // Extreme x -- beyond the exponent range or a sliver of 1: split off the power of 2
7272 // (rounded to the nearest, so the mantissa is close to 1) and work with exact Rationals
7273 // in the exponent.
7274 let (xp, g) = rational_mantissa_nearest_power_of_2(x);
7275 pow_squeeze_t(&xp, g, &Rational::exact_from(y), prec, rm)
7276 }
7277 }
7278
7279 #[allow(clippy::needless_pass_by_value)]
7280 /// Raises a [`Rational`] to a [`Float`] power, returning the result as a [`Float`] rounded to
7281 /// the specified precision and with the specified rounding mode. The [`Rational`] and the
7282 /// [`Float`] are both taken by value. An [`Ordering`] is also returned, indicating whether the
7283 /// rounded power is less than, equal to, or greater than the exact power. Although `NaN`s are
7284 /// not comparable to any [`Float`], whenever this function returns a `NaN` it also returns
7285 /// `Equal`.
7286 ///
7287 /// See [`RoundingMode`] for a description of the possible rounding modes.
7288 ///
7289 /// $$
7290 /// f(x,y,p,m) = x^y+\varepsilon.
7291 /// $$
7292 /// - If $x^y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
7293 /// - If $x^y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
7294 /// 2^{\lfloor\log_2 |x^y|\rfloor-p+1}$.
7295 /// - If $x^y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
7296 /// 2^{\lfloor\log_2 |x^y|\rfloor-p}$.
7297 ///
7298 /// If the output has a precision, it is `prec`.
7299 ///
7300 /// Special cases:
7301 /// - $f(x,\pm0.0,p,m)=1.0$ for any $x$, even $0$
7302 /// - $f(1,y,p,m)=1.0$ for any $y$, even `NaN`
7303 /// - $f(x,\text{NaN},p,m)=\text{NaN}$ otherwise
7304 /// - $f(x,\infty,p,m)=\infty$ if $|x|>1$, and $0.0$ if $|x|<1$
7305 /// - $f(x,-\infty,p,m)=0.0$ if $|x|>1$, and $\infty$ if $|x|<1$
7306 /// - $f(\pm1,\pm\infty,p,m)=1.0$
7307 /// - $f(-1,y,p,m)=1.0$ if $y$ is an even integer, and $-1.0$ if $y$ is an odd integer
7308 /// - $f(0,y,p,m)=0.0$ if $y>0$, and $\infty$ if $y<0$; a [`Rational`] zero is unsigned, so the
7309 /// results take positive signs
7310 /// - $f(x,y,p,m)=\text{NaN}$ if $x<0$ and $y$ is finite and not an integer
7311 ///
7312 /// Unlike a [`Float`] base, a [`Rational`] base may lie outside the [`Float`] exponent range or
7313 /// so close to 1 that no [`Float`] can represent its logarithm; both cases are handled exactly,
7314 /// by working with the base as an exact [`Rational`] throughout.
7315 ///
7316 /// Overflow and underflow:
7317 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
7318 /// returned instead.
7319 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor` or `Down`, $(1-(1/2)^p)2^{2^{30}-1}$
7320 /// is returned instead.
7321 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
7322 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
7323 /// instead.
7324 /// - If $0<f(x,y,p,m)\leq2^{-2^{30}-1}$ and $m$ is `Nearest`, $0.0$ is returned instead.
7325 /// - If $2^{-2^{30}-1}<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
7326 /// instead.
7327 /// - Negative results (from negative $x$ and odd integer $y$) mirror the bullets above, with
7328 /// the rounding directions reflected.
7329 ///
7330 /// If you know you'll be using `Nearest`, consider using [`Float::rational_pow_prec`] instead.
7331 ///
7332 /// # Worst-case complexity
7333 /// $T(n) = O(n^{3/2} \log n \log\log n)$
7334 ///
7335 /// $M(n) = O(n (\log n)^2)$
7336 ///
7337 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(prec, x.significant_bits(),
7338 /// y.significant_bits())`.
7339 ///
7340 /// # Panics
7341 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the given
7342 /// precision.
7343 ///
7344 /// # Examples
7345 /// ```
7346 /// use malachite_base::rounding_modes::RoundingMode::*;
7347 /// use malachite_float::Float;
7348 /// use malachite_q::Rational;
7349 /// use std::cmp::Ordering::*;
7350 ///
7351 /// let (p, o) = Float::rational_pow_prec_round(
7352 /// Rational::from_unsigneds(3u32, 2u32),
7353 /// Float::from(2.5),
7354 /// 5,
7355 /// Floor,
7356 /// );
7357 /// assert_eq!(p.to_string(), "2.75");
7358 /// assert_eq!(o, Less);
7359 ///
7360 /// let (p, o) = Float::rational_pow_prec_round(
7361 /// Rational::from_unsigneds(3u32, 2u32),
7362 /// Float::from(2.5),
7363 /// 5,
7364 /// Ceiling,
7365 /// );
7366 /// assert_eq!(p.to_string(), "2.88");
7367 /// assert_eq!(o, Greater);
7368 ///
7369 /// let (p, o) = Float::rational_pow_prec_round(
7370 /// Rational::from_unsigneds(3u32, 2u32),
7371 /// Float::from(2.5),
7372 /// 5,
7373 /// Nearest,
7374 /// );
7375 /// assert_eq!(p.to_string(), "2.75");
7376 /// assert_eq!(o, Less);
7377 ///
7378 /// let (p, o) = Float::rational_pow_prec_round(
7379 /// Rational::from_unsigneds(3u32, 2u32),
7380 /// Float::from(2.5),
7381 /// 20,
7382 /// Floor,
7383 /// );
7384 /// assert_eq!(p.to_string(), "2.7556725");
7385 /// assert_eq!(o, Less);
7386 ///
7387 /// let (p, o) = Float::rational_pow_prec_round(
7388 /// Rational::from_unsigneds(3u32, 2u32),
7389 /// Float::from(2.5),
7390 /// 20,
7391 /// Ceiling,
7392 /// );
7393 /// assert_eq!(p.to_string(), "2.7556763");
7394 /// assert_eq!(o, Greater);
7395 ///
7396 /// let (p, o) = Float::rational_pow_prec_round(
7397 /// Rational::from_unsigneds(3u32, 2u32),
7398 /// Float::from(2.5),
7399 /// 20,
7400 /// Nearest,
7401 /// );
7402 /// assert_eq!(p.to_string(), "2.7556763");
7403 /// assert_eq!(o, Greater);
7404 /// ```
7405 #[inline]
7406 pub fn rational_pow_prec_round(
7407 x: Rational,
7408 y: Self,
7409 prec: u64,
7410 rm: RoundingMode,
7411 ) -> (Self, Ordering) {
7412 Self::rational_pow_prec_round_ref_ref(&x, &y, prec, rm)
7413 }
7414
7415 #[allow(clippy::needless_pass_by_value)]
7416 /// Raises a [`Rational`] to a [`Float`] power, returning the result as a [`Float`] rounded to
7417 /// the specified precision and with the specified rounding mode. The [`Rational`] is taken by
7418 /// value and the [`Float`] by reference. An [`Ordering`] is also returned, indicating whether
7419 /// the rounded power is less than, equal to, or greater than the exact power. Although `NaN`s
7420 /// are not comparable to any [`Float`], whenever this function returns a `NaN` it also returns
7421 /// `Equal`.
7422 ///
7423 /// See [`RoundingMode`] for a description of the possible rounding modes.
7424 ///
7425 /// $$
7426 /// f(x,y,p,m) = x^y+\varepsilon.
7427 /// $$
7428 /// - If $x^y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
7429 /// - If $x^y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
7430 /// 2^{\lfloor\log_2 |x^y|\rfloor-p+1}$.
7431 /// - If $x^y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
7432 /// 2^{\lfloor\log_2 |x^y|\rfloor-p}$.
7433 ///
7434 /// If the output has a precision, it is `prec`.
7435 ///
7436 /// Special cases:
7437 /// - $f(x,\pm0.0,p,m)=1.0$ for any $x$, even $0$
7438 /// - $f(1,y,p,m)=1.0$ for any $y$, even `NaN`
7439 /// - $f(x,\text{NaN},p,m)=\text{NaN}$ otherwise
7440 /// - $f(x,\infty,p,m)=\infty$ if $|x|>1$, and $0.0$ if $|x|<1$
7441 /// - $f(x,-\infty,p,m)=0.0$ if $|x|>1$, and $\infty$ if $|x|<1$
7442 /// - $f(\pm1,\pm\infty,p,m)=1.0$
7443 /// - $f(-1,y,p,m)=1.0$ if $y$ is an even integer, and $-1.0$ if $y$ is an odd integer
7444 /// - $f(0,y,p,m)=0.0$ if $y>0$, and $\infty$ if $y<0$; a [`Rational`] zero is unsigned, so the
7445 /// results take positive signs
7446 /// - $f(x,y,p,m)=\text{NaN}$ if $x<0$ and $y$ is finite and not an integer
7447 ///
7448 /// Unlike a [`Float`] base, a [`Rational`] base may lie outside the [`Float`] exponent range or
7449 /// so close to 1 that no [`Float`] can represent its logarithm; both cases are handled exactly,
7450 /// by working with the base as an exact [`Rational`] throughout.
7451 ///
7452 /// Overflow and underflow:
7453 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
7454 /// returned instead.
7455 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor` or `Down`, $(1-(1/2)^p)2^{2^{30}-1}$
7456 /// is returned instead.
7457 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
7458 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
7459 /// instead.
7460 /// - If $0<f(x,y,p,m)\leq2^{-2^{30}-1}$ and $m$ is `Nearest`, $0.0$ is returned instead.
7461 /// - If $2^{-2^{30}-1}<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
7462 /// instead.
7463 /// - Negative results (from negative $x$ and odd integer $y$) mirror the bullets above, with
7464 /// the rounding directions reflected.
7465 ///
7466 /// If you know you'll be using `Nearest`, consider using [`Float::rational_pow_prec_val_ref`]
7467 /// instead.
7468 ///
7469 /// # Worst-case complexity
7470 /// $T(n) = O(n^{3/2} \log n \log\log n)$
7471 ///
7472 /// $M(n) = O(n (\log n)^2)$
7473 ///
7474 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(prec, x.significant_bits(),
7475 /// y.significant_bits())`.
7476 ///
7477 /// # Panics
7478 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the given
7479 /// precision.
7480 ///
7481 /// # Examples
7482 /// ```
7483 /// use malachite_base::rounding_modes::RoundingMode::*;
7484 /// use malachite_float::Float;
7485 /// use malachite_q::Rational;
7486 /// use std::cmp::Ordering::*;
7487 ///
7488 /// let (p, o) = Float::rational_pow_prec_round_val_ref(
7489 /// Rational::from_unsigneds(3u32, 2u32),
7490 /// &Float::from(2.5),
7491 /// 5,
7492 /// Floor,
7493 /// );
7494 /// assert_eq!(p.to_string(), "2.75");
7495 /// assert_eq!(o, Less);
7496 ///
7497 /// let (p, o) = Float::rational_pow_prec_round_val_ref(
7498 /// Rational::from_unsigneds(3u32, 2u32),
7499 /// &Float::from(2.5),
7500 /// 5,
7501 /// Ceiling,
7502 /// );
7503 /// assert_eq!(p.to_string(), "2.88");
7504 /// assert_eq!(o, Greater);
7505 ///
7506 /// let (p, o) = Float::rational_pow_prec_round_val_ref(
7507 /// Rational::from_unsigneds(3u32, 2u32),
7508 /// &Float::from(2.5),
7509 /// 5,
7510 /// Nearest,
7511 /// );
7512 /// assert_eq!(p.to_string(), "2.75");
7513 /// assert_eq!(o, Less);
7514 ///
7515 /// let (p, o) = Float::rational_pow_prec_round_val_ref(
7516 /// Rational::from_unsigneds(3u32, 2u32),
7517 /// &Float::from(2.5),
7518 /// 20,
7519 /// Floor,
7520 /// );
7521 /// assert_eq!(p.to_string(), "2.7556725");
7522 /// assert_eq!(o, Less);
7523 ///
7524 /// let (p, o) = Float::rational_pow_prec_round_val_ref(
7525 /// Rational::from_unsigneds(3u32, 2u32),
7526 /// &Float::from(2.5),
7527 /// 20,
7528 /// Ceiling,
7529 /// );
7530 /// assert_eq!(p.to_string(), "2.7556763");
7531 /// assert_eq!(o, Greater);
7532 ///
7533 /// let (p, o) = Float::rational_pow_prec_round_val_ref(
7534 /// Rational::from_unsigneds(3u32, 2u32),
7535 /// &Float::from(2.5),
7536 /// 20,
7537 /// Nearest,
7538 /// );
7539 /// assert_eq!(p.to_string(), "2.7556763");
7540 /// assert_eq!(o, Greater);
7541 /// ```
7542 #[inline]
7543 pub fn rational_pow_prec_round_val_ref(
7544 x: Rational,
7545 y: &Self,
7546 prec: u64,
7547 rm: RoundingMode,
7548 ) -> (Self, Ordering) {
7549 Self::rational_pow_prec_round_ref_ref(&x, y, prec, rm)
7550 }
7551
7552 #[allow(clippy::needless_pass_by_value)]
7553 /// Raises a [`Rational`] to a [`Float`] power, returning the result as a [`Float`] rounded to
7554 /// the specified precision and with the specified rounding mode. The [`Rational`] is taken by
7555 /// reference and the [`Float`] by value. An [`Ordering`] is also returned, indicating whether
7556 /// the rounded power is less than, equal to, or greater than the exact power. Although `NaN`s
7557 /// are not comparable to any [`Float`], whenever this function returns a `NaN` it also returns
7558 /// `Equal`.
7559 ///
7560 /// See [`RoundingMode`] for a description of the possible rounding modes.
7561 ///
7562 /// $$
7563 /// f(x,y,p,m) = x^y+\varepsilon.
7564 /// $$
7565 /// - If $x^y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
7566 /// - If $x^y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
7567 /// 2^{\lfloor\log_2 |x^y|\rfloor-p+1}$.
7568 /// - If $x^y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
7569 /// 2^{\lfloor\log_2 |x^y|\rfloor-p}$.
7570 ///
7571 /// If the output has a precision, it is `prec`.
7572 ///
7573 /// Special cases:
7574 /// - $f(x,\pm0.0,p,m)=1.0$ for any $x$, even $0$
7575 /// - $f(1,y,p,m)=1.0$ for any $y$, even `NaN`
7576 /// - $f(x,\text{NaN},p,m)=\text{NaN}$ otherwise
7577 /// - $f(x,\infty,p,m)=\infty$ if $|x|>1$, and $0.0$ if $|x|<1$
7578 /// - $f(x,-\infty,p,m)=0.0$ if $|x|>1$, and $\infty$ if $|x|<1$
7579 /// - $f(\pm1,\pm\infty,p,m)=1.0$
7580 /// - $f(-1,y,p,m)=1.0$ if $y$ is an even integer, and $-1.0$ if $y$ is an odd integer
7581 /// - $f(0,y,p,m)=0.0$ if $y>0$, and $\infty$ if $y<0$; a [`Rational`] zero is unsigned, so the
7582 /// results take positive signs
7583 /// - $f(x,y,p,m)=\text{NaN}$ if $x<0$ and $y$ is finite and not an integer
7584 ///
7585 /// Unlike a [`Float`] base, a [`Rational`] base may lie outside the [`Float`] exponent range or
7586 /// so close to 1 that no [`Float`] can represent its logarithm; both cases are handled exactly,
7587 /// by working with the base as an exact [`Rational`] throughout.
7588 ///
7589 /// Overflow and underflow:
7590 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
7591 /// returned instead.
7592 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor` or `Down`, $(1-(1/2)^p)2^{2^{30}-1}$
7593 /// is returned instead.
7594 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
7595 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
7596 /// instead.
7597 /// - If $0<f(x,y,p,m)\leq2^{-2^{30}-1}$ and $m$ is `Nearest`, $0.0$ is returned instead.
7598 /// - If $2^{-2^{30}-1}<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
7599 /// instead.
7600 /// - Negative results (from negative $x$ and odd integer $y$) mirror the bullets above, with
7601 /// the rounding directions reflected.
7602 ///
7603 /// If you know you'll be using `Nearest`, consider using [`Float::rational_pow_prec_ref_val`]
7604 /// instead.
7605 ///
7606 /// # Worst-case complexity
7607 /// $T(n) = O(n^{3/2} \log n \log\log n)$
7608 ///
7609 /// $M(n) = O(n (\log n)^2)$
7610 ///
7611 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(prec, x.significant_bits(),
7612 /// y.significant_bits())`.
7613 ///
7614 /// # Panics
7615 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the given
7616 /// precision.
7617 ///
7618 /// # Examples
7619 /// ```
7620 /// use malachite_base::rounding_modes::RoundingMode::*;
7621 /// use malachite_float::Float;
7622 /// use malachite_q::Rational;
7623 /// use std::cmp::Ordering::*;
7624 ///
7625 /// let (p, o) = Float::rational_pow_prec_round_ref_val(
7626 /// &Rational::from_unsigneds(3u32, 2u32),
7627 /// Float::from(2.5),
7628 /// 5,
7629 /// Floor,
7630 /// );
7631 /// assert_eq!(p.to_string(), "2.75");
7632 /// assert_eq!(o, Less);
7633 ///
7634 /// let (p, o) = Float::rational_pow_prec_round_ref_val(
7635 /// &Rational::from_unsigneds(3u32, 2u32),
7636 /// Float::from(2.5),
7637 /// 5,
7638 /// Ceiling,
7639 /// );
7640 /// assert_eq!(p.to_string(), "2.88");
7641 /// assert_eq!(o, Greater);
7642 ///
7643 /// let (p, o) = Float::rational_pow_prec_round_ref_val(
7644 /// &Rational::from_unsigneds(3u32, 2u32),
7645 /// Float::from(2.5),
7646 /// 5,
7647 /// Nearest,
7648 /// );
7649 /// assert_eq!(p.to_string(), "2.75");
7650 /// assert_eq!(o, Less);
7651 ///
7652 /// let (p, o) = Float::rational_pow_prec_round_ref_val(
7653 /// &Rational::from_unsigneds(3u32, 2u32),
7654 /// Float::from(2.5),
7655 /// 20,
7656 /// Floor,
7657 /// );
7658 /// assert_eq!(p.to_string(), "2.7556725");
7659 /// assert_eq!(o, Less);
7660 ///
7661 /// let (p, o) = Float::rational_pow_prec_round_ref_val(
7662 /// &Rational::from_unsigneds(3u32, 2u32),
7663 /// Float::from(2.5),
7664 /// 20,
7665 /// Ceiling,
7666 /// );
7667 /// assert_eq!(p.to_string(), "2.7556763");
7668 /// assert_eq!(o, Greater);
7669 ///
7670 /// let (p, o) = Float::rational_pow_prec_round_ref_val(
7671 /// &Rational::from_unsigneds(3u32, 2u32),
7672 /// Float::from(2.5),
7673 /// 20,
7674 /// Nearest,
7675 /// );
7676 /// assert_eq!(p.to_string(), "2.7556763");
7677 /// assert_eq!(o, Greater);
7678 /// ```
7679 #[inline]
7680 pub fn rational_pow_prec_round_ref_val(
7681 x: &Rational,
7682 y: Self,
7683 prec: u64,
7684 rm: RoundingMode,
7685 ) -> (Self, Ordering) {
7686 Self::rational_pow_prec_round_ref_ref(x, &y, prec, rm)
7687 }
7688
7689 #[allow(clippy::needless_pass_by_value)]
7690 /// Raises a [`Rational`] to a [`Float`] power, returning the result as a [`Float`] rounded to
7691 /// the specified precision and to the nearest value. The [`Rational`] and the [`Float`] are
7692 /// both taken by value. An [`Ordering`] is also returned, indicating whether the rounded power
7693 /// is less than, equal to, or greater than the exact power. Although `NaN`s are not comparable
7694 /// to any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
7695 ///
7696 /// If the power is equidistant from two [`Float`]s with the specified precision, the [`Float`]
7697 /// with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of
7698 /// the `Nearest` rounding mode.
7699 ///
7700 /// $$
7701 /// f(x,y,p) = x^y+\varepsilon.
7702 /// $$
7703 /// - If $x^y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
7704 /// - If $x^y$ is finite and nonzero, then $|\varepsilon| \leq 2^{\lfloor\log_2
7705 /// |x^y|\rfloor-p}$.
7706 ///
7707 /// If the output has a precision, it is `prec`.
7708 ///
7709 /// Special cases:
7710 /// - $f(x,\pm0.0,p)=1.0$ for any $x$, even $0$
7711 /// - $f(1,y,p)=1.0$ for any $y$, even `NaN`
7712 /// - $f(x,\text{NaN},p)=\text{NaN}$ otherwise
7713 /// - $f(x,\infty,p)=\infty$ if $|x|>1$, and $0.0$ if $|x|<1$
7714 /// - $f(x,-\infty,p)=0.0$ if $|x|>1$, and $\infty$ if $|x|<1$
7715 /// - $f(\pm1,\pm\infty,p)=1.0$
7716 /// - $f(-1,y,p)=1.0$ if $y$ is an even integer, and $-1.0$ if $y$ is an odd integer
7717 /// - $f(0,y,p)=0.0$ if $y>0$, and $\infty$ if $y<0$; a [`Rational`] zero is unsigned, so the
7718 /// results take positive signs
7719 /// - $f(x,y,p)=\text{NaN}$ if $x<0$ and $y$ is finite and not an integer
7720 ///
7721 /// Unlike a [`Float`] base, a [`Rational`] base may lie outside the [`Float`] exponent range or
7722 /// so close to 1 that no [`Float`] can represent its logarithm; both cases are handled exactly,
7723 /// by working with the base as an exact [`Rational`] throughout.
7724 ///
7725 /// Overflow and underflow:
7726 /// - If $f(x,y,p)\geq 2^{2^{30}-1}$, $\infty$ is returned instead.
7727 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
7728 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
7729 /// - Negative results (from negative $x$ and odd integer $y$) mirror the bullets above.
7730 ///
7731 /// If you want to use a rounding mode other than `Nearest`, consider using
7732 /// [`Float::rational_pow_prec_round`] instead.
7733 ///
7734 /// # Worst-case complexity
7735 /// $T(n) = O(n^{3/2} \log n \log\log n)$
7736 ///
7737 /// $M(n) = O(n (\log n)^2)$
7738 ///
7739 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(prec, x.significant_bits(),
7740 /// y.significant_bits())`.
7741 ///
7742 /// # Examples
7743 /// ```
7744 /// use malachite_float::Float;
7745 /// use malachite_q::Rational;
7746 /// use std::cmp::Ordering::*;
7747 ///
7748 /// let (p, o) =
7749 /// Float::rational_pow_prec(Rational::from_unsigneds(3u32, 2u32), Float::from(2.5), 5);
7750 /// assert_eq!(p.to_string(), "2.75");
7751 /// assert_eq!(o, Less);
7752 ///
7753 /// let (p, o) =
7754 /// Float::rational_pow_prec(Rational::from_unsigneds(3u32, 2u32), Float::from(2.5), 20);
7755 /// assert_eq!(p.to_string(), "2.7556763");
7756 /// assert_eq!(o, Greater);
7757 /// ```
7758 #[inline]
7759 pub fn rational_pow_prec(x: Rational, y: Self, prec: u64) -> (Self, Ordering) {
7760 Self::rational_pow_prec_ref_ref(&x, &y, prec)
7761 }
7762
7763 #[allow(clippy::needless_pass_by_value)]
7764 /// Raises a [`Rational`] to a [`Float`] power, returning the result as a [`Float`] rounded to
7765 /// the specified precision and to the nearest value. The [`Rational`] is taken by value and the
7766 /// [`Float`] by reference. An [`Ordering`] is also returned, indicating whether the rounded
7767 /// power is less than, equal to, or greater than the exact power. Although `NaN`s are not
7768 /// comparable to any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
7769 ///
7770 /// If the power is equidistant from two [`Float`]s with the specified precision, the [`Float`]
7771 /// with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of
7772 /// the `Nearest` rounding mode.
7773 ///
7774 /// $$
7775 /// f(x,y,p) = x^y+\varepsilon.
7776 /// $$
7777 /// - If $x^y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
7778 /// - If $x^y$ is finite and nonzero, then $|\varepsilon| \leq 2^{\lfloor\log_2
7779 /// |x^y|\rfloor-p}$.
7780 ///
7781 /// If the output has a precision, it is `prec`.
7782 ///
7783 /// Special cases:
7784 /// - $f(x,\pm0.0,p)=1.0$ for any $x$, even $0$
7785 /// - $f(1,y,p)=1.0$ for any $y$, even `NaN`
7786 /// - $f(x,\text{NaN},p)=\text{NaN}$ otherwise
7787 /// - $f(x,\infty,p)=\infty$ if $|x|>1$, and $0.0$ if $|x|<1$
7788 /// - $f(x,-\infty,p)=0.0$ if $|x|>1$, and $\infty$ if $|x|<1$
7789 /// - $f(\pm1,\pm\infty,p)=1.0$
7790 /// - $f(-1,y,p)=1.0$ if $y$ is an even integer, and $-1.0$ if $y$ is an odd integer
7791 /// - $f(0,y,p)=0.0$ if $y>0$, and $\infty$ if $y<0$; a [`Rational`] zero is unsigned, so the
7792 /// results take positive signs
7793 /// - $f(x,y,p)=\text{NaN}$ if $x<0$ and $y$ is finite and not an integer
7794 ///
7795 /// Unlike a [`Float`] base, a [`Rational`] base may lie outside the [`Float`] exponent range or
7796 /// so close to 1 that no [`Float`] can represent its logarithm; both cases are handled exactly,
7797 /// by working with the base as an exact [`Rational`] throughout.
7798 ///
7799 /// Overflow and underflow:
7800 /// - If $f(x,y,p)\geq 2^{2^{30}-1}$, $\infty$ is returned instead.
7801 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
7802 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
7803 /// - Negative results (from negative $x$ and odd integer $y$) mirror the bullets above.
7804 ///
7805 /// If you want to use a rounding mode other than `Nearest`, consider using
7806 /// [`Float::rational_pow_prec_round_val_ref`] instead.
7807 ///
7808 /// # Worst-case complexity
7809 /// $T(n) = O(n^{3/2} \log n \log\log n)$
7810 ///
7811 /// $M(n) = O(n (\log n)^2)$
7812 ///
7813 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(prec, x.significant_bits(),
7814 /// y.significant_bits())`.
7815 ///
7816 /// # Examples
7817 /// ```
7818 /// use malachite_float::Float;
7819 /// use malachite_q::Rational;
7820 /// use std::cmp::Ordering::*;
7821 ///
7822 /// let (p, o) = Float::rational_pow_prec_val_ref(
7823 /// Rational::from_unsigneds(3u32, 2u32),
7824 /// &Float::from(2.5),
7825 /// 5,
7826 /// );
7827 /// assert_eq!(p.to_string(), "2.75");
7828 /// assert_eq!(o, Less);
7829 ///
7830 /// let (p, o) = Float::rational_pow_prec_val_ref(
7831 /// Rational::from_unsigneds(3u32, 2u32),
7832 /// &Float::from(2.5),
7833 /// 20,
7834 /// );
7835 /// assert_eq!(p.to_string(), "2.7556763");
7836 /// assert_eq!(o, Greater);
7837 /// ```
7838 #[inline]
7839 pub fn rational_pow_prec_val_ref(x: Rational, y: &Self, prec: u64) -> (Self, Ordering) {
7840 Self::rational_pow_prec_ref_ref(&x, y, prec)
7841 }
7842
7843 #[allow(clippy::needless_pass_by_value)]
7844 /// Raises a [`Rational`] to a [`Float`] power, returning the result as a [`Float`] rounded to
7845 /// the specified precision and to the nearest value. The [`Rational`] is taken by reference and
7846 /// the [`Float`] by value. An [`Ordering`] is also returned, indicating whether the rounded
7847 /// power is less than, equal to, or greater than the exact power. Although `NaN`s are not
7848 /// comparable to any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
7849 ///
7850 /// If the power is equidistant from two [`Float`]s with the specified precision, the [`Float`]
7851 /// with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of
7852 /// the `Nearest` rounding mode.
7853 ///
7854 /// $$
7855 /// f(x,y,p) = x^y+\varepsilon.
7856 /// $$
7857 /// - If $x^y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
7858 /// - If $x^y$ is finite and nonzero, then $|\varepsilon| \leq 2^{\lfloor\log_2
7859 /// |x^y|\rfloor-p}$.
7860 ///
7861 /// If the output has a precision, it is `prec`.
7862 ///
7863 /// Special cases:
7864 /// - $f(x,\pm0.0,p)=1.0$ for any $x$, even $0$
7865 /// - $f(1,y,p)=1.0$ for any $y$, even `NaN`
7866 /// - $f(x,\text{NaN},p)=\text{NaN}$ otherwise
7867 /// - $f(x,\infty,p)=\infty$ if $|x|>1$, and $0.0$ if $|x|<1$
7868 /// - $f(x,-\infty,p)=0.0$ if $|x|>1$, and $\infty$ if $|x|<1$
7869 /// - $f(\pm1,\pm\infty,p)=1.0$
7870 /// - $f(-1,y,p)=1.0$ if $y$ is an even integer, and $-1.0$ if $y$ is an odd integer
7871 /// - $f(0,y,p)=0.0$ if $y>0$, and $\infty$ if $y<0$; a [`Rational`] zero is unsigned, so the
7872 /// results take positive signs
7873 /// - $f(x,y,p)=\text{NaN}$ if $x<0$ and $y$ is finite and not an integer
7874 ///
7875 /// Unlike a [`Float`] base, a [`Rational`] base may lie outside the [`Float`] exponent range or
7876 /// so close to 1 that no [`Float`] can represent its logarithm; both cases are handled exactly,
7877 /// by working with the base as an exact [`Rational`] throughout.
7878 ///
7879 /// Overflow and underflow:
7880 /// - If $f(x,y,p)\geq 2^{2^{30}-1}$, $\infty$ is returned instead.
7881 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
7882 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
7883 /// - Negative results (from negative $x$ and odd integer $y$) mirror the bullets above.
7884 ///
7885 /// If you want to use a rounding mode other than `Nearest`, consider using
7886 /// [`Float::rational_pow_prec_round_ref_val`] instead.
7887 ///
7888 /// # Worst-case complexity
7889 /// $T(n) = O(n^{3/2} \log n \log\log n)$
7890 ///
7891 /// $M(n) = O(n (\log n)^2)$
7892 ///
7893 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(prec, x.significant_bits(),
7894 /// y.significant_bits())`.
7895 ///
7896 /// # Examples
7897 /// ```
7898 /// use malachite_float::Float;
7899 /// use malachite_q::Rational;
7900 /// use std::cmp::Ordering::*;
7901 ///
7902 /// let (p, o) = Float::rational_pow_prec_ref_val(
7903 /// &Rational::from_unsigneds(3u32, 2u32),
7904 /// Float::from(2.5),
7905 /// 5,
7906 /// );
7907 /// assert_eq!(p.to_string(), "2.75");
7908 /// assert_eq!(o, Less);
7909 ///
7910 /// let (p, o) = Float::rational_pow_prec_ref_val(
7911 /// &Rational::from_unsigneds(3u32, 2u32),
7912 /// Float::from(2.5),
7913 /// 20,
7914 /// );
7915 /// assert_eq!(p.to_string(), "2.7556763");
7916 /// assert_eq!(o, Greater);
7917 /// ```
7918 #[inline]
7919 pub fn rational_pow_prec_ref_val(x: &Rational, y: Self, prec: u64) -> (Self, Ordering) {
7920 Self::rational_pow_prec_ref_ref(x, &y, prec)
7921 }
7922
7923 /// Raises a [`Rational`] to a [`Float`] power, returning the result as a [`Float`] rounded to
7924 /// the specified precision and to the nearest value. The [`Rational`] and the [`Float`] are
7925 /// both taken by reference. An [`Ordering`] is also returned, indicating whether the rounded
7926 /// power is less than, equal to, or greater than the exact power. Although `NaN`s are not
7927 /// comparable to any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
7928 ///
7929 /// If the power is equidistant from two [`Float`]s with the specified precision, the [`Float`]
7930 /// with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of
7931 /// the `Nearest` rounding mode.
7932 ///
7933 /// $$
7934 /// f(x,y,p) = x^y+\varepsilon.
7935 /// $$
7936 /// - If $x^y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
7937 /// - If $x^y$ is finite and nonzero, then $|\varepsilon| \leq 2^{\lfloor\log_2
7938 /// |x^y|\rfloor-p}$.
7939 ///
7940 /// If the output has a precision, it is `prec`.
7941 ///
7942 /// Special cases:
7943 /// - $f(x,\pm0.0,p)=1.0$ for any $x$, even $0$
7944 /// - $f(1,y,p)=1.0$ for any $y$, even `NaN`
7945 /// - $f(x,\text{NaN},p)=\text{NaN}$ otherwise
7946 /// - $f(x,\infty,p)=\infty$ if $|x|>1$, and $0.0$ if $|x|<1$
7947 /// - $f(x,-\infty,p)=0.0$ if $|x|>1$, and $\infty$ if $|x|<1$
7948 /// - $f(\pm1,\pm\infty,p)=1.0$
7949 /// - $f(-1,y,p)=1.0$ if $y$ is an even integer, and $-1.0$ if $y$ is an odd integer
7950 /// - $f(0,y,p)=0.0$ if $y>0$, and $\infty$ if $y<0$; a [`Rational`] zero is unsigned, so the
7951 /// results take positive signs
7952 /// - $f(x,y,p)=\text{NaN}$ if $x<0$ and $y$ is finite and not an integer
7953 ///
7954 /// Unlike a [`Float`] base, a [`Rational`] base may lie outside the [`Float`] exponent range or
7955 /// so close to 1 that no [`Float`] can represent its logarithm; both cases are handled exactly,
7956 /// by working with the base as an exact [`Rational`] throughout.
7957 ///
7958 /// Overflow and underflow:
7959 /// - If $f(x,y,p)\geq 2^{2^{30}-1}$, $\infty$ is returned instead.
7960 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
7961 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
7962 /// - Negative results (from negative $x$ and odd integer $y$) mirror the bullets above.
7963 ///
7964 /// If you want to use a rounding mode other than `Nearest`, consider using
7965 /// [`Float::rational_pow_prec_round_ref_ref`] instead.
7966 ///
7967 /// # Worst-case complexity
7968 /// $T(n) = O(n^{3/2} \log n \log\log n)$
7969 ///
7970 /// $M(n) = O(n (\log n)^2)$
7971 ///
7972 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(prec, x.significant_bits(),
7973 /// y.significant_bits())`.
7974 ///
7975 /// # Examples
7976 /// ```
7977 /// use malachite_float::Float;
7978 /// use malachite_q::Rational;
7979 /// use std::cmp::Ordering::*;
7980 ///
7981 /// let (p, o) = Float::rational_pow_prec_ref_ref(
7982 /// &Rational::from_unsigneds(3u32, 2u32),
7983 /// &Float::from(2.5),
7984 /// 5,
7985 /// );
7986 /// assert_eq!(p.to_string(), "2.75");
7987 /// assert_eq!(o, Less);
7988 ///
7989 /// let (p, o) = Float::rational_pow_prec_ref_ref(
7990 /// &Rational::from_unsigneds(3u32, 2u32),
7991 /// &Float::from(2.5),
7992 /// 20,
7993 /// );
7994 /// assert_eq!(p.to_string(), "2.7556763");
7995 /// assert_eq!(o, Greater);
7996 /// ```
7997 #[inline]
7998 pub fn rational_pow_prec_ref_ref(x: &Rational, y: &Self, prec: u64) -> (Self, Ordering) {
7999 Self::rational_pow_prec_round_ref_ref(x, y, prec, Nearest)
8000 }
8001}
8002
8003impl Float {
8004 /// Raises a [`Float`] to the power of a [`Rational`], rounding the result to the specified
8005 /// precision and with the specified rounding mode. Both the [`Float`] and the [`Rational`] are
8006 /// taken by value. An [`Ordering`] is also returned, indicating whether the rounded power is
8007 /// less than, equal to, or greater than the exact power. Although `NaN`s are not comparable to
8008 /// any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
8009 ///
8010 /// See [`RoundingMode`] for a description of the possible rounding modes.
8011 ///
8012 /// $$
8013 /// f(x,y,p,m) = x^y+\varepsilon.
8014 /// $$
8015 /// - If $x^y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
8016 /// - If $x^y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
8017 /// 2^{\lfloor\log_2 |x^y|\rfloor-p+1}$.
8018 /// - If $x^y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
8019 /// 2^{\lfloor\log_2 |x^y|\rfloor-p}$.
8020 ///
8021 /// If the output has a precision, it is `prec`.
8022 ///
8023 /// Special cases:
8024 /// - $f(x,0,p,m)=1.0$ for any $x$, even `NaN`
8025 /// - $f(\text{NaN},y,p,m)=\text{NaN}$ if $y \neq 0$
8026 /// - $f(x,y,p,m)=\text{NaN}$ if $x<0$ and $y$ is not an integer
8027 /// - $f(1.0,y,p,m)=1.0$
8028 /// - $f(-1.0,y,p,m)=1.0$ if $y$ is an even integer, and $-1.0$ if $y$ is an odd integer
8029 /// - $f(\infty,y,p,m)=\infty$ if $y>0$, and $0.0$ if $y<0$
8030 /// - $f(-\infty,y,p,m)=-\infty$ if $y$ is a positive odd integer, $\infty$ if $y$ is positive
8031 /// and not an odd integer, $-0.0$ if $y$ is a negative odd integer, and $0.0$ if $y$ is
8032 /// negative and not an odd integer
8033 /// - $f(0.0,y,p,m)=0.0$ if $y>0$, and $\infty$ if $y<0$
8034 /// - $f(-0.0,y,p,m)=-0.0$ if $y$ is a positive odd integer, $0.0$ if $y$ is positive and not an
8035 /// odd integer, $-\infty$ if $y$ is a negative odd integer, and $\infty$ if $y$ is negative
8036 /// and not an odd integer
8037 ///
8038 /// Unlike the exponent of a [`Float`], the exact [`Rational`] exponent selects a definite
8039 /// branch of the power, so results that are exactly representable (such as roots of perfect
8040 /// powers) are detected and rounded exactly.
8041 ///
8042 /// Overflow and underflow:
8043 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
8044 /// returned instead.
8045 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor` or `Down`, $(1-(1/2)^p)2^{2^{30}-1}$
8046 /// is returned instead.
8047 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
8048 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
8049 /// instead.
8050 /// - If $0<f(x,y,p,m)\leq2^{-2^{30}-1}$ and $m$ is `Nearest`, $0.0$ is returned instead.
8051 /// - If $2^{-2^{30}-1}<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
8052 /// instead.
8053 /// - Negative results (from negative $x$ and odd integer $y$) mirror the bullets above, with
8054 /// the rounding directions reflected.
8055 ///
8056 /// # Worst-case complexity
8057 /// $T(n) = O(n^{3/2} \log n \log\log n)$
8058 ///
8059 /// $M(n) = O(n (\log n)^2)$
8060 ///
8061 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(prec, self.significant_bits(),
8062 /// other.significant_bits())`.
8063 ///
8064 /// # Panics
8065 /// Panics if `prec` is zero, or if `rm` is `Exact` but the result cannot be represented exactly
8066 /// with the given precision.
8067 ///
8068 /// # Examples
8069 /// ```
8070 /// use malachite_base::rounding_modes::RoundingMode::*;
8071 /// use malachite_float::Float;
8072 /// use malachite_q::Rational;
8073 /// use std::cmp::Ordering::*;
8074 ///
8075 /// let (p, o) =
8076 /// Float::from(2).pow_rational_prec_round(Rational::from_signeds(3, 2), 20, Floor);
8077 /// assert_eq!(p.to_string(), "2.8284264");
8078 /// assert_eq!(o, Less);
8079 ///
8080 /// let (p, o) =
8081 /// Float::from(2).pow_rational_prec_round(Rational::from_signeds(3, 2), 20, Ceiling);
8082 /// assert_eq!(p.to_string(), "2.8284302");
8083 /// assert_eq!(o, Greater);
8084 ///
8085 /// let (p, o) =
8086 /// Float::from(8).pow_rational_prec_round(Rational::from_signeds(1, 3), 20, Floor);
8087 /// assert_eq!(p.to_string(), "2.0000000");
8088 /// assert_eq!(o, Equal);
8089 /// ```
8090 #[allow(clippy::needless_pass_by_value)]
8091 #[inline]
8092 pub fn pow_rational_prec_round(
8093 self,
8094 other: Rational,
8095 prec: u64,
8096 rm: RoundingMode,
8097 ) -> (Self, Ordering) {
8098 float_rational_pow(&self, &other, prec, rm)
8099 }
8100
8101 /// Raises a [`Float`] to the power of a [`Rational`], rounding the result to the specified
8102 /// precision and with the specified rounding mode. The [`Float`] is taken by value and the
8103 /// [`Rational`] by reference. An [`Ordering`] is also returned, indicating whether the rounded
8104 /// power is less than, equal to, or greater than the exact power. Although `NaN`s are not
8105 /// comparable to any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
8106 ///
8107 /// See [`RoundingMode`] for a description of the possible rounding modes.
8108 ///
8109 /// See the [`Float::pow_rational_prec_round`] documentation for information on special cases,
8110 /// overflow, and underflow.
8111 #[inline]
8112 pub fn pow_rational_prec_round_val_ref(
8113 self,
8114 other: &Rational,
8115 prec: u64,
8116 rm: RoundingMode,
8117 ) -> (Self, Ordering) {
8118 float_rational_pow(&self, other, prec, rm)
8119 }
8120
8121 /// Raises a [`Float`] to the power of a [`Rational`], rounding the result to the specified
8122 /// precision and with the specified rounding mode. The [`Float`] is taken by reference and the
8123 /// [`Rational`] by value. An [`Ordering`] is also returned, indicating whether the rounded
8124 /// power is less than, equal to, or greater than the exact power. Although `NaN`s are not
8125 /// comparable to any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
8126 ///
8127 /// See [`RoundingMode`] for a description of the possible rounding modes.
8128 ///
8129 /// See the [`Float::pow_rational_prec_round`] documentation for information on special cases,
8130 /// overflow, and underflow.
8131 #[allow(clippy::needless_pass_by_value)]
8132 #[inline]
8133 pub fn pow_rational_prec_round_ref_val(
8134 &self,
8135 other: Rational,
8136 prec: u64,
8137 rm: RoundingMode,
8138 ) -> (Self, Ordering) {
8139 float_rational_pow(self, &other, prec, rm)
8140 }
8141
8142 /// Raises a [`Float`] to the power of a [`Rational`], rounding the result to the specified
8143 /// precision and with the specified rounding mode. Both the [`Float`] and the [`Rational`] are
8144 /// taken by reference. An [`Ordering`] is also returned, indicating whether the rounded power
8145 /// is less than, equal to, or greater than the exact power. Although `NaN`s are not comparable
8146 /// to any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
8147 ///
8148 /// See [`RoundingMode`] for a description of the possible rounding modes.
8149 ///
8150 /// See the [`Float::pow_rational_prec_round`] documentation for information on special cases,
8151 /// overflow, and underflow.
8152 #[inline]
8153 pub fn pow_rational_prec_round_ref_ref(
8154 &self,
8155 other: &Rational,
8156 prec: u64,
8157 rm: RoundingMode,
8158 ) -> (Self, Ordering) {
8159 float_rational_pow(self, other, prec, rm)
8160 }
8161
8162 /// Raises a [`Float`] to the power of a [`Rational`], rounding the result to the specified
8163 /// precision and to the nearest value. Both the [`Float`] and the [`Rational`] are taken by
8164 /// value. An [`Ordering`] is also returned, indicating whether the rounded power is less than,
8165 /// equal to, or greater than the exact power. Although `NaN`s are not comparable to any
8166 /// [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
8167 ///
8168 /// If the power is equidistant from two [`Float`]s with the specified precision, the [`Float`]
8169 /// with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of
8170 /// the `Nearest` rounding mode.
8171 ///
8172 /// $$
8173 /// f(x,y,p,m) = x^y+\varepsilon.
8174 /// $$
8175 /// - If $x^y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
8176 /// - If $x^y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
8177 /// 2^{\lfloor\log_2 |x^y|\rfloor-p+1}$.
8178 /// - If $x^y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
8179 /// 2^{\lfloor\log_2 |x^y|\rfloor-p}$.
8180 ///
8181 /// If the output has a precision, it is `prec`.
8182 ///
8183 /// Special cases:
8184 /// - $f(x,0,p,m)=1.0$ for any $x$, even `NaN`
8185 /// - $f(\text{NaN},y,p,m)=\text{NaN}$ if $y \neq 0$
8186 /// - $f(x,y,p,m)=\text{NaN}$ if $x<0$ and $y$ is not an integer
8187 /// - $f(1.0,y,p,m)=1.0$
8188 /// - $f(-1.0,y,p,m)=1.0$ if $y$ is an even integer, and $-1.0$ if $y$ is an odd integer
8189 /// - $f(\infty,y,p,m)=\infty$ if $y>0$, and $0.0$ if $y<0$
8190 /// - $f(-\infty,y,p,m)=-\infty$ if $y$ is a positive odd integer, $\infty$ if $y$ is positive
8191 /// and not an odd integer, $-0.0$ if $y$ is a negative odd integer, and $0.0$ if $y$ is
8192 /// negative and not an odd integer
8193 /// - $f(0.0,y,p,m)=0.0$ if $y>0$, and $\infty$ if $y<0$
8194 /// - $f(-0.0,y,p,m)=-0.0$ if $y$ is a positive odd integer, $0.0$ if $y$ is positive and not an
8195 /// odd integer, $-\infty$ if $y$ is a negative odd integer, and $\infty$ if $y$ is negative
8196 /// and not an odd integer
8197 ///
8198 /// Unlike the exponent of a [`Float`], the exact [`Rational`] exponent selects a definite
8199 /// branch of the power, so results that are exactly representable (such as roots of perfect
8200 /// powers) are detected and rounded exactly.
8201 ///
8202 /// Overflow and underflow:
8203 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
8204 /// returned instead.
8205 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor` or `Down`, $(1-(1/2)^p)2^{2^{30}-1}$
8206 /// is returned instead.
8207 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
8208 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
8209 /// instead.
8210 /// - If $0<f(x,y,p,m)\leq2^{-2^{30}-1}$ and $m$ is `Nearest`, $0.0$ is returned instead.
8211 /// - If $2^{-2^{30}-1}<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
8212 /// instead.
8213 /// - Negative results (from negative $x$ and odd integer $y$) mirror the bullets above, with
8214 /// the rounding directions reflected.
8215 ///
8216 /// # Worst-case complexity
8217 /// $T(n) = O(n^{3/2} \log n \log\log n)$
8218 ///
8219 /// $M(n) = O(n (\log n)^2)$
8220 ///
8221 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(prec, self.significant_bits(),
8222 /// other.significant_bits())`.
8223 ///
8224 /// # Panics
8225 /// Panics if `prec` is zero.
8226 ///
8227 /// # Examples
8228 /// ```
8229 /// use malachite_float::Float;
8230 /// use malachite_q::Rational;
8231 /// use std::cmp::Ordering::*;
8232 ///
8233 /// let (p, o) = Float::from(2).pow_rational_prec(Rational::from_signeds(3, 2), 20);
8234 /// assert_eq!(p.to_string(), "2.8284264");
8235 /// assert_eq!(o, Less);
8236 ///
8237 /// let (p, o) = Float::from(27).pow_rational_prec(Rational::from_signeds(2, 3), 20);
8238 /// assert_eq!(p.to_string(), "9.0000000");
8239 /// assert_eq!(o, Equal);
8240 /// ```
8241 #[inline]
8242 pub fn pow_rational_prec(self, other: Rational, prec: u64) -> (Self, Ordering) {
8243 self.pow_rational_prec_round(other, prec, Nearest)
8244 }
8245
8246 /// Raises a [`Float`] to the power of a [`Rational`], rounding the result to the specified
8247 /// precision and to the nearest value. The [`Float`] is taken by value and the [`Rational`] by
8248 /// reference. An [`Ordering`] is also returned, indicating whether the rounded power is less
8249 /// than, equal to, or greater than the exact power. Although `NaN`s are not comparable to any
8250 /// [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
8251 ///
8252 /// If the power is equidistant from two [`Float`]s with the specified precision, the [`Float`]
8253 /// with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of
8254 /// the `Nearest` rounding mode.
8255 ///
8256 /// See the [`Float::pow_rational_prec_round`] documentation for information on special cases,
8257 /// overflow, and underflow.
8258 #[inline]
8259 pub fn pow_rational_prec_val_ref(self, other: &Rational, prec: u64) -> (Self, Ordering) {
8260 self.pow_rational_prec_round_val_ref(other, prec, Nearest)
8261 }
8262
8263 /// Raises a [`Float`] to the power of a [`Rational`], rounding the result to the specified
8264 /// precision and to the nearest value. The [`Float`] is taken by reference and the [`Rational`]
8265 /// by value. An [`Ordering`] is also returned, indicating whether the rounded power is less
8266 /// than, equal to, or greater than the exact power. Although `NaN`s are not comparable to any
8267 /// [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
8268 ///
8269 /// If the power is equidistant from two [`Float`]s with the specified precision, the [`Float`]
8270 /// with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of
8271 /// the `Nearest` rounding mode.
8272 ///
8273 /// See the [`Float::pow_rational_prec_round`] documentation for information on special cases,
8274 /// overflow, and underflow.
8275 #[inline]
8276 pub fn pow_rational_prec_ref_val(&self, other: Rational, prec: u64) -> (Self, Ordering) {
8277 self.pow_rational_prec_round_ref_val(other, prec, Nearest)
8278 }
8279
8280 /// Raises a [`Float`] to the power of a [`Rational`], rounding the result to the specified
8281 /// precision and to the nearest value. Both the [`Float`] and the [`Rational`] are taken by
8282 /// reference. An [`Ordering`] is also returned, indicating whether the rounded power is less
8283 /// than, equal to, or greater than the exact power. Although `NaN`s are not comparable to any
8284 /// [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
8285 ///
8286 /// If the power is equidistant from two [`Float`]s with the specified precision, the [`Float`]
8287 /// with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of
8288 /// the `Nearest` rounding mode.
8289 ///
8290 /// See the [`Float::pow_rational_prec_round`] documentation for information on special cases,
8291 /// overflow, and underflow.
8292 #[inline]
8293 pub fn pow_rational_prec_ref_ref(&self, other: &Rational, prec: u64) -> (Self, Ordering) {
8294 self.pow_rational_prec_round_ref_ref(other, prec, Nearest)
8295 }
8296
8297 /// Raises a [`Float`] to the power of a [`Rational`], rounding the result to the precision of
8298 /// the base and with the specified rounding mode. Both the [`Float`] and the [`Rational`] are
8299 /// taken by value. An [`Ordering`] is also returned, indicating whether the rounded power is
8300 /// less than, equal to, or greater than the exact power. Although `NaN`s are not comparable to
8301 /// any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
8302 ///
8303 /// The output precision is the precision of `self`. See [`RoundingMode`] for a description of
8304 /// the possible rounding modes.
8305 ///
8306 /// $$
8307 /// f(x,y,p,m) = x^y+\varepsilon.
8308 /// $$
8309 /// - If $x^y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
8310 /// - If $x^y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
8311 /// 2^{\lfloor\log_2 |x^y|\rfloor-p+1}$.
8312 /// - If $x^y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
8313 /// 2^{\lfloor\log_2 |x^y|\rfloor-p}$.
8314 ///
8315 /// If the output has a precision, it is `prec`.
8316 ///
8317 /// Special cases:
8318 /// - $f(x,0,p,m)=1.0$ for any $x$, even `NaN`
8319 /// - $f(\text{NaN},y,p,m)=\text{NaN}$ if $y \neq 0$
8320 /// - $f(x,y,p,m)=\text{NaN}$ if $x<0$ and $y$ is not an integer
8321 /// - $f(1.0,y,p,m)=1.0$
8322 /// - $f(-1.0,y,p,m)=1.0$ if $y$ is an even integer, and $-1.0$ if $y$ is an odd integer
8323 /// - $f(\infty,y,p,m)=\infty$ if $y>0$, and $0.0$ if $y<0$
8324 /// - $f(-\infty,y,p,m)=-\infty$ if $y$ is a positive odd integer, $\infty$ if $y$ is positive
8325 /// and not an odd integer, $-0.0$ if $y$ is a negative odd integer, and $0.0$ if $y$ is
8326 /// negative and not an odd integer
8327 /// - $f(0.0,y,p,m)=0.0$ if $y>0$, and $\infty$ if $y<0$
8328 /// - $f(-0.0,y,p,m)=-0.0$ if $y$ is a positive odd integer, $0.0$ if $y$ is positive and not an
8329 /// odd integer, $-\infty$ if $y$ is a negative odd integer, and $\infty$ if $y$ is negative
8330 /// and not an odd integer
8331 ///
8332 /// Unlike the exponent of a [`Float`], the exact [`Rational`] exponent selects a definite
8333 /// branch of the power, so results that are exactly representable (such as roots of perfect
8334 /// powers) are detected and rounded exactly.
8335 ///
8336 /// Overflow and underflow:
8337 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
8338 /// returned instead.
8339 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor` or `Down`, $(1-(1/2)^p)2^{2^{30}-1}$
8340 /// is returned instead.
8341 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
8342 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
8343 /// instead.
8344 /// - If $0<f(x,y,p,m)\leq2^{-2^{30}-1}$ and $m$ is `Nearest`, $0.0$ is returned instead.
8345 /// - If $2^{-2^{30}-1}<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
8346 /// instead.
8347 /// - Negative results (from negative $x$ and odd integer $y$) mirror the bullets above, with
8348 /// the rounding directions reflected.
8349 ///
8350 /// # Worst-case complexity
8351 /// $T(n) = O(n^{3/2} \log n \log\log n)$
8352 ///
8353 /// $M(n) = O(n (\log n)^2)$
8354 ///
8355 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(prec, self.significant_bits(),
8356 /// other.significant_bits())`.
8357 ///
8358 /// # Panics
8359 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the base's
8360 /// precision.
8361 ///
8362 /// # Examples
8363 /// ```
8364 /// use malachite_base::rounding_modes::RoundingMode::*;
8365 /// use malachite_float::Float;
8366 /// use malachite_q::Rational;
8367 /// use std::cmp::Ordering::*;
8368 ///
8369 /// // The output precision is the precision of the base, here 3 bits.
8370 /// let (p, o) = Float::from(5).pow_rational_round(Rational::from_signeds(3, 2), Floor);
8371 /// assert_eq!(p.to_string(), "10.0");
8372 /// assert_eq!(o, Less);
8373 ///
8374 /// let (p, o) = Float::from(5).pow_rational_round(Rational::from_signeds(3, 2), Ceiling);
8375 /// assert_eq!(p.to_string(), "12.0");
8376 /// assert_eq!(o, Greater);
8377 /// ```
8378 pub fn pow_rational_round(self, other: Rational, rm: RoundingMode) -> (Self, Ordering) {
8379 let prec = self.significant_bits();
8380 self.pow_rational_prec_round(other, prec, rm)
8381 }
8382
8383 /// Raises a [`Float`] to the power of a [`Rational`], rounding the result to the precision of
8384 /// the base and with the specified rounding mode. The [`Float`] is taken by value and the
8385 /// [`Rational`] by reference. An [`Ordering`] is also returned, indicating whether the rounded
8386 /// power is less than, equal to, or greater than the exact power. Although `NaN`s are not
8387 /// comparable to any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
8388 ///
8389 /// The output precision is the precision of `self`. See [`RoundingMode`] for a description of
8390 /// the possible rounding modes.
8391 ///
8392 /// See the [`Float::pow_rational_prec_round`] documentation for information on special cases,
8393 /// overflow, and underflow.
8394 pub fn pow_rational_round_val_ref(
8395 self,
8396 other: &Rational,
8397 rm: RoundingMode,
8398 ) -> (Self, Ordering) {
8399 let prec = self.significant_bits();
8400 self.pow_rational_prec_round_val_ref(other, prec, rm)
8401 }
8402
8403 /// Raises a [`Float`] to the power of a [`Rational`], rounding the result to the precision of
8404 /// the base and with the specified rounding mode. The [`Float`] is taken by reference and the
8405 /// [`Rational`] by value. An [`Ordering`] is also returned, indicating whether the rounded
8406 /// power is less than, equal to, or greater than the exact power. Although `NaN`s are not
8407 /// comparable to any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
8408 ///
8409 /// The output precision is the precision of `self`. See [`RoundingMode`] for a description of
8410 /// the possible rounding modes.
8411 ///
8412 /// See the [`Float::pow_rational_prec_round`] documentation for information on special cases,
8413 /// overflow, and underflow.
8414 pub fn pow_rational_round_ref_val(
8415 &self,
8416 other: Rational,
8417 rm: RoundingMode,
8418 ) -> (Self, Ordering) {
8419 let prec = self.significant_bits();
8420 self.pow_rational_prec_round_ref_val(other, prec, rm)
8421 }
8422
8423 /// Raises a [`Float`] to the power of a [`Rational`], rounding the result to the precision of
8424 /// the base and with the specified rounding mode. Both the [`Float`] and the [`Rational`] are
8425 /// taken by reference. An [`Ordering`] is also returned, indicating whether the rounded power
8426 /// is less than, equal to, or greater than the exact power. Although `NaN`s are not comparable
8427 /// to any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
8428 ///
8429 /// The output precision is the precision of `self`. See [`RoundingMode`] for a description of
8430 /// the possible rounding modes.
8431 ///
8432 /// See the [`Float::pow_rational_prec_round`] documentation for information on special cases,
8433 /// overflow, and underflow.
8434 pub fn pow_rational_round_ref_ref(
8435 &self,
8436 other: &Rational,
8437 rm: RoundingMode,
8438 ) -> (Self, Ordering) {
8439 let prec = self.significant_bits();
8440 self.pow_rational_prec_round_ref_ref(other, prec, rm)
8441 }
8442
8443 /// Raises a [`Float`] to the power of a [`Rational`] in place, taking the [`Rational`] by
8444 /// value.
8445 ///
8446 /// See the [`Float::pow_rational_prec_round`] documentation for information on special cases,
8447 /// overflow, and underflow.
8448 ///
8449 /// # Worst-case complexity
8450 /// $T(n) = O(n^{3/2} \log n \log\log n)$
8451 ///
8452 /// $M(n) = O(n (\log n)^2)$
8453 ///
8454 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(prec, self.significant_bits(),
8455 /// other.significant_bits())`.
8456 ///
8457 /// # Panics
8458 /// Panics if `prec` is zero, or if `rm` is `Exact` but the result cannot be represented exactly
8459 /// with the given precision.
8460 #[allow(clippy::needless_pass_by_value)]
8461 pub fn pow_rational_prec_round_assign(
8462 &mut self,
8463 other: Rational,
8464 prec: u64,
8465 rm: RoundingMode,
8466 ) -> Ordering {
8467 let (result, o) = float_rational_pow(self, &other, prec, rm);
8468 *self = result;
8469 o
8470 }
8471
8472 /// Raises a [`Float`] to the power of a [`Rational`] in place, taking the [`Rational`] by
8473 /// reference.
8474 ///
8475 /// See the [`Float::pow_rational_prec_round`] documentation for information on special cases,
8476 /// overflow, and underflow.
8477 ///
8478 /// # Worst-case complexity
8479 /// $T(n) = O(n^{3/2} \log n \log\log n)$
8480 ///
8481 /// $M(n) = O(n (\log n)^2)$
8482 ///
8483 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(prec, self.significant_bits(),
8484 /// other.significant_bits())`.
8485 ///
8486 /// # Panics
8487 /// Panics if `prec` is zero, or if `rm` is `Exact` but the result cannot be represented exactly
8488 /// with the given precision.
8489 pub fn pow_rational_prec_round_assign_ref(
8490 &mut self,
8491 other: &Rational,
8492 prec: u64,
8493 rm: RoundingMode,
8494 ) -> Ordering {
8495 let (result, o) = float_rational_pow(self, other, prec, rm);
8496 *self = result;
8497 o
8498 }
8499
8500 /// Raises a [`Float`] to the power of a [`Rational`] in place, taking the [`Rational`] by
8501 /// value.
8502 ///
8503 /// See the [`Float::pow_rational_prec_round`] documentation for information on special cases,
8504 /// overflow, and underflow.
8505 ///
8506 /// # Worst-case complexity
8507 /// $T(n) = O(n^{3/2} \log n \log\log n)$
8508 ///
8509 /// $M(n) = O(n (\log n)^2)$
8510 ///
8511 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(prec, self.significant_bits(),
8512 /// other.significant_bits())`.
8513 ///
8514 /// # Panics
8515 /// Panics if `prec` is zero.
8516 #[inline]
8517 pub fn pow_rational_prec_assign(&mut self, other: Rational, prec: u64) -> Ordering {
8518 self.pow_rational_prec_round_assign(other, prec, Nearest)
8519 }
8520
8521 /// Raises a [`Float`] to the power of a [`Rational`] in place, taking the [`Rational`] by
8522 /// reference.
8523 ///
8524 /// See the [`Float::pow_rational_prec_round`] documentation for information on special cases,
8525 /// overflow, and underflow.
8526 ///
8527 /// # Worst-case complexity
8528 /// $T(n) = O(n^{3/2} \log n \log\log n)$
8529 ///
8530 /// $M(n) = O(n (\log n)^2)$
8531 ///
8532 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(prec, self.significant_bits(),
8533 /// other.significant_bits())`.
8534 ///
8535 /// # Panics
8536 /// Panics if `prec` is zero.
8537 #[inline]
8538 pub fn pow_rational_prec_assign_ref(&mut self, other: &Rational, prec: u64) -> Ordering {
8539 self.pow_rational_prec_round_assign_ref(other, prec, Nearest)
8540 }
8541
8542 /// Raises a [`Float`] to the power of a [`Rational`] in place, taking the [`Rational`] by
8543 /// value.
8544 ///
8545 /// See the [`Float::pow_rational_prec_round`] documentation for information on special cases,
8546 /// overflow, and underflow.
8547 ///
8548 /// # Worst-case complexity
8549 /// $T(n) = O(n^{3/2} \log n \log\log n)$
8550 ///
8551 /// $M(n) = O(n (\log n)^2)$
8552 ///
8553 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(prec, self.significant_bits(),
8554 /// other.significant_bits())`.
8555 ///
8556 /// # Panics
8557 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the base's
8558 /// precision.
8559 pub fn pow_rational_round_assign(&mut self, other: Rational, rm: RoundingMode) -> Ordering {
8560 let prec = self.significant_bits();
8561 self.pow_rational_prec_round_assign(other, prec, rm)
8562 }
8563
8564 /// Raises a [`Float`] to the power of a [`Rational`] in place, taking the [`Rational`] by
8565 /// reference.
8566 ///
8567 /// See the [`Float::pow_rational_prec_round`] documentation for information on special cases,
8568 /// overflow, and underflow.
8569 ///
8570 /// # Worst-case complexity
8571 /// $T(n) = O(n^{3/2} \log n \log\log n)$
8572 ///
8573 /// $M(n) = O(n (\log n)^2)$
8574 ///
8575 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(prec, self.significant_bits(),
8576 /// other.significant_bits())`.
8577 ///
8578 /// # Panics
8579 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the base's
8580 /// precision.
8581 pub fn pow_rational_round_assign_ref(
8582 &mut self,
8583 other: &Rational,
8584 rm: RoundingMode,
8585 ) -> Ordering {
8586 let prec = self.significant_bits();
8587 self.pow_rational_prec_round_assign_ref(other, prec, rm)
8588 }
8589}
8590
8591impl Pow<Rational> for Float {
8592 type Output = Self;
8593
8594 /// Raises a [`Float`] to the power of a [`Rational`], rounding the result to the nearest value
8595 /// at the precision of the base. Both the [`Float`] and the [`Rational`] are taken by value.
8596 ///
8597 /// See the [`Float::pow_rational_prec_round`] documentation for information on special cases,
8598 /// overflow, and underflow.
8599 #[inline]
8600 fn pow(self, other: Rational) -> Self {
8601 let prec = self.significant_bits();
8602 self.pow_rational_prec_round(other, prec, Nearest).0
8603 }
8604}
8605
8606impl Pow<&Rational> for Float {
8607 type Output = Self;
8608
8609 /// Raises a [`Float`] to the power of a [`Rational`], rounding the result to the nearest value
8610 /// at the precision of the base. The [`Float`] is taken by value and the [`Rational`] by
8611 /// reference.
8612 ///
8613 /// See the [`Float::pow_rational_prec_round`] documentation for information on special cases,
8614 /// overflow, and underflow.
8615 #[inline]
8616 fn pow(self, other: &Rational) -> Self {
8617 let prec = self.significant_bits();
8618 self.pow_rational_prec_round_val_ref(other, prec, Nearest).0
8619 }
8620}
8621
8622impl Pow<Rational> for &Float {
8623 type Output = Float;
8624
8625 /// Raises a [`Float`] to the power of a [`Rational`], rounding the result to the nearest value
8626 /// at the precision of the base. The [`Float`] is taken by reference and the [`Rational`] by
8627 /// value.
8628 ///
8629 /// See the [`Float::pow_rational_prec_round`] documentation for information on special cases,
8630 /// overflow, and underflow.
8631 #[inline]
8632 fn pow(self, other: Rational) -> Float {
8633 let prec = self.significant_bits();
8634 self.pow_rational_prec_round_ref_val(other, prec, Nearest).0
8635 }
8636}
8637
8638impl Pow<&Rational> for &Float {
8639 type Output = Float;
8640
8641 /// Raises a [`Float`] to the power of a [`Rational`], rounding the result to the nearest value
8642 /// at the precision of the base. Both the [`Float`] and the [`Rational`] are taken by
8643 /// reference.
8644 ///
8645 /// See the [`Float::pow_rational_prec_round`] documentation for information on special cases,
8646 /// overflow, and underflow.
8647 #[inline]
8648 fn pow(self, other: &Rational) -> Float {
8649 let prec = self.significant_bits();
8650 self.pow_rational_prec_round_ref_ref(other, prec, Nearest).0
8651 }
8652}
8653
8654impl PowAssign<Rational> for Float {
8655 /// Raises a [`Float`] to the power of a [`Rational`] in place, taking the [`Rational`] by
8656 /// value, and rounding the result to the nearest value at the precision of the base.
8657 ///
8658 /// See the [`Float::pow_rational_prec_round`] documentation for information on special cases,
8659 /// overflow, and underflow.
8660 #[inline]
8661 fn pow_assign(&mut self, other: Rational) {
8662 let prec = self.significant_bits();
8663 self.pow_rational_prec_assign(other, prec);
8664 }
8665}
8666
8667impl PowAssign<&Rational> for Float {
8668 /// Raises a [`Float`] to the power of a [`Rational`] in place, taking the [`Rational`] by
8669 /// reference, and rounding the result to the nearest value at the precision of the base.
8670 ///
8671 /// See the [`Float::pow_rational_prec_round`] documentation for information on special cases,
8672 /// overflow, and underflow.
8673 #[inline]
8674 fn pow_assign(&mut self, other: &Rational) {
8675 let prec = self.significant_bits();
8676 self.pow_rational_prec_assign_ref(other, prec);
8677 }
8678}
8679
8680impl Float {
8681 /// Raises a [`Float`] to a [`Float`] power using the IEEE 754 `powr` function, rounding the
8682 /// result to the specified precision and with the specified rounding mode. Both [`Float`]s are
8683 /// taken by value. An [`Ordering`] is also returned, indicating whether the rounded power is
8684 /// less than, equal to, or greater than the exact power. Although `NaN`s are not comparable to
8685 /// any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
8686 ///
8687 /// See [`RoundingMode`] for a description of the possible rounding modes.
8688 ///
8689 /// $$
8690 /// f(x,y) = x^y+\varepsilon.
8691 /// $$
8692 /// - If $x^y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
8693 /// - If $x^y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
8694 /// 2^{\lfloor\log_2 |x^y|\rfloor-p+1}$.
8695 /// - If $x^y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
8696 /// 2^{\lfloor\log_2 |x^y|\rfloor-p}$.
8697 ///
8698 /// If the output has a precision, it is `prec`.
8699 ///
8700 /// `powr(x, y)` is $e^{y\ln x}$; unlike [`pow`](Float::pow_prec_round), its base is restricted
8701 /// to $x\geq 0$ and it never produces a negative result.
8702 ///
8703 /// Special cases:
8704 /// - $f(x,y)=\text{NaN}$ if $x$ is `NaN`, if $x<0$, if $x$ is $\pm0$ or $\infty$ and $y=0$, or
8705 /// if $x=1$ and $y$ is infinite
8706 /// - $f(x,0)=1.0$ if $x$ is finite and positive
8707 /// - $f(1.0,y)=1.0$ if $y$ is finite
8708 /// - $f(\infty,y)=\infty$ if $y>0$, and $0.0$ if $y<0$
8709 /// - $f(\pm0.0,y)=0.0$ if $y>0$, and $\infty$ if $y<0$
8710 /// - $f(x,\infty)=\infty$ if $x>1$, and $0.0$ if $0<x<1$
8711 /// - $f(x,-\infty)=0.0$ if $x>1$, and $\infty$ if $0<x<1$
8712 ///
8713 /// Overflow and underflow:
8714 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
8715 /// returned instead.
8716 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor` or `Down`, $(1-(1/2)^p)2^{2^{30}-1}$
8717 /// is returned instead.
8718 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
8719 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
8720 /// instead.
8721 /// - If $0<f(x,y,p,m)\leq2^{-2^{30}-1}$ and $m$ is `Nearest`, $0.0$ is returned instead.
8722 /// - If $2^{-2^{30}-1}<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
8723 /// instead.
8724 ///
8725 /// # Worst-case complexity
8726 /// $T(n) = O(n^{3/2} \log n \log\log n)$
8727 ///
8728 /// $M(n) = O(n (\log n)^2)$
8729 ///
8730 /// where $T$ is time, $M$ is additional memory, and $n$ is `prec`.
8731 ///
8732 /// # Panics
8733 /// Panics if `prec` is zero, or if `rm` is `Exact` but the result cannot be represented exactly
8734 /// with the given precision.
8735 /// # Examples
8736 /// ```
8737 /// use malachite_base::rounding_modes::RoundingMode::*;
8738 /// use malachite_float::Float;
8739 /// use std::cmp::Ordering::*;
8740 ///
8741 /// let (p, o) = Float::from(3).powr_prec_round(Float::from(2.5), 20, Floor);
8742 /// assert_eq!(p.to_string(), "15.588455");
8743 /// assert_eq!(o, Less);
8744 ///
8745 /// let (p, o) = Float::from(3).powr_prec_round(Float::from(2.5), 20, Ceiling);
8746 /// assert_eq!(p.to_string(), "15.588470");
8747 /// assert_eq!(o, Greater);
8748 ///
8749 /// // A negative base gives NaN (unlike `pow`).
8750 /// let (p, o) = Float::from(-2).powr_prec_round(Float::from(3), 10, Nearest);
8751 /// assert_eq!(p.to_string(), "NaN");
8752 /// assert_eq!(o, Equal);
8753 /// ```
8754 #[allow(clippy::needless_pass_by_value)]
8755 #[inline]
8756 pub fn powr_prec_round(self, other: Self, prec: u64, rm: RoundingMode) -> (Self, Ordering) {
8757 self.powr_prec_round_ref_ref(&other, prec, rm)
8758 }
8759
8760 /// Raises a [`Float`] to a [`Float`] power using the IEEE 754 `powr` function, rounding the
8761 /// result to the specified precision and with the specified rounding mode. The first [`Float`]
8762 /// is taken by value and the second by reference. An [`Ordering`] is also returned, indicating
8763 /// whether the rounded power is less than, equal to, or greater than the exact power. Although
8764 /// `NaN`s are not comparable to any [`Float`], whenever this function returns a `NaN` it also
8765 /// returns `Equal`.
8766 ///
8767 /// See [`RoundingMode`] for a description of the possible rounding modes.
8768 ///
8769 /// See the [`Float::powr_prec_round`] documentation for information on special cases, overflow,
8770 /// and underflow.
8771 #[inline]
8772 pub fn powr_prec_round_val_ref(
8773 self,
8774 other: &Self,
8775 prec: u64,
8776 rm: RoundingMode,
8777 ) -> (Self, Ordering) {
8778 self.powr_prec_round_ref_ref(other, prec, rm)
8779 }
8780
8781 /// Raises a [`Float`] to a [`Float`] power using the IEEE 754 `powr` function, rounding the
8782 /// result to the specified precision and with the specified rounding mode. The first [`Float`]
8783 /// is taken by reference and the second by value. An [`Ordering`] is also returned, indicating
8784 /// whether the rounded power is less than, equal to, or greater than the exact power. Although
8785 /// `NaN`s are not comparable to any [`Float`], whenever this function returns a `NaN` it also
8786 /// returns `Equal`.
8787 ///
8788 /// See [`RoundingMode`] for a description of the possible rounding modes.
8789 ///
8790 /// See the [`Float::powr_prec_round`] documentation for information on special cases, overflow,
8791 /// and underflow.
8792 #[allow(clippy::needless_pass_by_value)]
8793 #[inline]
8794 pub fn powr_prec_round_ref_val(
8795 &self,
8796 other: Self,
8797 prec: u64,
8798 rm: RoundingMode,
8799 ) -> (Self, Ordering) {
8800 self.powr_prec_round_ref_ref(&other, prec, rm)
8801 }
8802
8803 /// Raises a [`Float`] to a [`Float`] power using the IEEE 754 `powr` function, rounding the
8804 /// result to the specified precision and with the specified rounding mode. Both [`Float`]s are
8805 /// taken by reference. An [`Ordering`] is also returned, indicating whether the rounded power
8806 /// is less than, equal to, or greater than the exact power. Although `NaN`s are not comparable
8807 /// to any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
8808 ///
8809 /// See [`RoundingMode`] for a description of the possible rounding modes.
8810 ///
8811 /// $$
8812 /// f(x,y) = x^y+\varepsilon.
8813 /// $$
8814 /// - If $x^y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
8815 /// - If $x^y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
8816 /// 2^{\lfloor\log_2 |x^y|\rfloor-p+1}$.
8817 /// - If $x^y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
8818 /// 2^{\lfloor\log_2 |x^y|\rfloor-p}$.
8819 ///
8820 /// If the output has a precision, it is `prec`.
8821 ///
8822 /// `powr(x, y)` is $e^{y\ln x}$; unlike [`pow`](Float::pow_prec_round), its base is restricted
8823 /// to $x\geq 0$ and it never produces a negative result.
8824 ///
8825 /// Special cases:
8826 /// - $f(x,y)=\text{NaN}$ if $x$ is `NaN`, if $x<0$, if $x$ is $\pm0$ or $\infty$ and $y=0$, or
8827 /// if $x=1$ and $y$ is infinite
8828 /// - $f(x,0)=1.0$ if $x$ is finite and positive
8829 /// - $f(1.0,y)=1.0$ if $y$ is finite
8830 /// - $f(\infty,y)=\infty$ if $y>0$, and $0.0$ if $y<0$
8831 /// - $f(\pm0.0,y)=0.0$ if $y>0$, and $\infty$ if $y<0$
8832 /// - $f(x,\infty)=\infty$ if $x>1$, and $0.0$ if $0<x<1$
8833 /// - $f(x,-\infty)=0.0$ if $x>1$, and $\infty$ if $0<x<1$
8834 ///
8835 /// Overflow and underflow:
8836 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
8837 /// returned instead.
8838 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor` or `Down`, $(1-(1/2)^p)2^{2^{30}-1}$
8839 /// is returned instead.
8840 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
8841 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
8842 /// instead.
8843 /// - If $0<f(x,y,p,m)\leq2^{-2^{30}-1}$ and $m$ is `Nearest`, $0.0$ is returned instead.
8844 /// - If $2^{-2^{30}-1}<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
8845 /// instead.
8846 ///
8847 /// # Worst-case complexity
8848 /// $T(n) = O(n^{3/2} \log n \log\log n)$
8849 ///
8850 /// $M(n) = O(n (\log n)^2)$
8851 ///
8852 /// where $T$ is time, $M$ is additional memory, and $n$ is `prec`.
8853 ///
8854 /// # Panics
8855 /// Panics if `prec` is zero, or if `rm` is `Exact` but the result cannot be represented exactly
8856 /// with the given precision.
8857 /// # Examples
8858 /// ```
8859 /// use malachite_base::rounding_modes::RoundingMode::*;
8860 /// use malachite_float::Float;
8861 /// use std::cmp::Ordering::*;
8862 ///
8863 /// let (p, o) = Float::from(3).powr_prec_round(Float::from(2.5), 20, Floor);
8864 /// assert_eq!(p.to_string(), "15.588455");
8865 /// assert_eq!(o, Less);
8866 ///
8867 /// let (p, o) = Float::from(3).powr_prec_round(Float::from(2.5), 20, Ceiling);
8868 /// assert_eq!(p.to_string(), "15.588470");
8869 /// assert_eq!(o, Greater);
8870 ///
8871 /// // A negative base gives NaN (unlike `pow`).
8872 /// let (p, o) = Float::from(-2).powr_prec_round(Float::from(3), 10, Nearest);
8873 /// assert_eq!(p.to_string(), "NaN");
8874 /// assert_eq!(o, Equal);
8875 /// ```
8876 pub fn powr_prec_round_ref_ref(
8877 &self,
8878 other: &Self,
8879 prec: u64,
8880 rm: RoundingMode,
8881 ) -> (Self, Ordering) {
8882 assert_ne!(prec, 0);
8883 let x = self;
8884 let y = other;
8885 // powr(x, y) = exp(y * ln(x)). This is `mpfr_powr` from `powr.c`, MPFR 4.3.0.
8886 match (x, y) {
8887 // A NaN or negative base (finite negative or -Inf) is NaN (pow allows a negative base
8888 // with an integer exponent); and a singular +0, -0, or +Inf base with a zero exponent
8889 // is NaN (pow gives 1).
8890 (Self(NaN | Finite { sign: false, .. } | Infinity { sign: false }), _)
8891 | (Self(Zero { .. } | Infinity { sign: true }), float_either_zero!()) => {
8892 (Self::NAN, Equal)
8893 }
8894 // powr treats -0 like +0: a finite nonzero exponent gives +0 (y > 0) or +Inf (y < 0),
8895 // always positive (pow gives a signed result for odd-integer y).
8896 (float_negative_zero!(), Self(Finite { sign, .. })) => {
8897 if *sign {
8898 (Self::ZERO, Equal)
8899 } else {
8900 (Self::INFINITY, Equal)
8901 }
8902 }
8903 // A base of exactly 1 with an infinite exponent is NaN (pow gives 1).
8904 (_, float_either_infinity!()) if *x == 1u32 => (Self::NAN, Equal),
8905 // Everything else defers to pow.
8906 _ => self.pow_prec_round_ref_ref(y, prec, rm),
8907 }
8908 }
8909
8910 /// Raises a [`Float`] to a [`Float`] power using the IEEE 754 `powr` function, rounding the
8911 /// result to the specified precision and to the nearest value. Both [`Float`]s are taken by
8912 /// value. An [`Ordering`] is also returned, indicating whether the rounded power is less than,
8913 /// equal to, or greater than the exact power. Although `NaN`s are not comparable to any
8914 /// [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
8915 ///
8916 /// If the power is equidistant from two [`Float`]s with the specified precision, the [`Float`]
8917 /// with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of
8918 /// the `Nearest` rounding mode.
8919 ///
8920 /// $$
8921 /// f(x,y) = x^y+\varepsilon.
8922 /// $$
8923 /// - If $x^y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
8924 /// - If $x^y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
8925 /// 2^{\lfloor\log_2 |x^y|\rfloor-p+1}$.
8926 /// - If $x^y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
8927 /// 2^{\lfloor\log_2 |x^y|\rfloor-p}$.
8928 ///
8929 /// If the output has a precision, it is `prec`.
8930 ///
8931 /// `powr(x, y)` is $e^{y\ln x}$; unlike [`pow`](Float::pow_prec_round), its base is restricted
8932 /// to $x\geq 0$ and it never produces a negative result.
8933 ///
8934 /// Special cases:
8935 /// - $f(x,y)=\text{NaN}$ if $x$ is `NaN`, if $x<0$, if $x$ is $\pm0$ or $\infty$ and $y=0$, or
8936 /// if $x=1$ and $y$ is infinite
8937 /// - $f(x,0)=1.0$ if $x$ is finite and positive
8938 /// - $f(1.0,y)=1.0$ if $y$ is finite
8939 /// - $f(\infty,y)=\infty$ if $y>0$, and $0.0$ if $y<0$
8940 /// - $f(\pm0.0,y)=0.0$ if $y>0$, and $\infty$ if $y<0$
8941 /// - $f(x,\infty)=\infty$ if $x>1$, and $0.0$ if $0<x<1$
8942 /// - $f(x,-\infty)=0.0$ if $x>1$, and $\infty$ if $0<x<1$
8943 ///
8944 /// Overflow and underflow:
8945 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
8946 /// returned instead.
8947 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor` or `Down`, $(1-(1/2)^p)2^{2^{30}-1}$
8948 /// is returned instead.
8949 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
8950 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
8951 /// instead.
8952 /// - If $0<f(x,y,p,m)\leq2^{-2^{30}-1}$ and $m$ is `Nearest`, $0.0$ is returned instead.
8953 /// - If $2^{-2^{30}-1}<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
8954 /// instead.
8955 ///
8956 /// # Worst-case complexity
8957 /// $T(n) = O(n^{3/2} \log n \log\log n)$
8958 ///
8959 /// $M(n) = O(n (\log n)^2)$
8960 ///
8961 /// where $T$ is time, $M$ is additional memory, and $n$ is `prec`.
8962 ///
8963 /// # Panics
8964 /// Panics if `prec` is zero.
8965 /// # Examples
8966 /// ```
8967 /// use malachite_float::Float;
8968 /// use std::cmp::Ordering::*;
8969 ///
8970 /// let (p, o) = Float::from(9).powr_prec(Float::from(0.5), 10);
8971 /// assert_eq!(p.to_string(), "3.0000");
8972 /// assert_eq!(o, Equal);
8973 /// ```
8974 #[allow(clippy::needless_pass_by_value)]
8975 #[inline]
8976 pub fn powr_prec(self, other: Self, prec: u64) -> (Self, Ordering) {
8977 self.powr_prec_round_ref_ref(&other, prec, Nearest)
8978 }
8979
8980 /// Raises a [`Float`] to a [`Float`] power using the IEEE 754 `powr` function, rounding the
8981 /// result to the specified precision and to the nearest value. The first [`Float`] is taken by
8982 /// value and the second by reference. An [`Ordering`] is also returned, indicating whether the
8983 /// rounded power is less than, equal to, or greater than the exact power. Although `NaN`s are
8984 /// not comparable to any [`Float`], whenever this function returns a `NaN` it also returns
8985 /// `Equal`.
8986 ///
8987 /// If the power is equidistant from two [`Float`]s with the specified precision, the [`Float`]
8988 /// with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of
8989 /// the `Nearest` rounding mode.
8990 ///
8991 /// See the [`Float::powr_prec_round`] documentation for information on special cases, overflow,
8992 /// and underflow.
8993 #[inline]
8994 pub fn powr_prec_val_ref(self, other: &Self, prec: u64) -> (Self, Ordering) {
8995 self.powr_prec_round_ref_ref(other, prec, Nearest)
8996 }
8997
8998 /// Raises a [`Float`] to a [`Float`] power using the IEEE 754 `powr` function, rounding the
8999 /// result to the specified precision and to the nearest value. The first [`Float`] is taken by
9000 /// reference and the second by value. An [`Ordering`] is also returned, indicating whether the
9001 /// rounded power is less than, equal to, or greater than the exact power. Although `NaN`s are
9002 /// not comparable to any [`Float`], whenever this function returns a `NaN` it also returns
9003 /// `Equal`.
9004 ///
9005 /// If the power is equidistant from two [`Float`]s with the specified precision, the [`Float`]
9006 /// with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of
9007 /// the `Nearest` rounding mode.
9008 ///
9009 /// See the [`Float::powr_prec_round`] documentation for information on special cases, overflow,
9010 /// and underflow.
9011 #[allow(clippy::needless_pass_by_value)]
9012 #[inline]
9013 pub fn powr_prec_ref_val(&self, other: Self, prec: u64) -> (Self, Ordering) {
9014 self.powr_prec_round_ref_ref(&other, prec, Nearest)
9015 }
9016
9017 /// Raises a [`Float`] to a [`Float`] power using the IEEE 754 `powr` function, rounding the
9018 /// result to the specified precision and to the nearest value. Both [`Float`]s are taken by
9019 /// reference. An [`Ordering`] is also returned, indicating whether the rounded power is less
9020 /// than, equal to, or greater than the exact power. Although `NaN`s are not comparable to any
9021 /// [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
9022 ///
9023 /// If the power is equidistant from two [`Float`]s with the specified precision, the [`Float`]
9024 /// with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of
9025 /// the `Nearest` rounding mode.
9026 ///
9027 /// See the [`Float::powr_prec_round`] documentation for information on special cases, overflow,
9028 /// and underflow.
9029 #[inline]
9030 pub fn powr_prec_ref_ref(&self, other: &Self, prec: u64) -> (Self, Ordering) {
9031 self.powr_prec_round_ref_ref(other, prec, Nearest)
9032 }
9033
9034 /// Raises a [`Float`] to a [`Float`] power using the IEEE 754 `powr` function, rounding the
9035 /// result to the maximum of the precisions of the inputs and with the specified rounding mode.
9036 /// Both [`Float`]s are taken by value. An [`Ordering`] is also returned, indicating whether the
9037 /// rounded power is less than, equal to, or greater than the exact power. Although `NaN`s are
9038 /// not comparable to any [`Float`], whenever this function returns a `NaN` it also returns
9039 /// `Equal`.
9040 ///
9041 /// See [`RoundingMode`] for a description of the possible rounding modes.
9042 ///
9043 /// $$
9044 /// f(x,y) = x^y+\varepsilon.
9045 /// $$
9046 /// - If $x^y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
9047 /// - If $x^y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
9048 /// 2^{\lfloor\log_2 |x^y|\rfloor-p+1}$.
9049 /// - If $x^y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
9050 /// 2^{\lfloor\log_2 |x^y|\rfloor-p}$.
9051 ///
9052 /// If the output has a precision, it is `prec`.
9053 ///
9054 /// `powr(x, y)` is $e^{y\ln x}$; unlike [`pow`](Float::pow_prec_round), its base is restricted
9055 /// to $x\geq 0$ and it never produces a negative result.
9056 ///
9057 /// Special cases:
9058 /// - $f(x,y)=\text{NaN}$ if $x$ is `NaN`, if $x<0$, if $x$ is $\pm0$ or $\infty$ and $y=0$, or
9059 /// if $x=1$ and $y$ is infinite
9060 /// - $f(x,0)=1.0$ if $x$ is finite and positive
9061 /// - $f(1.0,y)=1.0$ if $y$ is finite
9062 /// - $f(\infty,y)=\infty$ if $y>0$, and $0.0$ if $y<0$
9063 /// - $f(\pm0.0,y)=0.0$ if $y>0$, and $\infty$ if $y<0$
9064 /// - $f(x,\infty)=\infty$ if $x>1$, and $0.0$ if $0<x<1$
9065 /// - $f(x,-\infty)=0.0$ if $x>1$, and $\infty$ if $0<x<1$
9066 ///
9067 /// Overflow and underflow:
9068 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
9069 /// returned instead.
9070 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Floor` or `Down`, $(1-(1/2)^p)2^{2^{30}-1}$
9071 /// is returned instead.
9072 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
9073 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
9074 /// instead.
9075 /// - If $0<f(x,y,p,m)\leq2^{-2^{30}-1}$ and $m$ is `Nearest`, $0.0$ is returned instead.
9076 /// - If $2^{-2^{30}-1}<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
9077 /// instead.
9078 ///
9079 /// # Worst-case complexity
9080 /// $T(n) = O(n^{3/2} \log n \log\log n)$
9081 ///
9082 /// $M(n) = O(n (\log n)^2)$
9083 ///
9084 /// where $T$ is time, $M$ is additional memory, and $n$ is `prec`.
9085 ///
9086 /// # Panics
9087 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the output
9088 /// precision.
9089 /// # Examples
9090 /// ```
9091 /// use malachite_base::rounding_modes::RoundingMode::*;
9092 /// use malachite_float::Float;
9093 /// use std::cmp::Ordering::*;
9094 ///
9095 /// let (p, o) = Float::from(3).powr_round(Float::from(2.5), Floor);
9096 /// assert_eq!(p.to_string(), "14.0");
9097 /// assert_eq!(o, Less);
9098 /// ```
9099 #[allow(clippy::needless_pass_by_value)]
9100 pub fn powr_round(self, other: Self, rm: RoundingMode) -> (Self, Ordering) {
9101 let prec = self.significant_bits().max(other.significant_bits());
9102 self.powr_prec_round_ref_ref(&other, prec, rm)
9103 }
9104
9105 /// Raises a [`Float`] to a [`Float`] power using the IEEE 754 `powr` function, rounding the
9106 /// result to the maximum of the precisions of the inputs and with the specified rounding mode.
9107 /// The first [`Float`] is taken by value and the second by reference. An [`Ordering`] is also
9108 /// returned, indicating whether the rounded power is less than, equal to, or greater than the
9109 /// exact power. Although `NaN`s are not comparable to any [`Float`], whenever this function
9110 /// returns a `NaN` it also returns `Equal`.
9111 ///
9112 /// See [`RoundingMode`] for a description of the possible rounding modes.
9113 ///
9114 /// See the [`Float::powr_prec_round`] documentation for information on special cases, overflow,
9115 /// and underflow.
9116 pub fn powr_round_val_ref(self, other: &Self, rm: RoundingMode) -> (Self, Ordering) {
9117 let prec = self.significant_bits().max(other.significant_bits());
9118 self.powr_prec_round_ref_ref(other, prec, rm)
9119 }
9120
9121 /// Raises a [`Float`] to a [`Float`] power using the IEEE 754 `powr` function, rounding the
9122 /// result to the maximum of the precisions of the inputs and with the specified rounding mode.
9123 /// The first [`Float`] is taken by reference and the second by value. An [`Ordering`] is also
9124 /// returned, indicating whether the rounded power is less than, equal to, or greater than the
9125 /// exact power. Although `NaN`s are not comparable to any [`Float`], whenever this function
9126 /// returns a `NaN` it also returns `Equal`.
9127 ///
9128 /// See [`RoundingMode`] for a description of the possible rounding modes.
9129 ///
9130 /// See the [`Float::powr_prec_round`] documentation for information on special cases, overflow,
9131 /// and underflow.
9132 #[allow(clippy::needless_pass_by_value)]
9133 pub fn powr_round_ref_val(&self, other: Self, rm: RoundingMode) -> (Self, Ordering) {
9134 let prec = self.significant_bits().max(other.significant_bits());
9135 self.powr_prec_round_ref_ref(&other, prec, rm)
9136 }
9137
9138 /// Raises a [`Float`] to a [`Float`] power using the IEEE 754 `powr` function, rounding the
9139 /// result to the maximum of the precisions of the inputs and with the specified rounding mode.
9140 /// Both [`Float`]s are taken by reference. An [`Ordering`] is also returned, indicating whether
9141 /// the rounded power is less than, equal to, or greater than the exact power. Although `NaN`s
9142 /// are not comparable to any [`Float`], whenever this function returns a `NaN` it also returns
9143 /// `Equal`.
9144 ///
9145 /// See [`RoundingMode`] for a description of the possible rounding modes.
9146 ///
9147 /// See the [`Float::powr_prec_round`] documentation for information on special cases, overflow,
9148 /// and underflow.
9149 pub fn powr_round_ref_ref(&self, other: &Self, rm: RoundingMode) -> (Self, Ordering) {
9150 let prec = self.significant_bits().max(other.significant_bits());
9151 self.powr_prec_round_ref_ref(other, prec, rm)
9152 }
9153
9154 /// Raises a [`Float`] to a [`Float`] power in place using the IEEE 754 `powr` function, taking
9155 /// the exponent by value.
9156 ///
9157 /// See the [`Float::powr_prec_round`] documentation for information on special cases, overflow,
9158 /// and underflow.
9159 ///
9160 /// # Worst-case complexity
9161 /// $T(n) = O(n^{3/2} \log n \log\log n)$
9162 ///
9163 /// $M(n) = O(n (\log n)^2)$
9164 ///
9165 /// where $T$ is time, $M$ is additional memory, and $n$ is `prec`.
9166 ///
9167 /// # Panics
9168 /// Panics if `prec` is zero, or if `rm` is `Exact` but the result cannot be represented exactly
9169 /// with the given precision.
9170 #[allow(clippy::needless_pass_by_value)]
9171 pub fn powr_prec_round_assign(&mut self, other: Self, prec: u64, rm: RoundingMode) -> Ordering {
9172 let (result, o) = self.powr_prec_round_ref_ref(&other, prec, rm);
9173 *self = result;
9174 o
9175 }
9176
9177 /// Raises a [`Float`] to a [`Float`] power in place using the IEEE 754 `powr` function, taking
9178 /// the exponent by reference.
9179 ///
9180 /// See the [`Float::powr_prec_round`] documentation for information on special cases, overflow,
9181 /// and underflow.
9182 ///
9183 /// # Worst-case complexity
9184 /// $T(n) = O(n^{3/2} \log n \log\log n)$
9185 ///
9186 /// $M(n) = O(n (\log n)^2)$
9187 ///
9188 /// where $T$ is time, $M$ is additional memory, and $n$ is `prec`.
9189 ///
9190 /// # Panics
9191 /// Panics if `prec` is zero, or if `rm` is `Exact` but the result cannot be represented exactly
9192 /// with the given precision.
9193 pub fn powr_prec_round_assign_ref(
9194 &mut self,
9195 other: &Self,
9196 prec: u64,
9197 rm: RoundingMode,
9198 ) -> Ordering {
9199 let (result, o) = self.powr_prec_round_ref_ref(other, prec, rm);
9200 *self = result;
9201 o
9202 }
9203
9204 /// Raises a [`Float`] to a [`Float`] power in place using the IEEE 754 `powr` function, taking
9205 /// the exponent by value.
9206 ///
9207 /// See the [`Float::powr_prec_round`] documentation for information on special cases, overflow,
9208 /// and underflow.
9209 ///
9210 /// # Worst-case complexity
9211 /// $T(n) = O(n^{3/2} \log n \log\log n)$
9212 ///
9213 /// $M(n) = O(n (\log n)^2)$
9214 ///
9215 /// where $T$ is time, $M$ is additional memory, and $n$ is `prec`.
9216 ///
9217 /// # Panics
9218 /// Panics if `prec` is zero.
9219 #[allow(clippy::needless_pass_by_value)]
9220 #[inline]
9221 pub fn powr_prec_assign(&mut self, other: Self, prec: u64) -> Ordering {
9222 self.powr_prec_round_assign(other, prec, Nearest)
9223 }
9224
9225 /// Raises a [`Float`] to a [`Float`] power in place using the IEEE 754 `powr` function, taking
9226 /// the exponent by reference.
9227 ///
9228 /// See the [`Float::powr_prec_round`] documentation for information on special cases, overflow,
9229 /// and underflow.
9230 ///
9231 /// # Worst-case complexity
9232 /// $T(n) = O(n^{3/2} \log n \log\log n)$
9233 ///
9234 /// $M(n) = O(n (\log n)^2)$
9235 ///
9236 /// where $T$ is time, $M$ is additional memory, and $n$ is `prec`.
9237 ///
9238 /// # Panics
9239 /// Panics if `prec` is zero.
9240 #[inline]
9241 pub fn powr_prec_assign_ref(&mut self, other: &Self, prec: u64) -> Ordering {
9242 self.powr_prec_round_assign_ref(other, prec, Nearest)
9243 }
9244
9245 /// Raises a [`Float`] to a [`Float`] power in place using the IEEE 754 `powr` function, taking
9246 /// the exponent by value.
9247 ///
9248 /// See the [`Float::powr_prec_round`] documentation for information on special cases, overflow,
9249 /// and underflow.
9250 ///
9251 /// # Worst-case complexity
9252 /// $T(n) = O(n^{3/2} \log n \log\log n)$
9253 ///
9254 /// $M(n) = O(n (\log n)^2)$
9255 ///
9256 /// where $T$ is time, $M$ is additional memory, and $n$ is `prec`.
9257 ///
9258 /// # Panics
9259 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the output
9260 /// precision.
9261 #[allow(clippy::needless_pass_by_value)]
9262 pub fn powr_round_assign(&mut self, other: Self, rm: RoundingMode) -> Ordering {
9263 let prec = self.significant_bits().max(other.significant_bits());
9264 self.powr_prec_round_assign(other, prec, rm)
9265 }
9266
9267 /// Raises a [`Float`] to a [`Float`] power in place using the IEEE 754 `powr` function, taking
9268 /// the exponent by reference.
9269 ///
9270 /// See the [`Float::powr_prec_round`] documentation for information on special cases, overflow,
9271 /// and underflow.
9272 ///
9273 /// # Worst-case complexity
9274 /// $T(n) = O(n^{3/2} \log n \log\log n)$
9275 ///
9276 /// $M(n) = O(n (\log n)^2)$
9277 ///
9278 /// where $T$ is time, $M$ is additional memory, and $n$ is `prec`.
9279 ///
9280 /// # Panics
9281 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the output
9282 /// precision.
9283 pub fn powr_round_assign_ref(&mut self, other: &Self, rm: RoundingMode) -> Ordering {
9284 let prec = self.significant_bits().max(other.significant_bits());
9285 self.powr_prec_round_assign_ref(other, prec, rm)
9286 }
9287}