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::float::arithmetic::exp::{
19 exp_overflow, exp_rational_near_one, exp_underflow, one_neighbor,
20};
21use crate::float::arithmetic::ln::ln_1_plus_rational_brackets;
22use crate::float::arithmetic::log_base_2::log_2_rational_brackets;
23use crate::float::arithmetic::round_near_x::float_round_near_x;
24use crate::{
25 Float, TWICE_WIDTH, emulate_float_float_to_float_fn, emulate_float_to_float_fn,
26 float_either_infinity, float_either_zero, float_nan, float_negative_zero, floor_and_ceiling,
27};
28use core::cmp::Ordering::{self, *};
29use core::cmp::max;
30use core::mem::swap;
31use malachite_base::fail_on_untested_path;
32use malachite_base::num::arithmetic::traits::{
33 Abs, AddMul, CeilingLogBase2, CheckedLogBase2, CheckedRoot, CheckedSqrt, DivisibleBy,
34 IsPowerOf2, NegAssign, Parity, Pow, PowAssign, Square, UnsignedAbs,
35};
36use malachite_base::num::basic::floats::PrimitiveFloat;
37use malachite_base::num::basic::integers::PrimitiveInt;
38use malachite_base::num::basic::traits::{
39 Infinity as InfinityTrait, NaN as NaNTrait, NegativeInfinity, NegativeZero, One,
40 Zero as ZeroTrait,
41};
42use malachite_base::num::comparison::traits::{OrdAbs, PartialOrdAbs};
43use malachite_base::num::conversion::traits::{ExactFrom, IsInteger, RoundingFrom, SaturatingFrom};
44use malachite_base::num::logic::traits::{BitAccess, BitIterable, SignificantBits};
45use malachite_base::rounding_modes::RoundingMode::{self, *};
46use malachite_nz::integer::Integer;
47use malachite_nz::natural::Natural;
48use malachite_nz::natural::arithmetic::float::round::float_can_round;
49use malachite_nz::platform::{Limb, SignedLimb};
50use malachite_q::Rational;
51
52// This is MPFR_POW_EXP_THRESHOLD from `pow.c`, MPFR 4.3.0.
53const POW_EXP_THRESHOLD: i64 = 256;
54
55// Whether y is an odd integer. This is equivalent to `mpfr_odd_p` from `mpfr-impl.h`, MPFR 4.3.0,
56// for finite nonzero y.
57fn float_odd_integer(y: &Float) -> bool {
58 if !y.is_finite() || y.is_zero() || !y.is_integer() {
59 return false;
60 }
61 // y = m * 2^(e - b) with m the b-bit significand: y is odd iff its unit bit is set, i.e. the
62 // significand's trailing zero count is exactly b - e. (For e > b, y is an even integer.) This
63 // avoids materializing the integer, whose bit length is the exponent and can be huge.
64 let e = i64::from(y.get_exponent().unwrap());
65 let m = y.significand_ref().unwrap();
66 let b = i64::exact_from(m.significant_bits());
67 e <= b && i64::exact_from(m.trailing_zeros().unwrap()) == b - e
68}
69
70// MPFR's `mpfr_underflow` as used by `mpfr_pow`: the callers pre-map Nearest per MPFR's convention.
71// A negative result mirrors the positive case with the rounding mode negated.
72fn pow_underflow(prec: u64, rm: RoundingMode, negative: bool) -> (Float, Ordering) {
73 if negative {
74 let (f, o) = exp_underflow(prec, -rm);
75 (-f, o.reverse())
76 } else {
77 exp_underflow(prec, rm)
78 }
79}
80
81// MPFR's `mpfr_overflow` as used by `mpfr_pow`.
82fn pow_overflow(prec: u64, rm: RoundingMode, negative: bool) -> (Float, Ordering) {
83 if negative {
84 let (f, o) = exp_overflow(prec, -rm);
85 (-f, o.reverse())
86 } else {
87 exp_overflow(prec, rm)
88 }
89}
90
91// Whether the significand of a finite nonzero Float is a power of 2 (sign-agnostic). This is
92// equivalent to `mpfr_powerof2_raw` from `mpfr-impl.h`, MPFR 4.3.0.
93fn raw_power_of_2(x: &Float) -> bool {
94 x.significand_ref().unwrap().is_power_of_2()
95}
96
97// The tiny-argument result 1 +/- ulp(1), following the tiny-x fast path of `mpfr_exp` and
98// MPFR_SMALL_INPUT_AFTER_SAVE_EXPO: the exact result is 1 + eps with sign(eps) given by `above`.
99fn float_one_plus_tiny(prec: u64, rm: RoundingMode, above: bool) -> (Float, Ordering) {
100 match (rm, above) {
101 (Up | Ceiling, true) => (one_neighbor(prec, true), Greater),
102 (Down | Floor, false) => (one_neighbor(prec, false), Less),
103 (_, true) => (Float::one_prec(prec), Less),
104 (_, false) => (Float::one_prec(prec), Greater),
105 }
106}
107
108// The outcome of `pow_near_one_fast_path`.
109enum NearOne {
110 // The result was rounded directly from 1 (or -1).
111 Rounded(Float, Ordering),
112 // The result is close to 1, but its interesting bits land within the output's window: the Ziv
113 // loop must run, but should start with this many extra bits of working precision, since the
114 // result's significand begins with about this many 0s or 1s after the leading bit. Without the
115 // jump start the loop would balloon, recomputing the power ~log(extra) times at growing
116 // precisions until the working precision covers the run.
117 JumpStart(u64),
118 // The fast path does not apply.
119 No,
120}
121
122// Fast path for x^z when x is so close to +/-1 that the result is very close to +/-1. Writing |x| =
123// 1 + d with d nonzero and fld = EXP(d), and sb_z = the bit length of |z| (z != 0, with its sign
124// given by `z_negative`), the path engages when fld + sb_z <= -3. Then |d| < 2^fld <= 2^-4 and
125// |z||d| < 2^(fld + sb_z) <= 2^-3, and with t = z ln(1 + d):
126// - |ln(1 + d)| <= |d|/(1 - |d|) <= (4/3)|d|, so |t| <= (4/3)|z||d| <= 1/6;
127// - |e^t - 1| <= |t| + t^2 <= (3/2)|t| for |t| <= 1/2;
128// so ||x|^z - 1| = |e^t - 1| <= 2|z||d| < 2^(fld + sb_z + 1), strictly (both |z| < 2^sb_z and |d| <
129// 2^fld are strict). This is exactly the error contract of `float_round_near_x` with v = 1 and err
130// = -(fld + sb_z).
131//
132// `float_round_near_x` also requires the exact result not to be representable, which holds whenever
133// it succeeds (it requires err > prec + 1): for positive z, the exact (1 + d)^z is a dyadic
134// rational whose bits span from its leading 1 down to exactly z*j, where 2^j is the lowest set bit
135// of d; since j <= fld and -fld >= err - sb_z > prec + 1 - sb_z, the span exceeds prec + 1 bits, so
136// the value is neither representable at prec nor a `Nearest` midpoint. For negative z the exact
137// value is not even dyadic (1/(1 + d)^|z| is dyadic only if (1 + d)^|z| is a power of 2, impossible
138// for 0 < |d| <= 2^-4).
139//
140// `negate` is true when the result is negative (x negative and z odd); the rounding is then
141// performed on the magnitude with the inverted rounding mode, and the ternary value is reversed.
142fn pow_near_one_fast_path(
143 x: &Float,
144 sb_z: u64,
145 z_negative: bool,
146 negate: bool,
147 prec: u64,
148 rm: RoundingMode,
149) -> NearOne {
150 // `Exact` is left entirely to the callers, so that this path never has to decide exactness.
151 if rm == Exact {
152 return NearOne::No;
153 }
154 let ex = i64::from(x.get_exponent().unwrap());
155 // |x| must be in [1/2, 2) for x to be near +/-1.
156 if ex != 0 && ex != 1 {
157 return NearOne::No;
158 }
159 // d = |x| - 1, exactly (the difference of two dyadic values whose bits span at most
160 // significant_bits(x) + 2 positions here).
161 let d = x
162 .abs()
163 .sub_prec_round(Float::ONE, x.significant_bits() + 2, Exact)
164 .0;
165 if d == 0u32 {
166 // |x| = 1 exactly; the callers' loops handle this case exactly and quickly.
167 return NearOne::No;
168 }
169 let fld = i64::from(d.get_exponent().unwrap());
170 let Some(shift) = fld.checked_add(i64::exact_from(sb_z)) else {
171 return NearOne::No;
172 };
173 if shift > -3 {
174 return NearOne::No;
175 }
176 let err = u64::exact_from(-shift);
177 // |x|^z > 1 iff |x| > 1 and z > 0, or |x| < 1 and z < 0.
178 let above = (d > 0u32) != z_negative;
179 let rm_abs = if negate { -rm } else { rm };
180 if let Some((v, o)) = float_round_near_x(&Float::ONE, err, above, prec, rm_abs) {
181 return if negate {
182 NearOne::Rounded(-v, o.reverse())
183 } else {
184 NearOne::Rounded(v, o)
185 };
186 }
187 NearOne::JumpStart(err)
188}
189
190// This is `mpfr_pow_pos_z` from `pow_z.c`, MPFR 4.3.0, with z positive. If `cr` is true the result
191// is correctly rounded; otherwise `prec` is used as the working precision. Returns the result and
192// its ordering; the result may be infinite or zero on intermediate overflow or underflow (the
193// callers handle those cases).
194fn pow_pos_natural(
195 x: &Float,
196 z: &Natural,
197 prec: u64,
198 rm: RoundingMode,
199 cr: bool,
200 extra_prec: u64,
201) -> (Float, Ordering) {
202 assert_ne!(*z, 0u32);
203 if *z == 1u32 {
204 return Float::from_float_prec_round_ref(x, prec, rm);
205 }
206 let size_z = z.significant_bits();
207 // Rounding directions chosen so that all intermediate roundings go the same way, making an
208 // intermediate overflow or underflow a true exception rather than rounding noise.
209 let x_exp_ge_1 = x.get_exponent().unwrap() >= 1;
210 let rnd1 = if x_exp_ge_1 {
211 Down
212 } else if x.is_sign_positive() {
213 Up
214 } else {
215 Floor
216 };
217 let rnd2 = if x_exp_ge_1 { Floor } else { Up };
218 // `extra_prec` is the near-1 jump start computed by the caller; see `pow_near_one_fast_path`.
219 let mut wprec = if cr {
220 prec + 3 + size_z + prec.ceiling_log_base_2() + extra_prec
221 } else {
222 prec
223 };
224 loop {
225 let mut inexmul;
226 let err = wprec - 1 - size_z;
227 let mut i = size_z;
228 let (mut res, o) = x.square_prec_round_ref(wprec, rnd2);
229 inexmul = o != Equal;
230 assert!(i >= 2);
231 if z.get_bit(i - 2) {
232 let o = res.mul_prec_round_assign_ref(x, wprec, rnd1);
233 inexmul |= o != Equal;
234 }
235 if i > 2 {
236 i -= 3;
237 while res.is_finite() && !res.is_zero() {
238 let o = res.square_prec_round_assign(wprec, rnd2);
239 inexmul |= o != Equal;
240 if z.get_bit(i) {
241 let o = res.mul_prec_round_assign_ref(x, wprec, rnd1);
242 inexmul |= o != Equal;
243 }
244 if i == 0 {
245 break;
246 }
247 i -= 1;
248 }
249 }
250 // In the shrinking regime (x's exponent < 1), rnd1/rnd2 are Up-directed, so `res` is an
251 // upper bound and can never round to zero. An inexact upper bound equal to the minimum
252 // positive Float proves the true value lies below it: a true underflow, reported as zero so
253 // the caller applies its underflow handling. (Values elsewhere in the bottom binade are
254 // representable and pass through normally; in the growing regime magnitudes only increase,
255 // so this cannot trigger.)
256 if !x_exp_ge_1
257 && inexmul
258 && res.is_finite()
259 && !res.is_zero()
260 && i64::from(res.get_exponent().unwrap()) == Float::MIN_EXPONENT_I64
261 && raw_power_of_2(&res)
262 {
263 res = if res.is_sign_negative() {
264 Float::NEGATIVE_ZERO
265 } else {
266 Float::ZERO
267 };
268 }
269 let is_zero = res.is_zero();
270 let exceptional = res.is_infinite() || is_zero;
271 if !inexmul
272 || !cr
273 || exceptional
274 || float_can_round(res.significand_ref().unwrap(), err, prec, rm)
275 {
276 if exceptional {
277 // overflow or underflow: the sign and the exceptional value are already correct
278 if !is_zero {
279 // The growing regime rounds toward zero (lower bounds), and the callers decide
280 // the overflow boundary exactly before descending here.
281 fail_on_untested_path("pow_pos_natural, overflow");
282 }
283 // A zero lies toward zero from the true value and an infinity away from it, so the
284 // ternary depends on the sign: +0 and -inf are less than the true value, -0 and
285 // +inf greater.
286 let o = if is_zero == res.is_sign_positive() {
287 Less
288 } else {
289 Greater
290 };
291 return (res, o);
292 }
293 return Float::from_float_prec_round(res, prec, rm);
294 }
295 wprec += wprec >> 1;
296 }
297}
298
299// The round-to-nearest underflow fallback of `mpfr_pow_pos_z` from `pow_z.c`, MPFR 4.3.0:
300// nearest-mode underflow must choose between 0 and 2^(emin - 1) according to which side of 2^(emin
301// - 2) the true value lies, which the multiplication-based path cannot know. Rerun via pow_general
302// at 2 bits of precision: its 2^k scaling keeps the computation in range, and the final
303// shl_prec_round applies the correct nearest-mode underflow rounding.
304fn pow_integer_underflow_nearest(x: &Float, z: &Integer, prec: u64) -> (Float, Ordering) {
305 let z_bits = z.significant_bits();
306 let zz = Float::from_integer_prec_round_ref(z, z_bits, Exact).0;
307 let (y2, o) = pow_general(x, &zz, 2, Nearest, true);
308 (Float::from_float_prec_round(y2, prec, Exact).0, o)
309}
310
311// This is `mpfr_pow_z` from `pow_z.c`, MPFR 4.3.0.
312fn pow_integer(x: &Float, z: &Integer, prec: u64, rm: RoundingMode) -> (Float, Ordering) {
313 if *z == 0u32 {
314 // The public entry handles y = 0 before calling pow_integer.
315 fail_on_untested_path("pow_integer, z == 0");
316 return (Float::one_prec(prec), Equal);
317 }
318 if x.is_nan() {
319 // The public entry filters singular x before calling pow_integer.
320 fail_on_untested_path("pow_integer, NaN x");
321 return (Float::NAN, Equal);
322 }
323 let z_pos = *z > 0u32;
324 let z_odd = z.odd();
325 if x.is_infinite() {
326 // The public entry filters singular x before calling pow_integer.
327 fail_on_untested_path("pow_integer, infinite x");
328 let negative = x.is_sign_negative() && z_odd;
329 return (
330 match (z_pos, negative) {
331 (true, false) => Float::INFINITY,
332 (true, true) => Float::NEGATIVE_INFINITY,
333 (false, false) => Float::ZERO,
334 (false, true) => Float::NEGATIVE_ZERO,
335 },
336 Equal,
337 );
338 }
339 if x.is_zero() {
340 // The public entry filters singular x before calling pow_integer.
341 fail_on_untested_path("pow_integer, zero x");
342 let negative = x.is_sign_negative() && z_odd;
343 return (
344 match (z_pos, negative) {
345 (true, false) => Float::ZERO,
346 (true, true) => Float::NEGATIVE_ZERO,
347 (false, false) => Float::INFINITY,
348 (false, true) => Float::NEGATIVE_INFINITY,
349 },
350 Equal,
351 );
352 }
353 // x = +/-2^b: x^z = (+/-1)^z * 2^(z*(b-1)+1-1)... handled exactly via the exponent.
354 if raw_power_of_2(x) {
355 let ex = i64::from(x.get_exponent().unwrap());
356 let sign_negative = x.is_sign_negative() && z_odd;
357 // new exponent = z * (ex - 1) + 1
358 let new_exp = Integer::ONE.add_mul(z, Integer::from(ex - 1));
359 let base = if sign_negative {
360 -Float::one_prec(prec)
361 } else {
362 Float::one_prec(prec)
363 };
364 return if new_exp < Float::MIN_EXPONENT {
365 pow_underflow(prec, if rm == Nearest { Down } else { rm }, sign_negative)
366 } else if new_exp > Float::MAX_EXPONENT {
367 // z(ex - 1) + 1 > MAX_EXPONENT implies z * log2|x| >= MAX_EXPONENT (the product is an
368 // exact integer at 64 bits here): a definite overflow. When called from `Float::pow`
369 // the entry's early overflow check already caught this; when called from the
370 // integer-exponent path of `Float::pow_rational` (which has no such pre-check), this is
371 // the first detection.
372 pow_overflow(prec, rm, sign_negative)
373 } else {
374 let sh = i64::exact_from(&(new_exp - Integer::ONE));
375 base.shl_prec_round(sh, prec, rm)
376 };
377 }
378 let negative = x.is_sign_negative() && z_odd;
379 // Near-1 fast path, checked before the exponent pre-bounds below: for x very close to +/-1,
380 // computing the 64-bit log2 estimate is itself expensive (the tiny logarithm must be resolved,
381 // which costs as much as the power itself), and in this regime |x^z| lies in (5/6, 6/5), so no
382 // overflow or underflow is possible and the pre-bounds are unnecessary.
383 let mut jump_extra = 0;
384 match pow_near_one_fast_path(
385 x,
386 z.unsigned_abs_ref().significant_bits(),
387 !z_pos,
388 negative,
389 prec,
390 rm,
391 ) {
392 NearOne::Rounded(v, o) => return (v, o),
393 NearOne::JumpStart(extra) => jump_extra = extra,
394 NearOne::No => {}
395 }
396 if jump_extra == 0 {
397 // Pre-bound the result exponent: result_exp ~ z * log2|x|. When it is far outside the
398 // exponent range (with a wide margin for the estimate's error), report the exception
399 // directly instead of letting the exponentiation saturate; this mirrors the role of MPFR's
400 // underflow/overflow flags, which malachite does not have, and keeps the Ziv loop from
401 // ballooning on saturated values.
402 let est = f64::rounding_from(x.abs().log_base_2_prec(64).0, Nearest).0
403 * f64::rounding_from(z, Nearest).0;
404 if est > const { Float::MAX_EXPONENT as f64 + 64.0 } {
405 // est > MAX_EXPONENT + 64: a definite overflow. When called from `Float::pow`, the
406 // entry's early overflow check already caught this; when called from the exact-power
407 // path of `Float::pow_rational` (which has no such pre-check), this is the first
408 // detection.
409 return pow_overflow(prec, rm, negative);
410 }
411 if est < const { Float::MIN_EXPONENT as f64 - 64.0 } {
412 return pow_underflow(prec, if rm == Nearest { Down } else { rm }, negative);
413 }
414 // Within the estimate's error margin of MAX_EXPONENT the overflow question is still open,
415 // and it must be decided here: every rounding used by `pow_pos_natural`'s growing regime
416 // and by the reciprocal path below decreases the magnitude, so an overflow would saturate
417 // at the largest finite value instead of reaching infinity, and the saturated all-ones
418 // significand is one that `float_can_round` never certifies -- the Ziv loop would grow
419 // forever. (Underflow needs no such decision: magnitude-decreasing rounding turns a true
420 // underflow into an exact zero, which the loops detect directly.) The check mirrors the
421 // role of MPFR's overflow flag.
422 if est >= const { Float::MAX_EXPONENT as f64 - 66.0 }
423 && pow_exponent_at_least(x, z, Float::MAX_EXPONENT_I64)
424 {
425 return pow_overflow(prec, rm, negative);
426 }
427 }
428 if z_pos {
429 let (result, o) = pow_pos_natural(x, z.unsigned_abs_ref(), prec, rm, true, jump_extra);
430 if result.is_zero() {
431 // pow_pos_natural only returns zero when the result underflowed.
432 return if rm == Nearest {
433 pow_integer_underflow_nearest(x, z, prec)
434 } else {
435 pow_underflow(prec, rm, x.is_sign_negative() && z_odd)
436 };
437 }
438 (result, o)
439 } else {
440 // z < 0: compute (1/x)^|z| via t = 1/x rounded toward 1/-1, then a non-correctly-rounded
441 // positive power at extended precision, with a Ziv loop.
442 let abs_z = z.unsigned_abs_ref();
443 let size_z = abs_z.significant_bits();
444 let mut wprec = prec + size_z + 3 + prec.ceiling_log_base_2() + jump_extra;
445 let rnd1 = if x.get_exponent().unwrap() < 1 {
446 Down
447 } else if x.is_sign_positive() {
448 Up
449 } else {
450 Floor
451 };
452 loop {
453 let t = Float::ONE.div_prec_round_val_ref(x, wprec, rnd1).0;
454 if t.is_infinite() {
455 // For |x| < 1 the reciprocal is rounded toward zero, so an overflowing 1/x
456 // saturates at the largest finite value rather than reaching infinity (and the
457 // exact overflow decision above has already returned in that case); for |x| >= 1 it
458 // is at most 1.
459 fail_on_untested_path("pow_integer, 1/x overflow");
460 return pow_overflow(prec, rm, t.is_sign_negative());
461 }
462 let t = pow_pos_natural(&t, abs_z, wprec, rm, false, 0).0;
463 if t.is_infinite() {
464 // The exact overflow decision above bounds |x^z| < 2^MAX_EXPONENT, and the
465 // magnitude-decreasing rounding directions keep the computed value below it.
466 fail_on_untested_path("pow_integer, (1/x)^|z| overflow");
467 return pow_overflow(prec, rm, t.is_sign_negative());
468 }
469 if t.is_zero() {
470 if rm == Nearest {
471 return pow_integer_underflow_nearest(x, z, prec);
472 }
473 return pow_underflow(prec, rm, x.is_sign_negative() && z_odd);
474 }
475 let err = wprec - size_z - 2;
476 if float_can_round(t.significand_ref().unwrap(), err, prec, rm) {
477 return Float::from_float_prec_round(t, prec, rm);
478 }
479 wprec += wprec >> 1;
480 }
481 }
482}
483
484// This is `mpfr_pow_ui` (`POW_U`) from `pow_ui.c`, MPFR 4.3.0: x^n for a `u64` n, by binary
485// exponentiation with a Ziv loop, falling back to `pow_integer` (`mpfr_pow_z`) on an internal
486// overflow or underflow.
487fn pow_u(x: Float, n: u64, prec: u64, rm: RoundingMode) -> (Float, Ordering) {
488 // x^0 = 1 for any x, even NaN
489 if n == 0 {
490 return (Float::one_prec(prec), Equal);
491 }
492 if x.is_nan() {
493 return (Float::NAN, Equal);
494 }
495 if x.is_infinite() {
496 // Inf^n = Inf; (-Inf)^n = Inf for n even, -Inf for n odd
497 return (
498 if x.is_sign_negative() && n.odd() {
499 Float::NEGATIVE_INFINITY
500 } else {
501 Float::INFINITY
502 },
503 Equal,
504 );
505 }
506 if x.is_zero() {
507 // 0^n = 0 for any n; positive unless x is negative and n is odd
508 return (
509 if x.is_sign_negative() && n.odd() {
510 Float::NEGATIVE_ZERO
511 } else {
512 Float::ZERO
513 },
514 Equal,
515 );
516 }
517 if n <= 2 {
518 return if n == 1 {
519 // x^1 = x
520 Float::from_float_prec_round(x, prec, rm)
521 } else {
522 // x^2 = sqr(x)
523 x.square_prec_round(prec, rm)
524 };
525 }
526 // n >= 3: square-and-multiply. `nlen` is the bit length of n, so 2^(nlen - 1) <= n < 2^nlen.
527 let nlen = n.significant_bits();
528 // Multiplications round away from zero (squares round up; their results are non-negative), so
529 // that an intermediate overflow or underflow is a true exception rather than rounding noise.
530 let rnd1 = if x.is_sign_positive() { Ceiling } else { Floor };
531 let mut wprec = {
532 let p = prec + 67 + prec.ceiling_log_base_2();
533 if p <= nlen {
534 // Unreachable for a `u64` n: p >= 1 + 3 + 64 = 68 always exceeds nlen, which is at most
535 // 64. (In MPFR, where GMP_NUMB_BITS may be 32 and n may be wider, this clamp matters.)
536 fail_on_untested_path("pow_u, working precision clamped up to nlen + 1");
537 nlen + 1
538 } else {
539 p
540 }
541 };
542 match pow_near_one_fast_path(&x, nlen, false, x.is_sign_negative() && n.odd(), prec, rm) {
543 NearOne::Rounded(v, o) => return (v, o),
544 NearOne::JumpStart(extra) => wprec += extra,
545 NearOne::No => {}
546 }
547 loop {
548 let err = wprec - 1 - nlen;
549 let (mut res, o) = x.square_prec_round_ref(wprec, Ceiling);
550 let mut inexact = o != Equal;
551 let mut i = nlen;
552 if n.get_bit(i - 2) {
553 inexact |= res.mul_prec_round_assign_ref(&x, wprec, rnd1) != Equal;
554 }
555 if i > 2 {
556 i -= 3;
557 loop {
558 if res.is_infinite() || res.is_zero() {
559 break;
560 }
561 inexact |= res.square_prec_round_assign(wprec, Ceiling) != Equal;
562 if n.get_bit(i) {
563 inexact |= res.mul_prec_round_assign_ref(&x, wprec, rnd1) != Equal;
564 }
565 if i == 0 {
566 break;
567 }
568 i -= 1;
569 }
570 }
571 // Internal overflow (res is infinite) or underflow (res reached the minimum exponent): the
572 // approximation error has not been accounted for, so hand off to `pow_integer`, which
573 // handles the exponent range precisely.
574 if res.is_infinite() || res.is_zero() || res.get_exponent().unwrap() <= Float::MIN_EXPONENT
575 {
576 if res.is_zero() {
577 // Unreachable: squares round up and multiplications round away from zero, so res is
578 // a magnitude over-estimate that never rounds to zero; underflow instead surfaces
579 // as the minimum binade, handled by the exponent check above.
580 fail_on_untested_path("pow_u, res rounded to zero");
581 }
582 return x.pow_integer_prec_round(Integer::from(n), prec, rm);
583 }
584 if !inexact || float_can_round(res.significand_ref().unwrap(), err, prec, rm) {
585 return Float::from_float_prec_round(res, prec, rm);
586 }
587 wprec += wprec >> 1;
588 }
589}
590
591// This is `mpfr_pow_ui` (`POW_U`) from `pow_ui.c`, MPFR 4.3.0: x^n for a `u64` n, by binary
592// exponentiation with a Ziv loop, falling back to `pow_integer` (`mpfr_pow_z`) on an internal
593// overflow or underflow.
594fn pow_u_ref(x: &Float, n: u64, prec: u64, rm: RoundingMode) -> (Float, Ordering) {
595 // x^0 = 1 for any x, even NaN
596 if n == 0 {
597 return (Float::one_prec(prec), Equal);
598 }
599 if x.is_nan() {
600 return (Float::NAN, Equal);
601 }
602 if x.is_infinite() {
603 // Inf^n = Inf; (-Inf)^n = Inf for n even, -Inf for n odd
604 return (
605 if x.is_sign_negative() && n.odd() {
606 Float::NEGATIVE_INFINITY
607 } else {
608 Float::INFINITY
609 },
610 Equal,
611 );
612 }
613 if x.is_zero() {
614 // 0^n = 0 for any n; positive unless x is negative and n is odd
615 return (
616 if x.is_sign_negative() && n.odd() {
617 Float::NEGATIVE_ZERO
618 } else {
619 Float::ZERO
620 },
621 Equal,
622 );
623 }
624 if n <= 2 {
625 return if n == 1 {
626 // x^1 = x
627 Float::from_float_prec_round_ref(x, prec, rm)
628 } else {
629 // x^2 = sqr(x)
630 x.square_prec_round_ref(prec, rm)
631 };
632 }
633 // n >= 3: square-and-multiply. `nlen` is the bit length of n, so 2^(nlen - 1) <= n < 2^nlen.
634 let nlen = n.significant_bits();
635 // Multiplications round away from zero (squares round up; their results are non-negative), so
636 // that an intermediate overflow or underflow is a true exception rather than rounding noise.
637 let rnd1 = if x.is_sign_positive() { Ceiling } else { Floor };
638 let mut wprec = {
639 let p = prec + 67 + prec.ceiling_log_base_2();
640 if p <= nlen {
641 // Unreachable for a `u64` n: p >= 1 + 3 + 64 = 68 always exceeds nlen, which is at most
642 // 64. (In MPFR, where GMP_NUMB_BITS may be 32 and n may be wider, this clamp matters.)
643 fail_on_untested_path("pow_u, working precision clamped up to nlen + 1");
644 nlen + 1
645 } else {
646 p
647 }
648 };
649 match pow_near_one_fast_path(x, nlen, false, x.is_sign_negative() && n.odd(), prec, rm) {
650 NearOne::Rounded(v, o) => return (v, o),
651 NearOne::JumpStart(extra) => wprec += extra,
652 NearOne::No => {}
653 }
654 loop {
655 let err = wprec - 1 - nlen;
656 let (mut res, o) = x.square_prec_round_ref(wprec, Ceiling);
657 let mut inexact = o != Equal;
658 let mut i = nlen;
659 if n.get_bit(i - 2) {
660 inexact |= res.mul_prec_round_assign_ref(x, wprec, rnd1) != Equal;
661 }
662 if i > 2 {
663 i -= 3;
664 loop {
665 if res.is_infinite() || res.is_zero() {
666 break;
667 }
668 inexact |= res.square_prec_round_assign(wprec, Ceiling) != Equal;
669 if n.get_bit(i) {
670 inexact |= res.mul_prec_round_assign_ref(x, wprec, rnd1) != Equal;
671 }
672 if i == 0 {
673 break;
674 }
675 i -= 1;
676 }
677 }
678 // Internal overflow (res is infinite) or underflow (res reached the minimum exponent): the
679 // approximation error has not been accounted for, so hand off to `pow_integer`, which
680 // handles the exponent range precisely.
681 if res.is_infinite() || res.is_zero() || res.get_exponent().unwrap() <= Float::MIN_EXPONENT
682 {
683 if res.is_zero() {
684 // Unreachable: squares round up and multiplications round away from zero, so res is
685 // a magnitude over-estimate that never rounds to zero; underflow instead surfaces
686 // as the minimum binade, handled by the exponent check above.
687 fail_on_untested_path("pow_u, res rounded to zero");
688 }
689 return x.pow_integer_prec_round_ref_val(Integer::from(n), prec, rm);
690 }
691 if !inexact || float_can_round(res.significand_ref().unwrap(), err, prec, rm) {
692 return Float::from_float_prec_round(res, prec, rm);
693 }
694 wprec += wprec >> 1;
695 }
696}
697
698// 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
699// `pow_u` (`mpfr_pow_ui`); for n < 0, x^n = (1/x)^|n| is computed by `pow_integer` (`mpfr_pow_z`),
700// whose negative-exponent path is exactly what `mpfr_pow_si` inlines.
701fn pow_s(x: Float, n: i64, prec: u64, rm: RoundingMode) -> (Float, Ordering) {
702 if n >= 0 {
703 pow_u(x, n.unsigned_abs(), prec, rm)
704 } else {
705 x.pow_integer_prec_round(Integer::from(n), prec, rm)
706 }
707}
708
709fn pow_s_ref(x: &Float, n: i64, prec: u64, rm: RoundingMode) -> (Float, Ordering) {
710 if n >= 0 {
711 pow_u_ref(x, n.unsigned_abs(), prec, rm)
712 } else {
713 x.pow_integer_prec_round_ref_val(Integer::from(n), prec, rm)
714 }
715}
716
717// 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
718// binary exponentiation (all roundings up, so the result is a magnitude over-estimate), falling
719// back to `pow_integer` (`mpfr_pow_z`) on overflow. Since k, n >= 0 the result never underflows.
720//
721// The error budget deliberately deviates from MPFR, whose accounting (one rounding for the initial
722// value plus one per squaring, size_n in all) undercounts: the initial rounding of k is amplified
723// to the n-th power through the squarings, and the multiplications contribute up to size_n - 1 more
724// factors, for at most 2n - 1 < 2^(size_n + 1) Higham factors in all -- a relative error below
725// 2^(size_n + 2 - wprec), so size_n + 2 bits are reserved. With MPFR's budget the `float_can_round`
726// gate certifies wrongly rounded results at small precisions (upstream mpfr_ui_pow_ui reproduces
727// this: 263^15 at precision 1 under `Nearest` returns 2^121 though the true value lies below the
728// tie 1.5 * 2^120, and 205^63 at precision 4 under `Down` returns a value above the true one).
729fn unsigned_pow_unsigned(k: u64, n: u64, prec: u64, rm: RoundingMode) -> (Float, Ordering) {
730 if n == 0 {
731 // k^0 = 1 for any k
732 return (Float::one_prec(prec), Equal);
733 } else if n == 1 || k <= 1 {
734 // k^1 = k; 1^n = 1 and 0^n = 0 for n >= 1; either way the value is k
735 return Float::from_unsigned_prec_round(k, prec, rm);
736 }
737 // k >= 2, n >= 2. `size_n` is the bit length of n, so 2^(size_n - 1) <= n < 2^size_n.
738 let size_n = n.significant_bits();
739 // k as an exact Float, for the multiplications.
740 let kf = Float::from(k);
741 let mut wprec = prec + 5 + size_n;
742 loop {
743 // res starts as k (rounded up), contributing the most significant bit of n.
744 let (mut res, o) = Float::from_unsigned_prec_round(k, wprec, Ceiling);
745 let mut inexact = o != Equal;
746 // err counts the roundings: 1 for the initial value, plus one per squaring.
747 for bit in n.bits().rev().skip(1) {
748 inexact |= res.square_prec_round_assign(wprec, Ceiling) != Equal;
749 if bit {
750 inexact |= res.mul_prec_round_assign_ref(&kf, wprec, Ceiling) != Equal;
751 }
752 }
753 if res.is_infinite() {
754 // Overflow: the approximation error has not been accounted for, so hand off to
755 // `pow_integer`, which handles the exponent range precisely.
756 return kf.pow_integer_prec_round(Integer::from(n), prec, rm);
757 }
758 if !inexact || float_can_round(res.significand_ref().unwrap(), wprec - size_n - 2, prec, rm)
759 {
760 return Float::from_float_prec_round(res, prec, rm);
761 }
762 wprec += wprec >> 1;
763 }
764}
765
766// This is `mpfr_pow_is_exact` from `pow.c`, MPFR 4.3.0: assuming x > 0, x not a power of 2, y
767// finite non-integer, decides whether x^y is exact, and if so computes it.
768fn pow_is_exact(x: &Float, y: &Float, prec: u64, rm: RoundingMode) -> Option<(Float, Ordering)> {
769 if y.is_sign_negative() {
770 return None;
771 }
772 // y = c * 2^d with c an odd integer, d < 0
773 let (c, mut d) = float_to_odd_mantissa_and_exponent(y);
774 // y is not an integer (the callers filter integers), so it has fractional bits.
775 assert!(d < 0);
776 // x = a * 2^b with a odd
777 let (mut a, mut b) = float_to_odd_mantissa_and_exponent_natural(x);
778 while d != 0 {
779 if b.odd() {
780 a <<= 1u32;
781 b -= 1;
782 }
783 a = a.checked_sqrt()?;
784 b >>= 1;
785 d += 1;
786 }
787 // x^y = (a * 2^b)^c with c an odd integer
788 let tmp_prec = a.significant_bits();
789 let tmp = Float::from_natural_prec_round(a, tmp_prec, Exact)
790 .0
791 .shl_prec_round(b, tmp_prec, Exact)
792 .0;
793 Some(pow_integer(&tmp, &c, prec, rm))
794}
795
796// Resolves |x|^y when the true product y * ln|x| lies at or below the bottom of the Float exponent
797// range. In that regime the Ziv loop's Ceiling-rounded product either underflows to -0.0 (making
798// exp return exactly 1, whose all-zero error window `float_can_round` can never certify -- an
799// infinite loop) or saturates at the minimum positive value (an overestimate whose error the loop's
800// budget does not account for, letting it certify a wrongly rounded result near the `Nearest` tie).
801// MPFR computes the product in an extended exponent range; malachite has none, so the tiny-product
802// case is resolved in exact Rational arithmetic, which has no exponent range at all.
803//
804// The true result is 1 + delta with 0 < |delta| <= 2^(MIN_EXPONENT + 1). Exact dyadic results --
805// including `Nearest` ties, which are dyadic -- are delegated to `pow_is_exact`; the remaining
806// values are irrationals strictly between any rounding boundaries, so bracketing exp(t) between the
807// exact Rationals 1 + t_lo and 1 + t_hi + t_hi^2 (valid for |t| <= 1/2) and widening the ln|x|
808// brackets Ziv-style always terminates. For |x| within a sliver of 1, ln|x| is bracketed by the
809// exact atanh-series helper -- a direct `ln` would need working precision on the order of the
810// sliver's depth (up to ~2^30 bits) to survive the cancellation.
811fn pow_general_tiny_product(
812 abs_x: &Float,
813 y: &Float,
814 prec: u64,
815 rm: RoundingMode,
816) -> (Float, Ordering) {
817 // y is never an integer here: the entry's sliver-of-one guard keeps |ln|x|| >= 2^(MIN_EXPONENT
818 // + 8), so an integer y (with |y| >= 1) cannot make the product underflow.
819 if let Some(result) = pow_is_exact(abs_x, y, prec, rm) {
820 return result;
821 }
822 let yr = Rational::exact_from(y);
823 let y_pos = *y > 0u32;
824 // Classify |x| as near 1 or not with a cheap low-precision subtraction; near the threshold
825 // either branch is correct, so the classification need not be exact.
826 let near_one = abs_x
827 .sub_prec_ref_val(Float::ONE, 64)
828 .0
829 .get_exponent()
830 .unwrap()
831 < -8;
832 let e = if near_one {
833 Some(Rational::exact_from(abs_x) - Rational::ONE)
834 } else {
835 None
836 };
837 let mut wp = 128;
838 loop {
839 // ln_lo <= ln|x| <= ln_hi, as exact Rationals
840 let (ln_lo, ln_hi) = if let Some(e) = &e {
841 ln_1_plus_rational_brackets(e, wp)
842 } else {
843 (
844 Rational::exact_from(abs_x.ln_prec_round_ref(wp, Floor).0),
845 Rational::exact_from(abs_x.ln_prec_round_ref(wp, Ceiling).0),
846 )
847 };
848 // t_lo <= y ln|x| <= t_hi
849 let (t_lo, t_hi) = if y_pos {
850 (&yr * ln_lo, &yr * ln_hi)
851 } else {
852 (&yr * ln_hi, &yr * ln_lo)
853 };
854 // 1 + t <= exp(t) <= 1 + t + t^2 for |t| <= 1/2
855 let lower = Rational::ONE + &t_lo;
856 let upper = Rational::ONE + &t_hi + (&t_hi).square();
857 let (p_lo, mut o_lo) = Float::from_rational_prec_round(lower, prec, rm);
858 let (p_hi, mut o_hi) = Float::from_rational_prec_round(upper, prec, rm);
859 // A bracket end landing exactly on a representable value rounds with `Equal`; the true
860 // value lies strictly between the ends, so the other end's ordering is the true one.
861 if o_lo == Equal {
862 o_lo = o_hi;
863 }
864 if o_hi == Equal {
865 o_hi = o_lo;
866 }
867 // `lower` and `upper` are positive Rationals near 1 (the result is `1 + tiny`), so
868 // `from_rational_prec_round` yields a positive value at precision `prec`, never `NaN` or
869 // `-0.0`, and a plain value comparison suffices.
870 if o_lo == o_hi && p_lo == p_hi {
871 return (p_lo, o_lo);
872 }
873 wp <<= 1;
874 }
875}
876
877// This is `mpfr_pow_general` from `pow.c`, MPFR 4.3.0: the Ziv loop computing exp(y * ln|x|), with
878// a scaling factor 2^k to dodge intermediate overflow and underflow.
879fn pow_general(
880 x: &Float,
881 y: &Float,
882 prec: u64,
883 mut rm: RoundingMode,
884 y_is_integer: bool,
885) -> (Float, Ordering) {
886 let abs_x = x.abs();
887 let mut neg_result = false;
888 if x.is_sign_negative() {
889 assert!(y_is_integer);
890 if float_odd_integer(y) {
891 neg_result = true;
892 rm.neg_assign(); // invert directed modes; Nearest stays
893 }
894 }
895 let mut wprec = prec + 9 + prec.ceiling_log_base_2();
896 // Pre-detect a product y * ln|x| below the exponent range, without first computing ln|x| at
897 // working precision: for |x| within a deep sliver of 1 that ln costs on the order of |log2(|x|
898 // - 1)| bits of internal precision (up to ~2^30) only for the product to underflow anyway. The
899 // exponent estimate errs on the side of not firing; the in-loop detection below is the
900 // backstop.
901 let ey = i64::from(y.get_exponent().unwrap());
902 let d = abs_x.sub_prec_ref_val(Float::ONE, 64).0;
903 let d_exp = i64::from(d.get_exponent().unwrap());
904 // the exponent of ln|x|, within ~1: for |x| near 1, ln|x| ~ |x| - 1; otherwise |ln|x|| > 2^-9
905 // and a 64-bit ln suffices
906 let ln_exp = if d_exp < -8 {
907 d_exp
908 } else {
909 i64::from(abs_x.ln_prec_round_ref(64, Floor).0.get_exponent().unwrap())
910 };
911 // Product exponents add within 1 (exp(a * b) is exp(a) + exp(b) or one less), and ln_exp itself
912 // is accurate within ~1, so trigger with a couple of binades of margin. Over-triggering is
913 // harmless: the resolver is correct for any small product, and for the borderline
914 // (bottom-binade but representable) products the x involved is deep within a near-sliver of 1,
915 // where the loop's `ln` would need catastrophic working precision anyway.
916 if ey.saturating_add(ln_exp) <= Float::MIN_EXPONENT_PLUS_2_I64 {
917 let (mut result, mut o) = pow_general_tiny_product(&abs_x, y, prec, rm);
918 if neg_result {
919 result.neg_assign();
920 o = o.reverse();
921 }
922 return (result, o);
923 }
924 let mut k: Option<Integer> = None;
925 let mut check_exact_case = false;
926 let mut exact_case = false;
927 let mut result;
928 let mut o;
929 loop {
930 // t = ln|x|, rounded so that t is an upper bound on y * ln|x|
931 let mut t = abs_x
932 .ln_prec_round_ref(wprec, if y.is_sign_negative() { Floor } else { Ceiling })
933 .0;
934 t.mul_prec_round_assign_ref(y, wprec, Ceiling);
935 // A product below the exponent range comes back as -0.0 (negative underflow) or saturated
936 // at the minimum positive value (positive underflow); both derail the loop, so resolve them
937 // exactly. (A genuine product equal to the minimum positive value takes this path too,
938 // harmlessly.)
939 if k.is_none()
940 && (t.is_zero()
941 || (t.get_exponent() == Some(Float::MIN_EXPONENT) && raw_power_of_2(&t)))
942 {
943 (result, o) = pow_general_tiny_product(&abs_x, y, prec, rm);
944 break;
945 }
946 let exp_t = t.get_exponent().map_or(0, i64::from);
947 if let Some(kv) = &k {
948 t.sub_prec_round_assign(
949 Float::ln_2_prec_round(wprec, Floor)
950 .0
951 .mul_prec_round(
952 Float::from_signed_prec(i64::exact_from(kv), wprec).0,
953 wprec,
954 Floor,
955 )
956 .0,
957 wprec,
958 Ceiling,
959 );
960 }
961 let mut err = if !t.is_zero() && exp_t >= -1 {
962 exp_t + 3
963 } else {
964 1
965 };
966 if let Some(kv) = &k {
967 let exp_k = i64::exact_from(kv.significant_bits());
968 if exp_k > err {
969 err = exp_k;
970 }
971 err += 1;
972 }
973 t.exp_prec_assign(wprec);
974 // MPFR checks the underflow flag here, which also fires when the result rounds UP into the
975 // bottom binade (e.g. to the minimum positive value); malachite has no flags, so treat any
976 // bottom-binade result as "possibly spurious underflow" and take the 2^k rescue path, which
977 // recomputes in a comfortable range.
978 let t_bottom_binade = t.is_finite()
979 && !t.is_zero()
980 && k.is_none()
981 && t.get_exponent()
982 .is_some_and(|e| i64::from(e) == Float::MIN_EXPONENT_I64);
983 if t.is_zero() || t.is_infinite() || t_bottom_binade {
984 // After a 2^k rescue the computation stays comfortably in range, so a singular result
985 // cannot recur (MPFR_ASSERTN(!k_non_zero) in mpfr_pow_general).
986 assert!(k.is_none());
987 if t.is_zero() {
988 // real underflow of |x|^y
989 (result, o) = pow_underflow(prec, if rm == Nearest { Down } else { rm }, false);
990 break;
991 }
992 if t.is_infinite() {
993 // possible overflow: recompute a lower bound
994 let t2 = abs_x
995 .ln_prec_round_ref(wprec, if y.is_sign_negative() { Ceiling } else { Floor })
996 .0
997 .mul_prec_round_val_ref(y, wprec, Floor)
998 .0
999 .exp_round(Floor)
1000 .0;
1001 if t2.is_infinite() {
1002 // The entry check bounds |x^y| < 2^MAX_EXPONENT, so the lower-bound
1003 // recomputation cannot be infinite.
1004 fail_on_untested_path("pow_general, confirmed overflow");
1005 (result, o) = pow_overflow(prec, rm, false);
1006 break;
1007 }
1008 }
1009 // scale by 2^-k with k ~ y*log2|x|
1010 k = Some(
1011 Integer::rounding_from(
1012 abs_x.log_base_2_prec_ref(64).0.mul_prec_val_ref(y, 64).0,
1013 Nearest,
1014 )
1015 .0,
1016 );
1017 continue;
1018 }
1019 if float_can_round(
1020 t.significand_ref().unwrap(),
1021 wprec.checked_sub(u64::saturating_from(err)).unwrap_or(1),
1022 prec,
1023 rm,
1024 ) {
1025 (result, o) = Float::from_float_prec_round(t, prec, rm);
1026 break;
1027 }
1028 if !check_exact_case && !y_is_integer {
1029 if let Some((z, oz)) = pow_is_exact(&abs_x, y, prec, rm) {
1030 result = z;
1031 o = oz;
1032 exact_case = true;
1033 break;
1034 }
1035 check_exact_case = true;
1036 }
1037 wprec += wprec >> 1;
1038 }
1039 if !exact_case && let Some(kv) = &k {
1040 let lk = i64::exact_from(kv);
1041 // Double-rounding guard from `mpfr_pow_general`: in rounding to nearest, if the scaled
1042 // result would be exactly 2^(emin - 2) but the unscaled rounding already went below the
1043 // exact value, the true result is above the underflow tie point and must round up to
1044 // 2^(emin - 1), not down to zero. (The result is positive here; the sign is applied below.)
1045 let mut shift_rm = rm;
1046 if rm == Nearest
1047 && o == Less
1048 && lk < 0
1049 && result
1050 .get_exponent()
1051 .is_some_and(|e| i64::from(e) == Float::MIN_EXPONENT_MINUS_1_I64 - lk)
1052 && raw_power_of_2(&result)
1053 {
1054 shift_rm = Ceiling;
1055 }
1056 let (shifted, oo) = result.shl_prec_round(lk, prec, shift_rm);
1057 result = shifted;
1058 if oo != Equal {
1059 o = oo;
1060 }
1061 }
1062 if neg_result {
1063 result.neg_assign();
1064 o = o.reverse();
1065 }
1066 (result, o)
1067}
1068
1069// Decomposes a finite nonzero Float into (odd Integer mantissa, exponent): x = c * 2^d.
1070fn float_to_odd_mantissa_and_exponent(x: &Float) -> (Integer, i64) {
1071 let (n, d) = float_to_odd_mantissa_and_exponent_natural(&x.abs());
1072 (Integer::from_sign_and_abs(x.is_sign_positive(), n), d)
1073}
1074
1075fn float_to_odd_mantissa_and_exponent_natural(x: &Float) -> (Natural, i64) {
1076 let m = x.significand_ref().unwrap().clone();
1077 let e = i64::from(x.get_exponent().unwrap()) - i64::exact_from(m.significant_bits());
1078 let tz = m.trailing_zeros().unwrap();
1079 (m >> tz, e + i64::exact_from(tz))
1080}
1081
1082// Decides exactly whether z * log2|x| >= bound -- equivalently, whether |x|^z >= 2^bound -- for a
1083// finite nonzero x that is not a power of 2 and a nonzero z. Writing |x| = a * 2^b with a odd (and
1084// a >= 3, since x is not a power of 2), log2|x| = b + log2(a), and log2(a) is bracketed between
1085// exact Rationals at widening precision. log2(a) is irrational, so z * (b + log2(a)) never equals
1086// the integer bound and the comparison always resolves.
1087fn pow_exponent_at_least(x: &Float, z: &Integer, bound: i64) -> bool {
1088 let (a, b) = float_to_odd_mantissa_and_exponent_natural(&x.abs());
1089 debug_assert!(a > 1u32);
1090 let ar = Rational::from(a);
1091 let zr = Rational::from(z);
1092 let br = Rational::from(b);
1093 let bound_r = Rational::from(bound);
1094 let z_pos = *z > 0u32;
1095 let mut wprec = 128;
1096 loop {
1097 let (l_lo, l_hi) = log_2_rational_brackets(&ar, wprec);
1098 let (t_lo, t_hi) = if z_pos {
1099 (&zr * (&br + l_lo), &zr * (&br + l_hi))
1100 } else {
1101 (&zr * (&br + l_hi), &zr * (&br + l_lo))
1102 };
1103 if t_lo >= bound_r {
1104 return true;
1105 }
1106 if t_hi < bound_r {
1107 return false;
1108 }
1109 wprec <<= 1;
1110 }
1111}
1112
1113// If `|x|` is a sliver of 1 -- within a couple of binades of the smallest positive `Float`, where
1114// `ln|x|` falls below the smallest positive `Float` -- returns `x`'s exact `Rational` value, and
1115// otherwise `None`. Only a `Float` in `(1/2, 2)` with a precision near `2^30` can be a sliver, so
1116// the exact `Rational` (which occupies ~128 MB) is built only past the cheap exponent and precision
1117// tests.
1118fn float_sliver_of_one(x: &Float) -> Option<Rational> {
1119 let ex = i64::from(x.get_exponent().unwrap());
1120 if (ex == 0 || ex == 1) && x.get_prec().unwrap() >= Float::NEAR_ONE_MAX_PREC {
1121 let xr = Rational::exact_from(x);
1122 let d = (&xr).abs() - Rational::ONE;
1123 if d != 0u32 && d.floor_log_base_2_abs() < Float::MIN_EXPONENT_PLUS_8_I64 {
1124 return Some(xr);
1125 }
1126 }
1127 None
1128}
1129
1130impl Float {
1131 // This is `mpfr_pow` from `pow.c`, MPFR 4.3.0.
1132
1133 /// Raises a [`Float`] to a [`Float`] power, rounding the result to the specified precision and
1134 /// with the specified rounding mode. Both [`Float`]s are taken by reference. An [`Ordering`] is
1135 /// also returned, indicating whether the rounded power is less than, equal to, or greater than
1136 /// the exact power. Although `NaN`s are not comparable to any [`Float`], whenever this function
1137 /// returns a `NaN` it also returns `Equal`.
1138 ///
1139 /// See [`RoundingMode`] for a description of the possible rounding modes.
1140 ///
1141 /// $$
1142 /// f(x,y,p,m) = x^y+\varepsilon.
1143 /// $$
1144 /// - If $x^y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
1145 /// - If $x^y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
1146 /// 2^{\lfloor\log_2 |x^y|\rfloor-p+1}$.
1147 /// - If $x^y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
1148 /// 2^{\lfloor\log_2 |x^y|\rfloor-p}$.
1149 ///
1150 /// If the output has a precision, it is `prec`.
1151 ///
1152 /// Special cases:
1153 /// - $f(x,\pm0.0,p,m)=1.0$ for any $x$, even `NaN`
1154 /// - $f(1.0,y,p,m)=1.0$ for any $y$, even `NaN`
1155 /// - $f(\text{NaN},y,p,m)=f(x,\text{NaN},p,m)=\text{NaN}$ otherwise
1156 /// - $f(x,\infty,p,m)=\infty$ if $|x|>1$, and $0.0$ if $|x|<1$
1157 /// - $f(x,-\infty,p,m)=0.0$ if $|x|>1$, and $\infty$ if $|x|<1$
1158 /// - $f(-1.0,\pm\infty,p,m)=1.0$
1159 /// - $f(-1.0,y,p,m)=1.0$ if $y$ is an even integer, and $-1.0$ if $y$ is an odd integer
1160 /// - $f(\infty,y,p,m)=\infty$ if $y>0$, and $0.0$ if $y<0$
1161 /// - $f(-\infty,y,p,m)=-\infty$ if $y$ is a positive odd integer, $\infty$ if $y$ is positive
1162 /// and not an odd integer, $-0.0$ if $y$ is a negative odd integer, and $0.0$ if $y$ is
1163 /// negative and not an odd integer
1164 /// - $f(0.0,y,p,m)=0.0$ if $y>0$, and $\infty$ if $y<0$
1165 /// - $f(-0.0,y,p,m)=-0.0$ if $y$ is a positive odd integer, $0.0$ if $y$ is positive and not an
1166 /// odd integer, $-\infty$ if $y$ is a negative odd integer, and $\infty$ if $y$ is negative
1167 /// and not an odd integer
1168 /// - $f(x,y,p,m)=\text{NaN}$ if $x$ is finite and negative and $y$ is finite and not an integer
1169 ///
1170 /// Overflow and underflow:
1171 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
1172 /// returned instead.
1173 /// - 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}$
1174 /// is returned instead.
1175 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
1176 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
1177 /// instead.
1178 /// - If $0<f(x,y,p,m)\leq2^{-2^{30}-1}$ and $m$ is `Nearest`, $0.0$ is returned instead.
1179 /// - If $2^{-2^{30}-1}<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
1180 /// instead.
1181 /// - Negative results (from negative $x$ and odd integer $y$) mirror the bullets above, with
1182 /// the rounding directions reflected.
1183 ///
1184 /// If you know you'll be using `Nearest`, consider using [`Float::pow_prec_ref_ref`] instead.
1185 /// If you know that your target precision is the maximum of the precisions of the two inputs,
1186 /// consider using [`Float::pow_round_ref_ref`] instead. If both of these things are true,
1187 /// consider using [`Pow::pow`] instead.
1188 ///
1189 /// # Worst-case complexity
1190 /// $T(n, m) = O(n^{3/2} \log n \log\log n + m)$
1191 ///
1192 /// $M(n, m) = O(n \log n + m)$
1193 ///
1194 /// where $T$ is time, $M$ is additional memory, $n$ is `prec`, and $m$ is
1195 /// `max(self.significant_bits(), other.significant_bits())`.
1196 ///
1197 /// # Panics
1198 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the given
1199 /// precision.
1200 ///
1201 /// # Examples
1202 /// ```
1203 /// use malachite_base::rounding_modes::RoundingMode::*;
1204 /// use malachite_float::Float;
1205 /// use std::cmp::Ordering::*;
1206 ///
1207 /// let (p, o) = (&Float::from(3)).pow_prec_round_ref_ref(&Float::from(2.5), 5, Floor);
1208 /// assert_eq!(p.to_string(), "15.5");
1209 /// assert_eq!(o, Less);
1210 ///
1211 /// let (p, o) = (&Float::from(3)).pow_prec_round_ref_ref(&Float::from(2.5), 5, Ceiling);
1212 /// assert_eq!(p.to_string(), "16.0");
1213 /// assert_eq!(o, Greater);
1214 ///
1215 /// let (p, o) = (&Float::from(3)).pow_prec_round_ref_ref(&Float::from(2.5), 5, Nearest);
1216 /// assert_eq!(p.to_string(), "15.5");
1217 /// assert_eq!(o, Less);
1218 ///
1219 /// let (p, o) = (&Float::from(3)).pow_prec_round_ref_ref(&Float::from(2.5), 20, Floor);
1220 /// assert_eq!(p.to_string(), "15.588455");
1221 /// assert_eq!(o, Less);
1222 ///
1223 /// let (p, o) = (&Float::from(3)).pow_prec_round_ref_ref(&Float::from(2.5), 20, Ceiling);
1224 /// assert_eq!(p.to_string(), "15.588470");
1225 /// assert_eq!(o, Greater);
1226 ///
1227 /// let (p, o) = (&Float::from(3)).pow_prec_round_ref_ref(&Float::from(2.5), 20, Nearest);
1228 /// assert_eq!(p.to_string(), "15.588455");
1229 /// assert_eq!(o, Less);
1230 /// ```
1231 pub fn pow_prec_round_ref_ref(
1232 &self,
1233 y: &Self,
1234 prec: u64,
1235 rm: RoundingMode,
1236 ) -> (Self, Ordering) {
1237 assert_ne!(prec, 0);
1238 // Exact rounding: compute with Nearest and demand exactness (the exact cases all flow
1239 // through the integer-power and exact-power paths, which report Equal).
1240 if rm == Exact {
1241 let (result, o) = self.pow_prec_ref_ref(y, prec);
1242 assert_eq!(o, Equal, "Inexact pow");
1243 return (result, Equal);
1244 }
1245 let x = self;
1246 // Singular cases; see Section F.9.4.4 of the C standard.
1247 match (x, y) {
1248 // pow(x, 0) = 1 for any x, even NaN
1249 (_, float_either_zero!()) => {
1250 return (Self::one_prec(prec), Equal);
1251 }
1252 (float_nan!(), _) => return (Self::NAN, Equal),
1253 // pow(+1, NaN) = 1
1254 (_, float_nan!()) => {
1255 return if *x == 1u32 {
1256 (Self::one_prec(prec), Equal)
1257 } else {
1258 (Self::NAN, Equal)
1259 };
1260 }
1261 (float_either_infinity!(), Self(Infinity { sign })) => {
1262 return if *sign {
1263 (Self::INFINITY, Equal)
1264 } else {
1265 (Self::ZERO, Equal)
1266 };
1267 }
1268 (_, Self(Infinity { sign })) => {
1269 let mut cmp = x.partial_cmp_abs(&Self::ONE).unwrap();
1270 if !*sign {
1271 cmp = cmp.reverse();
1272 }
1273 return match cmp {
1274 Greater => (Self::INFINITY, Equal),
1275 Less => (Self::ZERO, Equal),
1276 Equal => (Self::one_prec(prec), Equal),
1277 };
1278 }
1279 (Self(Infinity { sign }), _) => {
1280 let negative = !*sign && float_odd_integer(y);
1281 return (
1282 match (y.is_sign_positive(), negative) {
1283 (true, false) => Self::INFINITY,
1284 (true, true) => Self::NEGATIVE_INFINITY,
1285 (false, false) => Self::ZERO,
1286 (false, true) => Self::NEGATIVE_ZERO,
1287 },
1288 Equal,
1289 );
1290 }
1291 (Self(Zero { sign }), _) => {
1292 let negative = !*sign && float_odd_integer(y);
1293 return (
1294 match (y.is_sign_negative(), negative) {
1295 (true, false) => Self::INFINITY,
1296 (true, true) => Self::NEGATIVE_INFINITY,
1297 (false, false) => Self::ZERO,
1298 (false, true) => Self::NEGATIVE_ZERO,
1299 },
1300 Equal,
1301 );
1302 }
1303 _ => {}
1304 }
1305 // x^y for x < 0 and y not an integer is not defined
1306 let y_is_integer = y.is_integer();
1307 if x.is_sign_negative() && !y_is_integer {
1308 return (Self::NAN, Equal);
1309 }
1310 let cmp_x_1 = x.partial_cmp_abs(&Self::ONE).unwrap();
1311 if cmp_x_1 == Equal {
1312 let negative = x.is_sign_negative() && float_odd_integer(y);
1313 return Self::from_float_prec_round(
1314 if negative { -Self::ONE } else { Self::ONE },
1315 prec,
1316 rm,
1317 );
1318 }
1319 // When |x| is a sliver of 1 -- within a couple of binades of the smallest positive Float --
1320 // ln|x| falls below the smallest positive Float, so every Float-based route below (the
1321 // early over/underflow bounds, `pow_general`) would underflow it and lose the precision
1322 // needed for y * ln|x| (which can still be an ordinary, even overflowing, value). Delegate
1323 // to the exact-Rational power, which brackets log2 with the atanh series over `Rational`s
1324 // and never materializes a sub-`MIN_EXPONENT` Float logarithm. Only huge-precision Floats
1325 // in (1/2, 2) can be slivers, so the exact Rational is built only past those cheap tests.
1326 if let Some(xr) = float_sliver_of_one(x) {
1327 return Self::rational_pow_prec_round_val_ref(xr, y, prec, rm);
1328 }
1329 let ex = i64::from(x.get_exponent().unwrap());
1330 let ey = i64::from(y.get_exponent().unwrap());
1331 // Fast check for no possible overflow or underflow: |y| <= 2^15 and moderate ex means |y *
1332 // log2|x|| stays far from the exponent limits.
1333 let no_over_under = ey <= 15 && -32767 < ex && ex <= 32767;
1334 if !no_over_under {
1335 // early overflow detection: lower bound on y * log2|x|
1336 if (cmp_x_1 == Greater) == y.is_sign_positive() {
1337 let t = x
1338 .abs()
1339 .log_base_2_prec_round_ref(64, Down)
1340 .0
1341 .mul_prec_round_val_ref(y, 64, Down)
1342 .0;
1343 if t >= const { Self::const_from_signed(Self::MAX_EXPONENT as SignedLimb) } {
1344 return pow_overflow(prec, rm, x.is_sign_negative() && float_odd_integer(y));
1345 }
1346 }
1347 // early underflow detection: ebound such that |x^y| < 2^ebound
1348 if if y.is_sign_negative() { ex > 1 } else { ex < 0 } {
1349 let mut tmp = Self::from_signed_prec(ex, 64).0;
1350 if y.is_sign_negative() {
1351 tmp.sub_prec_assign(Self::ONE, 64);
1352 }
1353 tmp.mul_prec_round_assign_ref(y, 64, Ceiling);
1354 let mut ebound = i64::rounding_from(&tmp, Ceiling).0;
1355 // For y < 0 the bound |x^y| <= 2^((ex - 1) * y) is not strict, so if the product is
1356 // an exact integer the exponent bound must be bumped to keep |x^y| < 2^ebound
1357 // (mpfr_nextabove(tmp) in mpfr_pow); otherwise x = 2^(ex - 1) exactly achieves the
1358 // bound and a representable result would be misreported as underflow.
1359 if y.is_sign_negative() && tmp == ebound {
1360 ebound += 1;
1361 }
1362 let lim = Self::MIN_EXPONENT_I64 - if rm == Nearest { 2 } else { 1 };
1363 if ebound <= lim {
1364 return pow_underflow(
1365 prec,
1366 if rm == Nearest { Down } else { rm },
1367 x.is_sign_negative() && float_odd_integer(y),
1368 );
1369 }
1370 }
1371 }
1372 // y a not-too-large integer: use the multiplication-based algorithm
1373 if y_is_integer && ey <= POW_EXP_THRESHOLD {
1374 return pow_integer(x, &Integer::rounding_from(y, Nearest).0, prec, rm);
1375 }
1376 // (+/-2^b)^y, which could be exact
1377 if raw_power_of_2(x) {
1378 if x.is_sign_negative() {
1379 // necessarily ey > threshold; |x| <= 1/2 means underflow (overflow was already
1380 // detected above)
1381 let negative = float_odd_integer(y);
1382 return pow_underflow(prec, if rm == Nearest { Down } else { rm }, negative);
1383 }
1384 let b = ex - 1;
1385 let (tmp, o) = y.mul_prec_ref_val(Self::from(b), y.significant_bits() + 64);
1386 assert_eq!(o, Equal);
1387 return Self::power_of_2_of_float_prec_round(tmp, prec, rm);
1388 }
1389 // y * ln(x) very small: 1 + tiny
1390 let expx = if cmp_x_1 == Less { 1 - ex } else { ex };
1391 let logt = i64::exact_from(u64::exact_from(expx.max(1)).ceiling_log_base_2());
1392 let err = ey + logt;
1393 if err < -i64::exact_from(prec) - 1 {
1394 let above = y.is_sign_positive() == (cmp_x_1 == Greater);
1395 return float_one_plus_tiny(prec, rm, above);
1396 }
1397 pow_general(x, y, prec, rm, y_is_integer)
1398 }
1399}
1400
1401impl Float {
1402 #[allow(clippy::needless_pass_by_value)]
1403 /// Raises a [`Float`] to a [`Float`] power, rounding the result to the specified precision and
1404 /// with the specified rounding mode. Both [`Float`]s are taken by value. An [`Ordering`] is
1405 /// also returned, indicating whether the rounded power is less than, equal to, or greater than
1406 /// the exact power. Although `NaN`s are not comparable to any [`Float`], whenever this function
1407 /// returns a `NaN` it also returns `Equal`.
1408 ///
1409 /// See [`RoundingMode`] for a description of the possible rounding modes.
1410 ///
1411 /// $$
1412 /// f(x,y,p,m) = x^y+\varepsilon.
1413 /// $$
1414 /// - If $x^y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
1415 /// - If $x^y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
1416 /// 2^{\lfloor\log_2 |x^y|\rfloor-p+1}$.
1417 /// - If $x^y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
1418 /// 2^{\lfloor\log_2 |x^y|\rfloor-p}$.
1419 ///
1420 /// If the output has a precision, it is `prec`.
1421 ///
1422 /// Special cases:
1423 /// - $f(x,\pm0.0,p,m)=1.0$ for any $x$, even `NaN`
1424 /// - $f(1.0,y,p,m)=1.0$ for any $y$, even `NaN`
1425 /// - $f(\text{NaN},y,p,m)=f(x,\text{NaN},p,m)=\text{NaN}$ otherwise
1426 /// - $f(x,\infty,p,m)=\infty$ if $|x|>1$, and $0.0$ if $|x|<1$
1427 /// - $f(x,-\infty,p,m)=0.0$ if $|x|>1$, and $\infty$ if $|x|<1$
1428 /// - $f(-1.0,\pm\infty,p,m)=1.0$
1429 /// - $f(-1.0,y,p,m)=1.0$ if $y$ is an even integer, and $-1.0$ if $y$ is an odd integer
1430 /// - $f(\infty,y,p,m)=\infty$ if $y>0$, and $0.0$ if $y<0$
1431 /// - $f(-\infty,y,p,m)=-\infty$ if $y$ is a positive odd integer, $\infty$ if $y$ is positive
1432 /// and not an odd integer, $-0.0$ if $y$ is a negative odd integer, and $0.0$ if $y$ is
1433 /// negative and not an odd integer
1434 /// - $f(0.0,y,p,m)=0.0$ if $y>0$, and $\infty$ if $y<0$
1435 /// - $f(-0.0,y,p,m)=-0.0$ if $y$ is a positive odd integer, $0.0$ if $y$ is positive and not an
1436 /// odd integer, $-\infty$ if $y$ is a negative odd integer, and $\infty$ if $y$ is negative
1437 /// and not an odd integer
1438 /// - $f(x,y,p,m)=\text{NaN}$ if $x$ is finite and negative and $y$ is finite and not an integer
1439 ///
1440 /// Overflow and underflow:
1441 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
1442 /// returned instead.
1443 /// - 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}$
1444 /// is returned instead.
1445 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
1446 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
1447 /// instead.
1448 /// - If $0<f(x,y,p,m)\leq2^{-2^{30}-1}$ and $m$ is `Nearest`, $0.0$ is returned instead.
1449 /// - If $2^{-2^{30}-1}<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
1450 /// instead.
1451 /// - Negative results (from negative $x$ and odd integer $y$) mirror the bullets above, with
1452 /// the rounding directions reflected.
1453 ///
1454 /// If you know you'll be using `Nearest`, consider using [`Float::pow_prec`] instead. If you
1455 /// know that your target precision is the maximum of the precisions of the two inputs, consider
1456 /// using [`Float::pow_round`] instead. If both of these things are true, consider using
1457 /// [`Pow::pow`] instead.
1458 ///
1459 /// # Worst-case complexity
1460 /// $T(n, m) = O(n^{3/2} \log n \log\log n + m)$
1461 ///
1462 /// $M(n, m) = O(n \log n + m)$
1463 ///
1464 /// where $T$ is time, $M$ is additional memory, $n$ is `prec`, and $m$ is
1465 /// `max(self.significant_bits(), other.significant_bits())`.
1466 ///
1467 /// # Panics
1468 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the given
1469 /// precision.
1470 ///
1471 /// # Examples
1472 /// ```
1473 /// use malachite_base::rounding_modes::RoundingMode::*;
1474 /// use malachite_float::Float;
1475 /// use std::cmp::Ordering::*;
1476 ///
1477 /// let (p, o) = Float::from(3).pow_prec_round(Float::from(2.5), 5, Floor);
1478 /// assert_eq!(p.to_string(), "15.5");
1479 /// assert_eq!(o, Less);
1480 ///
1481 /// let (p, o) = Float::from(3).pow_prec_round(Float::from(2.5), 5, Ceiling);
1482 /// assert_eq!(p.to_string(), "16.0");
1483 /// assert_eq!(o, Greater);
1484 ///
1485 /// let (p, o) = Float::from(3).pow_prec_round(Float::from(2.5), 5, Nearest);
1486 /// assert_eq!(p.to_string(), "15.5");
1487 /// assert_eq!(o, Less);
1488 ///
1489 /// let (p, o) = Float::from(3).pow_prec_round(Float::from(2.5), 20, Floor);
1490 /// assert_eq!(p.to_string(), "15.588455");
1491 /// assert_eq!(o, Less);
1492 ///
1493 /// let (p, o) = Float::from(3).pow_prec_round(Float::from(2.5), 20, Ceiling);
1494 /// assert_eq!(p.to_string(), "15.588470");
1495 /// assert_eq!(o, Greater);
1496 ///
1497 /// let (p, o) = Float::from(3).pow_prec_round(Float::from(2.5), 20, Nearest);
1498 /// assert_eq!(p.to_string(), "15.588455");
1499 /// assert_eq!(o, Less);
1500 /// ```
1501 #[inline]
1502 pub fn pow_prec_round(self, other: Self, prec: u64, rm: RoundingMode) -> (Self, Ordering) {
1503 self.pow_prec_round_ref_ref(&other, prec, rm)
1504 }
1505
1506 /// Raises a [`Float`] to a [`Float`] power, rounding the result to the specified precision and
1507 /// with the specified rounding mode. The first [`Float`] is taken by value and the second by
1508 /// reference. An [`Ordering`] is also returned, indicating whether the rounded power is less
1509 /// than, equal to, or greater than the exact power. Although `NaN`s are not comparable to any
1510 /// [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
1511 ///
1512 /// See [`RoundingMode`] for a description of the possible rounding modes.
1513 ///
1514 /// $$
1515 /// f(x,y,p,m) = x^y+\varepsilon.
1516 /// $$
1517 /// - If $x^y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
1518 /// - If $x^y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
1519 /// 2^{\lfloor\log_2 |x^y|\rfloor-p+1}$.
1520 /// - If $x^y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
1521 /// 2^{\lfloor\log_2 |x^y|\rfloor-p}$.
1522 ///
1523 /// If the output has a precision, it is `prec`.
1524 ///
1525 /// Special cases:
1526 /// - $f(x,\pm0.0,p,m)=1.0$ for any $x$, even `NaN`
1527 /// - $f(1.0,y,p,m)=1.0$ for any $y$, even `NaN`
1528 /// - $f(\text{NaN},y,p,m)=f(x,\text{NaN},p,m)=\text{NaN}$ otherwise
1529 /// - $f(x,\infty,p,m)=\infty$ if $|x|>1$, and $0.0$ if $|x|<1$
1530 /// - $f(x,-\infty,p,m)=0.0$ if $|x|>1$, and $\infty$ if $|x|<1$
1531 /// - $f(-1.0,\pm\infty,p,m)=1.0$
1532 /// - $f(-1.0,y,p,m)=1.0$ if $y$ is an even integer, and $-1.0$ if $y$ is an odd integer
1533 /// - $f(\infty,y,p,m)=\infty$ if $y>0$, and $0.0$ if $y<0$
1534 /// - $f(-\infty,y,p,m)=-\infty$ if $y$ is a positive odd integer, $\infty$ if $y$ is positive
1535 /// and not an odd integer, $-0.0$ if $y$ is a negative odd integer, and $0.0$ if $y$ is
1536 /// negative and not an odd integer
1537 /// - $f(0.0,y,p,m)=0.0$ if $y>0$, and $\infty$ if $y<0$
1538 /// - $f(-0.0,y,p,m)=-0.0$ if $y$ is a positive odd integer, $0.0$ if $y$ is positive and not an
1539 /// odd integer, $-\infty$ if $y$ is a negative odd integer, and $\infty$ if $y$ is negative
1540 /// and not an odd integer
1541 /// - $f(x,y,p,m)=\text{NaN}$ if $x$ is finite and negative and $y$ is finite and not an integer
1542 ///
1543 /// Overflow and underflow:
1544 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
1545 /// returned instead.
1546 /// - 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}$
1547 /// is returned instead.
1548 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
1549 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
1550 /// instead.
1551 /// - If $0<f(x,y,p,m)\leq2^{-2^{30}-1}$ and $m$ is `Nearest`, $0.0$ is returned instead.
1552 /// - If $2^{-2^{30}-1}<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
1553 /// instead.
1554 /// - Negative results (from negative $x$ and odd integer $y$) mirror the bullets above, with
1555 /// the rounding directions reflected.
1556 ///
1557 /// If you know you'll be using `Nearest`, consider using [`Float::pow_prec_val_ref`] instead.
1558 /// If you know that your target precision is the maximum of the precisions of the two inputs,
1559 /// consider using [`Float::pow_round_val_ref`] instead. If both of these things are true,
1560 /// consider using [`Pow::pow`] instead.
1561 ///
1562 /// # Worst-case complexity
1563 /// $T(n, m) = O(n^{3/2} \log n \log\log n + m)$
1564 ///
1565 /// $M(n, m) = O(n \log n + m)$
1566 ///
1567 /// where $T$ is time, $M$ is additional memory, $n$ is `prec`, and $m$ is
1568 /// `max(self.significant_bits(), other.significant_bits())`.
1569 ///
1570 /// # Panics
1571 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the given
1572 /// precision.
1573 ///
1574 /// # Examples
1575 /// ```
1576 /// use malachite_base::rounding_modes::RoundingMode::*;
1577 /// use malachite_float::Float;
1578 /// use std::cmp::Ordering::*;
1579 ///
1580 /// let (p, o) = Float::from(3).pow_prec_round_val_ref(&Float::from(2.5), 5, Floor);
1581 /// assert_eq!(p.to_string(), "15.5");
1582 /// assert_eq!(o, Less);
1583 ///
1584 /// let (p, o) = Float::from(3).pow_prec_round_val_ref(&Float::from(2.5), 5, Ceiling);
1585 /// assert_eq!(p.to_string(), "16.0");
1586 /// assert_eq!(o, Greater);
1587 ///
1588 /// let (p, o) = Float::from(3).pow_prec_round_val_ref(&Float::from(2.5), 5, Nearest);
1589 /// assert_eq!(p.to_string(), "15.5");
1590 /// assert_eq!(o, Less);
1591 ///
1592 /// let (p, o) = Float::from(3).pow_prec_round_val_ref(&Float::from(2.5), 20, Floor);
1593 /// assert_eq!(p.to_string(), "15.588455");
1594 /// assert_eq!(o, Less);
1595 ///
1596 /// let (p, o) = Float::from(3).pow_prec_round_val_ref(&Float::from(2.5), 20, Ceiling);
1597 /// assert_eq!(p.to_string(), "15.588470");
1598 /// assert_eq!(o, Greater);
1599 ///
1600 /// let (p, o) = Float::from(3).pow_prec_round_val_ref(&Float::from(2.5), 20, Nearest);
1601 /// assert_eq!(p.to_string(), "15.588455");
1602 /// assert_eq!(o, Less);
1603 /// ```
1604 #[inline]
1605 pub fn pow_prec_round_val_ref(
1606 self,
1607 other: &Self,
1608 prec: u64,
1609 rm: RoundingMode,
1610 ) -> (Self, Ordering) {
1611 self.pow_prec_round_ref_ref(other, prec, rm)
1612 }
1613
1614 #[allow(clippy::needless_pass_by_value)]
1615 /// Raises a [`Float`] to a [`Float`] power, rounding the result to the specified precision and
1616 /// with the specified rounding mode. The first [`Float`] is taken by reference and the second
1617 /// by value. An [`Ordering`] is also returned, indicating whether the rounded power is less
1618 /// than, equal to, or greater than the exact power. Although `NaN`s are not comparable to any
1619 /// [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
1620 ///
1621 /// See [`RoundingMode`] for a description of the possible rounding modes.
1622 ///
1623 /// $$
1624 /// f(x,y,p,m) = x^y+\varepsilon.
1625 /// $$
1626 /// - If $x^y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
1627 /// - If $x^y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
1628 /// 2^{\lfloor\log_2 |x^y|\rfloor-p+1}$.
1629 /// - If $x^y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
1630 /// 2^{\lfloor\log_2 |x^y|\rfloor-p}$.
1631 ///
1632 /// If the output has a precision, it is `prec`.
1633 ///
1634 /// Special cases:
1635 /// - $f(x,\pm0.0,p,m)=1.0$ for any $x$, even `NaN`
1636 /// - $f(1.0,y,p,m)=1.0$ for any $y$, even `NaN`
1637 /// - $f(\text{NaN},y,p,m)=f(x,\text{NaN},p,m)=\text{NaN}$ otherwise
1638 /// - $f(x,\infty,p,m)=\infty$ if $|x|>1$, and $0.0$ if $|x|<1$
1639 /// - $f(x,-\infty,p,m)=0.0$ if $|x|>1$, and $\infty$ if $|x|<1$
1640 /// - $f(-1.0,\pm\infty,p,m)=1.0$
1641 /// - $f(-1.0,y,p,m)=1.0$ if $y$ is an even integer, and $-1.0$ if $y$ is an odd integer
1642 /// - $f(\infty,y,p,m)=\infty$ if $y>0$, and $0.0$ if $y<0$
1643 /// - $f(-\infty,y,p,m)=-\infty$ if $y$ is a positive odd integer, $\infty$ if $y$ is positive
1644 /// and not an odd integer, $-0.0$ if $y$ is a negative odd integer, and $0.0$ if $y$ is
1645 /// negative and not an odd integer
1646 /// - $f(0.0,y,p,m)=0.0$ if $y>0$, and $\infty$ if $y<0$
1647 /// - $f(-0.0,y,p,m)=-0.0$ if $y$ is a positive odd integer, $0.0$ if $y$ is positive and not an
1648 /// odd integer, $-\infty$ if $y$ is a negative odd integer, and $\infty$ if $y$ is negative
1649 /// and not an odd integer
1650 /// - $f(x,y,p,m)=\text{NaN}$ if $x$ is finite and negative and $y$ is finite and not an integer
1651 ///
1652 /// Overflow and underflow:
1653 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
1654 /// returned instead.
1655 /// - 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}$
1656 /// is returned instead.
1657 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
1658 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
1659 /// instead.
1660 /// - If $0<f(x,y,p,m)\leq2^{-2^{30}-1}$ and $m$ is `Nearest`, $0.0$ is returned instead.
1661 /// - If $2^{-2^{30}-1}<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
1662 /// instead.
1663 /// - Negative results (from negative $x$ and odd integer $y$) mirror the bullets above, with
1664 /// the rounding directions reflected.
1665 ///
1666 /// If you know you'll be using `Nearest`, consider using [`Float::pow_prec_ref_val`] instead.
1667 /// If you know that your target precision is the maximum of the precisions of the two inputs,
1668 /// consider using [`Float::pow_round_ref_val`] instead. If both of these things are true,
1669 /// consider using [`Pow::pow`] instead.
1670 ///
1671 /// # Worst-case complexity
1672 /// $T(n, m) = O(n^{3/2} \log n \log\log n + m)$
1673 ///
1674 /// $M(n, m) = O(n \log n + m)$
1675 ///
1676 /// where $T$ is time, $M$ is additional memory, $n$ is `prec`, and $m$ is
1677 /// `max(self.significant_bits(), other.significant_bits())`.
1678 ///
1679 /// # Panics
1680 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the given
1681 /// precision.
1682 ///
1683 /// # Examples
1684 /// ```
1685 /// use malachite_base::rounding_modes::RoundingMode::*;
1686 /// use malachite_float::Float;
1687 /// use std::cmp::Ordering::*;
1688 ///
1689 /// let (p, o) = (&Float::from(3)).pow_prec_round_ref_val(Float::from(2.5), 5, Floor);
1690 /// assert_eq!(p.to_string(), "15.5");
1691 /// assert_eq!(o, Less);
1692 ///
1693 /// let (p, o) = (&Float::from(3)).pow_prec_round_ref_val(Float::from(2.5), 5, Ceiling);
1694 /// assert_eq!(p.to_string(), "16.0");
1695 /// assert_eq!(o, Greater);
1696 ///
1697 /// let (p, o) = (&Float::from(3)).pow_prec_round_ref_val(Float::from(2.5), 5, Nearest);
1698 /// assert_eq!(p.to_string(), "15.5");
1699 /// assert_eq!(o, Less);
1700 ///
1701 /// let (p, o) = (&Float::from(3)).pow_prec_round_ref_val(Float::from(2.5), 20, Floor);
1702 /// assert_eq!(p.to_string(), "15.588455");
1703 /// assert_eq!(o, Less);
1704 ///
1705 /// let (p, o) = (&Float::from(3)).pow_prec_round_ref_val(Float::from(2.5), 20, Ceiling);
1706 /// assert_eq!(p.to_string(), "15.588470");
1707 /// assert_eq!(o, Greater);
1708 ///
1709 /// let (p, o) = (&Float::from(3)).pow_prec_round_ref_val(Float::from(2.5), 20, Nearest);
1710 /// assert_eq!(p.to_string(), "15.588455");
1711 /// assert_eq!(o, Less);
1712 /// ```
1713 #[inline]
1714 pub fn pow_prec_round_ref_val(
1715 &self,
1716 other: Self,
1717 prec: u64,
1718 rm: RoundingMode,
1719 ) -> (Self, Ordering) {
1720 self.pow_prec_round_ref_ref(&other, prec, rm)
1721 }
1722
1723 #[allow(clippy::needless_pass_by_value)]
1724 /// Raises a [`Float`] to a [`Float`] power, rounding the result to the specified precision and
1725 /// to the nearest value. Both [`Float`]s are taken by value. An [`Ordering`] is also returned,
1726 /// indicating whether the rounded power is less than, equal to, or greater than the exact
1727 /// power. Although `NaN`s are not comparable to any [`Float`], whenever this function returns a
1728 /// `NaN` it also returns `Equal`.
1729 ///
1730 /// If the power is equidistant from two [`Float`]s with the specified precision, the [`Float`]
1731 /// with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of
1732 /// the `Nearest` rounding mode.
1733 ///
1734 /// $$
1735 /// f(x,y,p) = x^y+\varepsilon.
1736 /// $$
1737 /// - If $x^y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
1738 /// - If $x^y$ is finite and nonzero, then $|\varepsilon| \leq 2^{\lfloor\log_2
1739 /// |x^y|\rfloor-p}$.
1740 ///
1741 /// If the output has a precision, it is `prec`.
1742 ///
1743 /// Special cases:
1744 /// - $f(x,\pm0.0,p)=1.0$ for any $x$, even `NaN`
1745 /// - $f(1.0,y,p)=1.0$ for any $y$, even `NaN`
1746 /// - $f(\text{NaN},y,p)=f(x,\text{NaN},p)=\text{NaN}$ otherwise
1747 /// - $f(x,\infty,p)=\infty$ if $|x|>1$, and $0.0$ if $|x|<1$
1748 /// - $f(x,-\infty,p)=0.0$ if $|x|>1$, and $\infty$ if $|x|<1$
1749 /// - $f(-1.0,\pm\infty,p)=1.0$
1750 /// - $f(-1.0,y,p)=1.0$ if $y$ is an even integer, and $-1.0$ if $y$ is an odd integer
1751 /// - $f(\infty,y,p)=\infty$ if $y>0$, and $0.0$ if $y<0$
1752 /// - $f(-\infty,y,p)=-\infty$ if $y$ is a positive odd integer, $\infty$ if $y$ is positive and
1753 /// not an odd integer, $-0.0$ if $y$ is a negative odd integer, and $0.0$ if $y$ is negative
1754 /// and not an odd integer
1755 /// - $f(0.0,y,p)=0.0$ if $y>0$, and $\infty$ if $y<0$
1756 /// - $f(-0.0,y,p)=-0.0$ if $y$ is a positive odd integer, $0.0$ if $y$ is positive and not an
1757 /// odd integer, $-\infty$ if $y$ is a negative odd integer, and $\infty$ if $y$ is negative
1758 /// and not an odd integer
1759 /// - $f(x,y,p)=\text{NaN}$ if $x$ is finite and negative and $y$ is finite and not an integer
1760 ///
1761 /// Overflow and underflow:
1762 /// - If $f(x,y,p)\geq 2^{2^{30}-1}$, $\infty$ is returned instead.
1763 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
1764 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
1765 /// - Negative results (from negative $x$ and odd integer $y$) mirror the bullets above.
1766 ///
1767 /// If you want to use a rounding mode other than `Nearest`, consider using
1768 /// [`Float::pow_prec_round`] instead. If you know that your target precision is the maximum of
1769 /// the precisions of the two inputs, consider using [`Pow::pow`] instead.
1770 ///
1771 /// # Worst-case complexity
1772 /// $T(n, m) = O(n^{3/2} \log n \log\log n + m)$
1773 ///
1774 /// $M(n, m) = O(n \log n + m)$
1775 ///
1776 /// where $T$ is time, $M$ is additional memory, $n$ is `prec`, and $m$ is
1777 /// `max(self.significant_bits(), other.significant_bits())`.
1778 ///
1779 /// # Examples
1780 /// ```
1781 /// use malachite_float::Float;
1782 /// use std::cmp::Ordering::*;
1783 ///
1784 /// let (p, o) = Float::from(3).pow_prec(Float::from(2.5), 5);
1785 /// assert_eq!(p.to_string(), "15.5");
1786 /// assert_eq!(o, Less);
1787 ///
1788 /// let (p, o) = Float::from(3).pow_prec(Float::from(2.5), 20);
1789 /// assert_eq!(p.to_string(), "15.588455");
1790 /// assert_eq!(o, Less);
1791 /// ```
1792 #[inline]
1793 pub fn pow_prec(self, other: Self, prec: u64) -> (Self, Ordering) {
1794 self.pow_prec_ref_ref(&other, prec)
1795 }
1796
1797 /// Raises a [`Float`] to a [`Float`] power, rounding the result to the specified precision and
1798 /// to the nearest value. Both [`Float`]s are taken by reference. An [`Ordering`] is also
1799 /// returned, indicating whether the rounded power is less than, equal to, or greater than the
1800 /// exact power. Although `NaN`s are not comparable to any [`Float`], whenever this function
1801 /// returns a `NaN` it also returns `Equal`.
1802 ///
1803 /// If the power is equidistant from two [`Float`]s with the specified precision, the [`Float`]
1804 /// with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of
1805 /// the `Nearest` rounding mode.
1806 ///
1807 /// $$
1808 /// f(x,y,p) = x^y+\varepsilon.
1809 /// $$
1810 /// - If $x^y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
1811 /// - If $x^y$ is finite and nonzero, then $|\varepsilon| \leq 2^{\lfloor\log_2
1812 /// |x^y|\rfloor-p}$.
1813 ///
1814 /// If the output has a precision, it is `prec`.
1815 ///
1816 /// Special cases:
1817 /// - $f(x,\pm0.0,p)=1.0$ for any $x$, even `NaN`
1818 /// - $f(1.0,y,p)=1.0$ for any $y$, even `NaN`
1819 /// - $f(\text{NaN},y,p)=f(x,\text{NaN},p)=\text{NaN}$ otherwise
1820 /// - $f(x,\infty,p)=\infty$ if $|x|>1$, and $0.0$ if $|x|<1$
1821 /// - $f(x,-\infty,p)=0.0$ if $|x|>1$, and $\infty$ if $|x|<1$
1822 /// - $f(-1.0,\pm\infty,p)=1.0$
1823 /// - $f(-1.0,y,p)=1.0$ if $y$ is an even integer, and $-1.0$ if $y$ is an odd integer
1824 /// - $f(\infty,y,p)=\infty$ if $y>0$, and $0.0$ if $y<0$
1825 /// - $f(-\infty,y,p)=-\infty$ if $y$ is a positive odd integer, $\infty$ if $y$ is positive and
1826 /// not an odd integer, $-0.0$ if $y$ is a negative odd integer, and $0.0$ if $y$ is negative
1827 /// and not an odd integer
1828 /// - $f(0.0,y,p)=0.0$ if $y>0$, and $\infty$ if $y<0$
1829 /// - $f(-0.0,y,p)=-0.0$ if $y$ is a positive odd integer, $0.0$ if $y$ is positive and not an
1830 /// odd integer, $-\infty$ if $y$ is a negative odd integer, and $\infty$ if $y$ is negative
1831 /// and not an odd integer
1832 /// - $f(x,y,p)=\text{NaN}$ if $x$ is finite and negative and $y$ is finite and not an integer
1833 ///
1834 /// Overflow and underflow:
1835 /// - If $f(x,y,p)\geq 2^{2^{30}-1}$, $\infty$ is returned instead.
1836 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
1837 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
1838 /// - Negative results (from negative $x$ and odd integer $y$) mirror the bullets above.
1839 ///
1840 /// If you want to use a rounding mode other than `Nearest`, consider using
1841 /// [`Float::pow_prec_round_ref_ref`] instead. If you know that your target precision is the
1842 /// maximum of the precisions of the two inputs, consider using [`Pow::pow`] instead.
1843 ///
1844 /// # Worst-case complexity
1845 /// $T(n, m) = O(n^{3/2} \log n \log\log n + m)$
1846 ///
1847 /// $M(n, m) = O(n \log n + m)$
1848 ///
1849 /// where $T$ is time, $M$ is additional memory, $n$ is `prec`, and $m$ is
1850 /// `max(self.significant_bits(), other.significant_bits())`.
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)$
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)$
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)$
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)$
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, m) = O(n^{3/2} \log n \log\log n + m)$
2283 ///
2284 /// $M(n, m) = O(n \log n + m)$
2285 ///
2286 /// where $T$ is time, $M$ is additional memory, $n$ is `prec`, and $m$ is
2287 /// `max(self.significant_bits(), other.significant_bits())`.
2288 ///
2289 /// # Examples
2290 /// ```
2291 /// use malachite_float::Float;
2292 /// use std::cmp::Ordering::*;
2293 ///
2294 /// let (p, o) = Float::from(3).pow_prec_val_ref(&Float::from(2.5), 5);
2295 /// assert_eq!(p.to_string(), "15.5");
2296 /// assert_eq!(o, Less);
2297 ///
2298 /// let (p, o) = Float::from(3).pow_prec_val_ref(&Float::from(2.5), 20);
2299 /// assert_eq!(p.to_string(), "15.588455");
2300 /// assert_eq!(o, Less);
2301 /// ```
2302 #[inline]
2303 pub fn pow_prec_val_ref(self, other: &Self, prec: u64) -> (Self, Ordering) {
2304 self.pow_prec_ref_ref(other, prec)
2305 }
2306
2307 #[allow(clippy::needless_pass_by_value)]
2308 /// Raises a [`Float`] to a [`Float`] power, rounding the result to the specified precision and
2309 /// to the nearest value. The first [`Float`] is taken by reference and the second by value. An
2310 /// [`Ordering`] is also returned, indicating whether the rounded power is less than, equal to,
2311 /// or greater than the exact power. Although `NaN`s are not comparable to any [`Float`],
2312 /// whenever this function returns a `NaN` it also returns `Equal`.
2313 ///
2314 /// If the power is equidistant from two [`Float`]s with the specified precision, the [`Float`]
2315 /// with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of
2316 /// the `Nearest` rounding mode.
2317 ///
2318 /// $$
2319 /// f(x,y,p) = x^y+\varepsilon.
2320 /// $$
2321 /// - If $x^y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
2322 /// - If $x^y$ is finite and nonzero, then $|\varepsilon| \leq 2^{\lfloor\log_2
2323 /// |x^y|\rfloor-p}$.
2324 ///
2325 /// If the output has a precision, it is `prec`.
2326 ///
2327 /// Special cases:
2328 /// - $f(x,\pm0.0,p)=1.0$ for any $x$, even `NaN`
2329 /// - $f(1.0,y,p)=1.0$ for any $y$, even `NaN`
2330 /// - $f(\text{NaN},y,p)=f(x,\text{NaN},p)=\text{NaN}$ otherwise
2331 /// - $f(x,\infty,p)=\infty$ if $|x|>1$, and $0.0$ if $|x|<1$
2332 /// - $f(x,-\infty,p)=0.0$ if $|x|>1$, and $\infty$ if $|x|<1$
2333 /// - $f(-1.0,\pm\infty,p)=1.0$
2334 /// - $f(-1.0,y,p)=1.0$ if $y$ is an even integer, and $-1.0$ if $y$ is an odd integer
2335 /// - $f(\infty,y,p)=\infty$ if $y>0$, and $0.0$ if $y<0$
2336 /// - $f(-\infty,y,p)=-\infty$ if $y$ is a positive odd integer, $\infty$ if $y$ is positive and
2337 /// not an odd integer, $-0.0$ if $y$ is a negative odd integer, and $0.0$ if $y$ is negative
2338 /// and not an odd integer
2339 /// - $f(0.0,y,p)=0.0$ if $y>0$, and $\infty$ if $y<0$
2340 /// - $f(-0.0,y,p)=-0.0$ if $y$ is a positive odd integer, $0.0$ if $y$ is positive and not an
2341 /// odd integer, $-\infty$ if $y$ is a negative odd integer, and $\infty$ if $y$ is negative
2342 /// and not an odd integer
2343 /// - $f(x,y,p)=\text{NaN}$ if $x$ is finite and negative and $y$ is finite and not an integer
2344 ///
2345 /// Overflow and underflow:
2346 /// - If $f(x,y,p)\geq 2^{2^{30}-1}$, $\infty$ is returned instead.
2347 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
2348 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
2349 /// - Negative results (from negative $x$ and odd integer $y$) mirror the bullets above.
2350 ///
2351 /// If you want to use a rounding mode other than `Nearest`, consider using
2352 /// [`Float::pow_prec_round_ref_val`] instead. If you know that your target precision is the
2353 /// maximum of the precisions of the two inputs, consider using [`Pow::pow`] instead.
2354 ///
2355 /// # Worst-case complexity
2356 /// $T(n, m) = O(n^{3/2} \log n \log\log n + m)$
2357 ///
2358 /// $M(n, m) = O(n \log n + m)$
2359 ///
2360 /// where $T$ is time, $M$ is additional memory, $n$ is `prec`, and $m$ is
2361 /// `max(self.significant_bits(), other.significant_bits())`.
2362 ///
2363 /// # Examples
2364 /// ```
2365 /// use malachite_float::Float;
2366 /// use std::cmp::Ordering::*;
2367 ///
2368 /// let (p, o) = (&Float::from(3)).pow_prec_ref_val(Float::from(2.5), 5);
2369 /// assert_eq!(p.to_string(), "15.5");
2370 /// assert_eq!(o, Less);
2371 ///
2372 /// let (p, o) = (&Float::from(3)).pow_prec_ref_val(Float::from(2.5), 20);
2373 /// assert_eq!(p.to_string(), "15.588455");
2374 /// assert_eq!(o, Less);
2375 /// ```
2376 #[inline]
2377 pub fn pow_prec_ref_val(&self, other: Self, prec: u64) -> (Self, Ordering) {
2378 self.pow_prec_ref_ref(&other, prec)
2379 }
2380
2381 #[allow(clippy::needless_pass_by_value)]
2382 /// Raises a [`Float`] to a [`Float`] power in place, rounding the result to the specified
2383 /// precision and with the specified rounding mode. The [`Float`] on the right-hand side is
2384 /// taken by value. An [`Ordering`] is returned, indicating whether the rounded power is less
2385 /// than, equal to, or greater than the exact power. Although `NaN`s are not comparable to any
2386 /// [`Float`], whenever this function sets a `NaN` it also returns `Equal`.
2387 ///
2388 /// See [`RoundingMode`] for a description of the possible rounding modes.
2389 ///
2390 /// $$
2391 /// f(x,y,p,m) = x^y+\varepsilon.
2392 /// $$
2393 /// - If $x^y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
2394 /// - If $x^y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
2395 /// 2^{\lfloor\log_2 |x^y|\rfloor-p+1}$.
2396 /// - If $x^y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
2397 /// 2^{\lfloor\log_2 |x^y|\rfloor-p}$.
2398 ///
2399 /// If the output has a precision, it is `prec`.
2400 ///
2401 /// See the [`Float::pow_prec_round`] documentation for information on special cases, overflow,
2402 /// and underflow.
2403 ///
2404 /// If you know you'll be using `Nearest`, consider using [`Float::pow_prec_assign`] instead. If
2405 /// you know that your target precision is the maximum of the precisions of the two inputs,
2406 /// consider using [`Float::pow_round_assign`] instead. If both of these things are true,
2407 /// consider using [`PowAssign::pow_assign`] instead.
2408 ///
2409 /// # Worst-case complexity
2410 /// $T(n, m) = O(n^{3/2} \log n \log\log n + m)$
2411 ///
2412 /// $M(n, m) = O(n \log n + m)$
2413 ///
2414 /// where $T$ is time, $M$ is additional memory, $n$ is `prec`, and $m$ is
2415 /// `max(self.significant_bits(), other.significant_bits())`.
2416 ///
2417 /// # Panics
2418 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the given
2419 /// precision.
2420 ///
2421 /// # Examples
2422 /// ```
2423 /// use malachite_base::rounding_modes::RoundingMode::*;
2424 /// use malachite_float::Float;
2425 /// use std::cmp::Ordering::*;
2426 ///
2427 /// let mut x = Float::from(3);
2428 /// assert_eq!(x.pow_prec_round_assign(Float::from(2.5), 5, Floor), Less);
2429 /// assert_eq!(x.to_string(), "15.5");
2430 ///
2431 /// let mut x = Float::from(3);
2432 /// assert_eq!(
2433 /// x.pow_prec_round_assign(Float::from(2.5), 5, Ceiling),
2434 /// Greater
2435 /// );
2436 /// assert_eq!(x.to_string(), "16.0");
2437 ///
2438 /// let mut x = Float::from(3);
2439 /// assert_eq!(x.pow_prec_round_assign(Float::from(2.5), 5, Nearest), Less);
2440 /// assert_eq!(x.to_string(), "15.5");
2441 ///
2442 /// let mut x = Float::from(3);
2443 /// assert_eq!(x.pow_prec_round_assign(Float::from(2.5), 20, Floor), Less);
2444 /// assert_eq!(x.to_string(), "15.588455");
2445 ///
2446 /// let mut x = Float::from(3);
2447 /// assert_eq!(
2448 /// x.pow_prec_round_assign(Float::from(2.5), 20, Ceiling),
2449 /// Greater
2450 /// );
2451 /// assert_eq!(x.to_string(), "15.588470");
2452 ///
2453 /// let mut x = Float::from(3);
2454 /// assert_eq!(x.pow_prec_round_assign(Float::from(2.5), 20, Nearest), Less);
2455 /// assert_eq!(x.to_string(), "15.588455");
2456 /// ```
2457 pub fn pow_prec_round_assign(&mut self, other: Self, prec: u64, rm: RoundingMode) -> Ordering {
2458 let (result, o) = self.pow_prec_round_ref_ref(&other, prec, rm);
2459 *self = result;
2460 o
2461 }
2462
2463 /// Raises a [`Float`] to a [`Float`] power in place, rounding the result to the specified
2464 /// precision and with the specified rounding mode. The [`Float`] on the right-hand side is
2465 /// taken by reference. An [`Ordering`] is returned, indicating whether the rounded power is
2466 /// less than, equal to, or greater than the exact power. Although `NaN`s are not comparable to
2467 /// any [`Float`], whenever this function sets a `NaN` it also returns `Equal`.
2468 ///
2469 /// See [`RoundingMode`] for a description of the possible rounding modes.
2470 ///
2471 /// $$
2472 /// f(x,y,p,m) = x^y+\varepsilon.
2473 /// $$
2474 /// - If $x^y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
2475 /// - If $x^y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
2476 /// 2^{\lfloor\log_2 |x^y|\rfloor-p+1}$.
2477 /// - If $x^y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
2478 /// 2^{\lfloor\log_2 |x^y|\rfloor-p}$.
2479 ///
2480 /// If the output has a precision, it is `prec`.
2481 ///
2482 /// See the [`Float::pow_prec_round`] documentation for information on special cases, overflow,
2483 /// and underflow.
2484 ///
2485 /// If you know you'll be using `Nearest`, consider using [`Float::pow_prec_assign_ref`]
2486 /// instead. If you know that your target precision is the maximum of the precisions of the two
2487 /// inputs, consider using [`Float::pow_round_assign_ref`] instead. If both of these things are
2488 /// true, consider using [`PowAssign::pow_assign`] instead.
2489 ///
2490 /// # Worst-case complexity
2491 /// $T(n, m) = O(n^{3/2} \log n \log\log n + m)$
2492 ///
2493 /// $M(n, m) = O(n \log n + m)$
2494 ///
2495 /// where $T$ is time, $M$ is additional memory, $n$ is `prec`, and $m$ is
2496 /// `max(self.significant_bits(), other.significant_bits())`.
2497 ///
2498 /// # Panics
2499 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the given
2500 /// precision.
2501 ///
2502 /// # Examples
2503 /// ```
2504 /// use malachite_base::rounding_modes::RoundingMode::*;
2505 /// use malachite_float::Float;
2506 /// use std::cmp::Ordering::*;
2507 ///
2508 /// let mut x = Float::from(3);
2509 /// assert_eq!(
2510 /// x.pow_prec_round_assign_ref(&Float::from(2.5), 5, Floor),
2511 /// Less
2512 /// );
2513 /// assert_eq!(x.to_string(), "15.5");
2514 ///
2515 /// let mut x = Float::from(3);
2516 /// assert_eq!(
2517 /// x.pow_prec_round_assign_ref(&Float::from(2.5), 5, Ceiling),
2518 /// Greater
2519 /// );
2520 /// assert_eq!(x.to_string(), "16.0");
2521 ///
2522 /// let mut x = Float::from(3);
2523 /// assert_eq!(
2524 /// x.pow_prec_round_assign_ref(&Float::from(2.5), 5, Nearest),
2525 /// Less
2526 /// );
2527 /// assert_eq!(x.to_string(), "15.5");
2528 ///
2529 /// let mut x = Float::from(3);
2530 /// assert_eq!(
2531 /// x.pow_prec_round_assign_ref(&Float::from(2.5), 20, Floor),
2532 /// Less
2533 /// );
2534 /// assert_eq!(x.to_string(), "15.588455");
2535 ///
2536 /// let mut x = Float::from(3);
2537 /// assert_eq!(
2538 /// x.pow_prec_round_assign_ref(&Float::from(2.5), 20, Ceiling),
2539 /// Greater
2540 /// );
2541 /// assert_eq!(x.to_string(), "15.588470");
2542 ///
2543 /// let mut x = Float::from(3);
2544 /// assert_eq!(
2545 /// x.pow_prec_round_assign_ref(&Float::from(2.5), 20, Nearest),
2546 /// Less
2547 /// );
2548 /// assert_eq!(x.to_string(), "15.588455");
2549 /// ```
2550 pub fn pow_prec_round_assign_ref(
2551 &mut self,
2552 other: &Self,
2553 prec: u64,
2554 rm: RoundingMode,
2555 ) -> Ordering {
2556 let (result, o) = self.pow_prec_round_ref_ref(other, prec, rm);
2557 *self = result;
2558 o
2559 }
2560
2561 /// Raises a [`Float`] to a [`Float`] power in place, rounding the result to the specified
2562 /// precision and to the nearest value. The [`Float`] on the right-hand side is taken by value.
2563 /// An [`Ordering`] is returned, indicating whether the rounded power is less than, equal to, or
2564 /// greater than the exact power. Although `NaN`s are not comparable to any [`Float`], whenever
2565 /// this function sets a `NaN` it also returns `Equal`.
2566 ///
2567 /// If the power is equidistant from two [`Float`]s with the specified precision, the [`Float`]
2568 /// with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of
2569 /// the `Nearest` rounding mode.
2570 ///
2571 /// $$
2572 /// f(x,y,p) = x^y+\varepsilon.
2573 /// $$
2574 /// - If $x^y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
2575 /// - If $x^y$ is finite and nonzero, then $|\varepsilon| \leq 2^{\lfloor\log_2
2576 /// |x^y|\rfloor-p}$.
2577 ///
2578 /// If the output has a precision, it is `prec`.
2579 ///
2580 /// See the [`Float::pow_prec_round`] documentation for information on special cases, overflow,
2581 /// and underflow.
2582 ///
2583 /// If you want to use a rounding mode other than `Nearest`, consider using
2584 /// [`Float::pow_prec_round_assign`] instead. If you know that your target precision is the
2585 /// maximum of the precisions of the two inputs, consider using [`PowAssign::pow_assign`]
2586 /// instead.
2587 ///
2588 /// # Worst-case complexity
2589 /// $T(n, m) = O(n^{3/2} \log n \log\log n + m)$
2590 ///
2591 /// $M(n, m) = O(n \log n + m)$
2592 ///
2593 /// where $T$ is time, $M$ is additional memory, $n$ is `prec`, and $m$ is
2594 /// `max(self.significant_bits(), other.significant_bits())`.
2595 ///
2596 /// # Examples
2597 /// ```
2598 /// use malachite_float::Float;
2599 /// use std::cmp::Ordering::*;
2600 ///
2601 /// let mut x = Float::from(3);
2602 /// assert_eq!(x.pow_prec_assign(Float::from(2.5), 5), Less);
2603 /// assert_eq!(x.to_string(), "15.5");
2604 ///
2605 /// let mut x = Float::from(3);
2606 /// assert_eq!(x.pow_prec_assign(Float::from(2.5), 20), Less);
2607 /// assert_eq!(x.to_string(), "15.588455");
2608 /// ```
2609 #[inline]
2610 pub fn pow_prec_assign(&mut self, other: Self, prec: u64) -> Ordering {
2611 self.pow_prec_round_assign(other, prec, Nearest)
2612 }
2613
2614 /// Raises a [`Float`] to a [`Float`] power in place, rounding the result to the specified
2615 /// precision and to the nearest value. The [`Float`] on the right-hand side is taken by
2616 /// reference. An [`Ordering`] is returned, indicating whether the rounded power is less than,
2617 /// equal to, or greater than the exact power. Although `NaN`s are not comparable to any
2618 /// [`Float`], whenever this function sets a `NaN` it also returns `Equal`.
2619 ///
2620 /// If the power is equidistant from two [`Float`]s with the specified precision, the [`Float`]
2621 /// with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of
2622 /// the `Nearest` rounding mode.
2623 ///
2624 /// $$
2625 /// f(x,y,p) = x^y+\varepsilon.
2626 /// $$
2627 /// - If $x^y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
2628 /// - If $x^y$ is finite and nonzero, then $|\varepsilon| \leq 2^{\lfloor\log_2
2629 /// |x^y|\rfloor-p}$.
2630 ///
2631 /// If the output has a precision, it is `prec`.
2632 ///
2633 /// See the [`Float::pow_prec_round`] documentation for information on special cases, overflow,
2634 /// and underflow.
2635 ///
2636 /// If you want to use a rounding mode other than `Nearest`, consider using
2637 /// [`Float::pow_prec_round_assign_ref`] instead. If you know that your target precision is the
2638 /// maximum of the precisions of the two inputs, consider using [`PowAssign::pow_assign`]
2639 /// instead.
2640 ///
2641 /// # Worst-case complexity
2642 /// $T(n, m) = O(n^{3/2} \log n \log\log n + m)$
2643 ///
2644 /// $M(n, m) = O(n \log n + m)$
2645 ///
2646 /// where $T$ is time, $M$ is additional memory, $n$ is `prec`, and $m$ is
2647 /// `max(self.significant_bits(), other.significant_bits())`.
2648 ///
2649 /// # Examples
2650 /// ```
2651 /// use malachite_float::Float;
2652 /// use std::cmp::Ordering::*;
2653 ///
2654 /// let mut x = Float::from(3);
2655 /// assert_eq!(x.pow_prec_assign_ref(&Float::from(2.5), 5), Less);
2656 /// assert_eq!(x.to_string(), "15.5");
2657 ///
2658 /// let mut x = Float::from(3);
2659 /// assert_eq!(x.pow_prec_assign_ref(&Float::from(2.5), 20), Less);
2660 /// assert_eq!(x.to_string(), "15.588455");
2661 /// ```
2662 #[inline]
2663 pub fn pow_prec_assign_ref(&mut self, other: &Self, prec: u64) -> Ordering {
2664 self.pow_prec_round_assign_ref(other, prec, Nearest)
2665 }
2666
2667 /// Raises a [`Float`] to a [`Float`] power in place, rounding the result to the maximum of the
2668 /// precisions of the two inputs and with the specified rounding mode. The [`Float`] on the
2669 /// right-hand side is taken by value. An [`Ordering`] is returned, indicating whether the
2670 /// rounded power is less than, equal to, or greater than the exact power. Although `NaN`s are
2671 /// not comparable to any [`Float`], whenever this function sets a `NaN` it also returns
2672 /// `Equal`.
2673 ///
2674 /// See [`RoundingMode`] for a description of the possible rounding modes.
2675 ///
2676 /// $$
2677 /// f(x,y,p,m) = x^y+\varepsilon.
2678 /// $$
2679 /// - If $x^y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
2680 /// - If $x^y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
2681 /// 2^{\lfloor\log_2 |x^y|\rfloor-p+1}$.
2682 /// - If $x^y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
2683 /// 2^{\lfloor\log_2 |x^y|\rfloor-p}$.
2684 ///
2685 /// If the output has a precision, it is the maximum of the precisions of the inputs.
2686 ///
2687 /// See the [`Float::pow_prec_round`] documentation for information on special cases, overflow,
2688 /// and underflow.
2689 ///
2690 /// If you want to specify an output precision, consider using [`Float::pow_prec_round_assign`]
2691 /// instead. If you know you'll be using the `Nearest` rounding mode, consider using
2692 /// [`PowAssign::pow_assign`] instead.
2693 ///
2694 /// # Worst-case complexity
2695 /// $T(n) = O(n^{3/2} \log n \log\log n)$
2696 ///
2697 /// $M(n) = O(n \log n)$
2698 ///
2699 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
2700 /// other.significant_bits())`.
2701 ///
2702 /// # Panics
2703 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the given
2704 /// precision.
2705 ///
2706 /// # Examples
2707 /// ```
2708 /// use malachite_base::rounding_modes::RoundingMode::*;
2709 /// use malachite_float::Float;
2710 /// use std::cmp::Ordering::*;
2711 ///
2712 /// let mut x = Float::from(3);
2713 /// assert_eq!(x.pow_round_assign(Float::from(2.5), Floor), Less);
2714 /// assert_eq!(x.to_string(), "14.0");
2715 ///
2716 /// let mut x = Float::from(3);
2717 /// assert_eq!(x.pow_round_assign(Float::from(2.5), Ceiling), Greater);
2718 /// assert_eq!(x.to_string(), "16.0");
2719 ///
2720 /// let mut x = Float::from(3);
2721 /// assert_eq!(x.pow_round_assign(Float::from(2.5), Nearest), Greater);
2722 /// assert_eq!(x.to_string(), "16.0");
2723 /// ```
2724 pub fn pow_round_assign(&mut self, other: Self, rm: RoundingMode) -> Ordering {
2725 let prec = self.significant_bits().max(other.significant_bits());
2726 self.pow_prec_round_assign(other, prec, rm)
2727 }
2728
2729 /// Raises a [`Float`] to a [`Float`] power in place, rounding the result to the maximum of the
2730 /// precisions of the two inputs and with the specified rounding mode. The [`Float`] on the
2731 /// right-hand side is taken by reference. An [`Ordering`] is returned, indicating whether the
2732 /// rounded power is less than, equal to, or greater than the exact power. Although `NaN`s are
2733 /// not comparable to any [`Float`], whenever this function sets a `NaN` it also returns
2734 /// `Equal`.
2735 ///
2736 /// See [`RoundingMode`] for a description of the possible rounding modes.
2737 ///
2738 /// $$
2739 /// f(x,y,p,m) = x^y+\varepsilon.
2740 /// $$
2741 /// - If $x^y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
2742 /// - If $x^y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
2743 /// 2^{\lfloor\log_2 |x^y|\rfloor-p+1}$.
2744 /// - If $x^y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
2745 /// 2^{\lfloor\log_2 |x^y|\rfloor-p}$.
2746 ///
2747 /// If the output has a precision, it is the maximum of the precisions of the inputs.
2748 ///
2749 /// See the [`Float::pow_prec_round`] documentation for information on special cases, overflow,
2750 /// and underflow.
2751 ///
2752 /// If you want to specify an output precision, consider using
2753 /// [`Float::pow_prec_round_assign_ref`] instead. If you know you'll be using the `Nearest`
2754 /// rounding mode, consider using [`PowAssign::pow_assign`] instead.
2755 ///
2756 /// # Worst-case complexity
2757 /// $T(n) = O(n^{3/2} \log n \log\log n)$
2758 ///
2759 /// $M(n) = O(n \log n)$
2760 ///
2761 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
2762 /// other.significant_bits())`.
2763 ///
2764 /// # Panics
2765 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the given
2766 /// precision.
2767 ///
2768 /// # Examples
2769 /// ```
2770 /// use malachite_base::rounding_modes::RoundingMode::*;
2771 /// use malachite_float::Float;
2772 /// use std::cmp::Ordering::*;
2773 ///
2774 /// let mut x = Float::from(3);
2775 /// assert_eq!(x.pow_round_assign_ref(&Float::from(2.5), Floor), Less);
2776 /// assert_eq!(x.to_string(), "14.0");
2777 ///
2778 /// let mut x = Float::from(3);
2779 /// assert_eq!(x.pow_round_assign_ref(&Float::from(2.5), Ceiling), Greater);
2780 /// assert_eq!(x.to_string(), "16.0");
2781 ///
2782 /// let mut x = Float::from(3);
2783 /// assert_eq!(x.pow_round_assign_ref(&Float::from(2.5), Nearest), Greater);
2784 /// assert_eq!(x.to_string(), "16.0");
2785 /// ```
2786 pub fn pow_round_assign_ref(&mut self, other: &Self, rm: RoundingMode) -> Ordering {
2787 let prec = self.significant_bits().max(other.significant_bits());
2788 self.pow_prec_round_assign_ref(other, prec, rm)
2789 }
2790}
2791
2792impl Pow<Self> for Float {
2793 type Output = Self;
2794
2795 /// Raises a [`Float`] to a [`Float`] power, rounding the result to the nearest value. Both
2796 /// [`Float`]s are taken by value.
2797 ///
2798 /// If the output has a precision, it is the maximum of the precisions of the inputs. If the
2799 /// power is equidistant from two [`Float`]s with the specified precision, the [`Float`] with
2800 /// fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of the
2801 /// `Nearest` rounding mode.
2802 ///
2803 /// $$
2804 /// f(x,y) = x^y+\varepsilon.
2805 /// $$
2806 /// - If $x^y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
2807 /// - If $x^y$ is finite and nonzero, then $|\varepsilon| \leq 2^{\lfloor\log_2
2808 /// |x^y|\rfloor-p}$, where $p$ is the maximum precision of the inputs.
2809 ///
2810 /// See the [`Float::pow_prec_round`] documentation for information on special cases, overflow,
2811 /// and underflow.
2812 ///
2813 /// If you want to specify an output precision, consider using [`Float::pow_prec`] instead. If
2814 /// you want both of these things, consider using [`Float::pow_prec_round`] instead.
2815 ///
2816 /// # Worst-case complexity
2817 /// $T(n) = O(n^{3/2} \log n \log\log n)$
2818 ///
2819 /// $M(n) = O(n \log n)$
2820 ///
2821 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
2822 /// other.significant_bits())`.
2823 ///
2824 /// # Examples
2825 /// ```
2826 /// use malachite_base::num::arithmetic::traits::Pow;
2827 /// use malachite_float::Float;
2828 ///
2829 /// assert_eq!(Float::from(3).pow(Float::from(2.5)).to_string(), "16.0");
2830 /// assert_eq!(Float::from(10).pow(Float::from(-0.5)).to_string(), "0.31");
2831 /// ```
2832 fn pow(self, other: Self) -> Self {
2833 let prec = self.significant_bits().max(other.significant_bits());
2834 self.pow_prec_ref_ref(&other, prec).0
2835 }
2836}
2837
2838impl Pow<&Self> for Float {
2839 type Output = Self;
2840
2841 /// Raises a [`Float`] to a [`Float`] power, rounding the result to the nearest value. The first
2842 /// [`Float`] is taken by value and the second by reference.
2843 ///
2844 /// If the output has a precision, it is the maximum of the precisions of the inputs. If the
2845 /// power is equidistant from two [`Float`]s with the specified precision, the [`Float`] with
2846 /// fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of the
2847 /// `Nearest` rounding mode.
2848 ///
2849 /// $$
2850 /// f(x,y) = x^y+\varepsilon.
2851 /// $$
2852 /// - If $x^y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
2853 /// - If $x^y$ is finite and nonzero, then $|\varepsilon| \leq 2^{\lfloor\log_2
2854 /// |x^y|\rfloor-p}$, where $p$ is the maximum precision of the inputs.
2855 ///
2856 /// See the [`Float::pow_prec_round`] documentation for information on special cases, overflow,
2857 /// and underflow.
2858 ///
2859 /// If you want to specify an output precision, consider using [`Float::pow_prec`] instead. If
2860 /// you want both of these things, consider using [`Float::pow_prec_round`] instead.
2861 ///
2862 /// # Worst-case complexity
2863 /// $T(n) = O(n^{3/2} \log n \log\log n)$
2864 ///
2865 /// $M(n) = O(n \log n)$
2866 ///
2867 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
2868 /// other.significant_bits())`.
2869 ///
2870 /// # Examples
2871 /// ```
2872 /// use malachite_base::num::arithmetic::traits::Pow;
2873 /// use malachite_float::Float;
2874 ///
2875 /// assert_eq!(Float::from(3).pow(&Float::from(2.5)).to_string(), "16.0");
2876 /// assert_eq!(Float::from(10).pow(&Float::from(-0.5)).to_string(), "0.31");
2877 /// ```
2878 fn pow(self, other: &Self) -> Self {
2879 let prec = self.significant_bits().max(other.significant_bits());
2880 self.pow_prec_ref_ref(other, prec).0
2881 }
2882}
2883
2884impl Pow<Float> for &Float {
2885 type Output = Float;
2886
2887 /// Raises a [`Float`] to a [`Float`] power, rounding the result to the nearest value. The first
2888 /// [`Float`] is taken by reference and the second by value.
2889 ///
2890 /// If the output has a precision, it is the maximum of the precisions of the inputs. If the
2891 /// power is equidistant from two [`Float`]s with the specified precision, the [`Float`] with
2892 /// fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of the
2893 /// `Nearest` rounding mode.
2894 ///
2895 /// $$
2896 /// f(x,y) = x^y+\varepsilon.
2897 /// $$
2898 /// - If $x^y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
2899 /// - If $x^y$ is finite and nonzero, then $|\varepsilon| \leq 2^{\lfloor\log_2
2900 /// |x^y|\rfloor-p}$, where $p$ is the maximum precision of the inputs.
2901 ///
2902 /// See the [`Float::pow_prec_round`] documentation for information on special cases, overflow,
2903 /// and underflow.
2904 ///
2905 /// If you want to specify an output precision, consider using [`Float::pow_prec`] instead. If
2906 /// you want both of these things, consider using [`Float::pow_prec_round`] instead.
2907 ///
2908 /// # Worst-case complexity
2909 /// $T(n) = O(n^{3/2} \log n \log\log n)$
2910 ///
2911 /// $M(n) = O(n \log n)$
2912 ///
2913 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
2914 /// other.significant_bits())`.
2915 ///
2916 /// # Examples
2917 /// ```
2918 /// use malachite_base::num::arithmetic::traits::Pow;
2919 /// use malachite_float::Float;
2920 ///
2921 /// assert_eq!((&Float::from(3)).pow(Float::from(2.5)).to_string(), "16.0");
2922 /// assert_eq!(
2923 /// (&Float::from(10)).pow(Float::from(-0.5)).to_string(),
2924 /// "0.31"
2925 /// );
2926 /// ```
2927 fn pow(self, other: Float) -> Float {
2928 let prec = self.significant_bits().max(other.significant_bits());
2929 self.pow_prec_ref_ref(&other, prec).0
2930 }
2931}
2932
2933impl Pow<&Float> for &Float {
2934 type Output = Float;
2935
2936 /// Raises a [`Float`] to a [`Float`] power, rounding the result to the nearest value. Both
2937 /// [`Float`]s are taken by reference.
2938 ///
2939 /// If the output has a precision, it is the maximum of the precisions of the inputs. If the
2940 /// power is equidistant from two [`Float`]s with the specified precision, the [`Float`] with
2941 /// fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of the
2942 /// `Nearest` rounding mode.
2943 ///
2944 /// $$
2945 /// f(x,y) = x^y+\varepsilon.
2946 /// $$
2947 /// - If $x^y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
2948 /// - If $x^y$ is finite and nonzero, then $|\varepsilon| \leq 2^{\lfloor\log_2
2949 /// |x^y|\rfloor-p}$, where $p$ is the maximum precision of the inputs.
2950 ///
2951 /// See the [`Float::pow_prec_round`] documentation for information on special cases, overflow,
2952 /// and underflow.
2953 ///
2954 /// If you want to specify an output precision, consider using [`Float::pow_prec`] instead. If
2955 /// you want both of these things, consider using [`Float::pow_prec_round`] instead.
2956 ///
2957 /// # Worst-case complexity
2958 /// $T(n) = O(n^{3/2} \log n \log\log n)$
2959 ///
2960 /// $M(n) = O(n \log n)$
2961 ///
2962 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
2963 /// other.significant_bits())`.
2964 ///
2965 /// # Examples
2966 /// ```
2967 /// use malachite_base::num::arithmetic::traits::Pow;
2968 /// use malachite_float::Float;
2969 ///
2970 /// assert_eq!((&Float::from(3)).pow(&Float::from(2.5)).to_string(), "16.0");
2971 /// assert_eq!(
2972 /// (&Float::from(10)).pow(&Float::from(-0.5)).to_string(),
2973 /// "0.31"
2974 /// );
2975 /// ```
2976 fn pow(self, other: &Float) -> Float {
2977 let prec = self.significant_bits().max(other.significant_bits());
2978 self.pow_prec_ref_ref(other, prec).0
2979 }
2980}
2981
2982impl PowAssign<Self> for Float {
2983 /// Raises a [`Float`] to a [`Float`] power in place, rounding the result to the nearest value.
2984 /// The [`Float`] on the right-hand side is taken by value.
2985 ///
2986 /// If the output has a precision, it is the maximum of the precisions of the inputs. If the
2987 /// power is equidistant from two [`Float`]s with the specified precision, the [`Float`] with
2988 /// fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of the
2989 /// `Nearest` rounding mode.
2990 ///
2991 /// $$
2992 /// f(x,y) = x^y+\varepsilon.
2993 /// $$
2994 /// - If $x^y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
2995 /// - If $x^y$ is finite and nonzero, then $|\varepsilon| \leq 2^{\lfloor\log_2
2996 /// |x^y|\rfloor-p}$, where $p$ is the maximum precision of the inputs.
2997 ///
2998 /// See the [`Float::pow_prec_round`] documentation for information on special cases, overflow,
2999 /// and underflow.
3000 ///
3001 /// If you want to specify an output precision, consider using [`Float::pow_prec`] instead. If
3002 /// you want both of these things, consider using [`Float::pow_prec_round`] instead.
3003 ///
3004 /// # Worst-case complexity
3005 /// $T(n) = O(n^{3/2} \log n \log\log n)$
3006 ///
3007 /// $M(n) = O(n \log n)$
3008 ///
3009 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
3010 /// other.significant_bits())`.
3011 ///
3012 /// # Examples
3013 /// ```
3014 /// use malachite_base::num::arithmetic::traits::PowAssign;
3015 /// use malachite_float::Float;
3016 ///
3017 /// let mut x = Float::from(3);
3018 /// x.pow_assign(Float::from(2.5));
3019 /// assert_eq!(x.to_string(), "16.0");
3020 /// ```
3021 fn pow_assign(&mut self, other: Self) {
3022 let prec = self.significant_bits().max(other.significant_bits());
3023 *self = self.pow_prec_ref_ref(&other, prec).0;
3024 }
3025}
3026
3027impl PowAssign<&Self> for Float {
3028 /// Raises a [`Float`] to a [`Float`] power in place, rounding the result to the nearest value.
3029 /// The [`Float`] on the right-hand side is taken by reference.
3030 ///
3031 /// If the output has a precision, it is the maximum of the precisions of the inputs. If the
3032 /// power is equidistant from two [`Float`]s with the specified precision, the [`Float`] with
3033 /// fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of the
3034 /// `Nearest` rounding mode.
3035 ///
3036 /// $$
3037 /// f(x,y) = x^y+\varepsilon.
3038 /// $$
3039 /// - If $x^y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
3040 /// - If $x^y$ is finite and nonzero, then $|\varepsilon| \leq 2^{\lfloor\log_2
3041 /// |x^y|\rfloor-p}$, where $p$ is the maximum precision of the inputs.
3042 ///
3043 /// See the [`Float::pow_prec_round`] documentation for information on special cases, overflow,
3044 /// and underflow.
3045 ///
3046 /// If you want to specify an output precision, consider using [`Float::pow_prec`] instead. If
3047 /// you want both of these things, consider using [`Float::pow_prec_round`] instead.
3048 ///
3049 /// # Worst-case complexity
3050 /// $T(n) = O(n^{3/2} \log n \log\log n)$
3051 ///
3052 /// $M(n) = O(n \log n)$
3053 ///
3054 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
3055 /// other.significant_bits())`.
3056 ///
3057 /// # Examples
3058 /// ```
3059 /// use malachite_base::num::arithmetic::traits::PowAssign;
3060 /// use malachite_float::Float;
3061 ///
3062 /// let mut x = Float::from(3);
3063 /// x.pow_assign(&Float::from(2.5));
3064 /// assert_eq!(x.to_string(), "16.0");
3065 /// ```
3066 fn pow_assign(&mut self, other: &Self) {
3067 let prec = self.significant_bits().max(other.significant_bits());
3068 *self = self.pow_prec_ref_ref(other, prec).0;
3069 }
3070}
3071
3072// Represents an `Integer` exactly as a `Float`, at just enough precision. Routes a `Float ^
3073// Integer` power through the `Float ^ Float` power, which dispatches to `pow_integer`.
3074fn integer_to_exact_float(z: Integer) -> Float {
3075 let prec = z.significant_bits().max(1);
3076 Float::from_integer_prec_round(z, prec, Exact).0
3077}
3078
3079impl Float {
3080 /// Raises a [`Float`] to the power of an [`Integer`], rounding the result to the specified
3081 /// precision and with the specified rounding mode. Both are taken by value. An [`Ordering`] is
3082 /// also returned, indicating whether the rounded power is less than, equal to, or greater than
3083 /// the exact power. Although `NaN`s are not comparable to any [`Float`], whenever this function
3084 /// returns a `NaN` it also returns `Equal`.
3085 ///
3086 /// See [`RoundingMode`] for a description of the possible rounding modes.
3087 ///
3088 /// $$
3089 /// f(x,n,p,m) = x^n+\varepsilon.
3090 /// $$
3091 /// - If $x^n$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
3092 /// - If $x^n$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
3093 /// 2^{\lfloor\log_2 |x^n|\rfloor-p+1}$.
3094 /// - If $x^n$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
3095 /// 2^{\lfloor\log_2 |x^n|\rfloor-p}$.
3096 ///
3097 /// See the [`Float::pow_integer_prec_round_ref_ref`] documentation for information on special
3098 /// cases, overflow, and underflow.
3099 ///
3100 /// # Worst-case complexity
3101 /// $T(n) = O(n^{3/2} \log n \log\log n)$
3102 ///
3103 /// $M(n) = O(n \log n)$
3104 ///
3105 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
3106 /// other.significant_bits())`.
3107 ///
3108 /// # Panics
3109 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the given
3110 /// precision.
3111 ///
3112 /// # Examples
3113 /// ```
3114 /// use malachite_base::rounding_modes::RoundingMode::*;
3115 /// use malachite_float::Float;
3116 /// use malachite_nz::integer::Integer;
3117 /// use std::cmp::Ordering::*;
3118 ///
3119 /// let (p, o) = Float::from(3).pow_integer_prec_round(Integer::from(5), 20, Floor);
3120 /// assert_eq!(p.to_string(), "243.00000");
3121 /// assert_eq!(o, Equal);
3122 ///
3123 /// let (p, o) = Float::from(3).pow_integer_prec_round(Integer::from(-2), 10, Ceiling);
3124 /// assert_eq!(p.to_string(), "0.11121");
3125 /// assert_eq!(o, Greater);
3126 /// ```
3127 #[inline]
3128 pub fn pow_integer_prec_round(
3129 self,
3130 other: Integer,
3131 prec: u64,
3132 rm: RoundingMode,
3133 ) -> (Self, Ordering) {
3134 self.pow_prec_round(integer_to_exact_float(other), prec, rm)
3135 }
3136
3137 /// Raises a [`Float`] to the power of an [`Integer`], rounding the result to the specified
3138 /// precision and with the specified rounding mode. The [`Float`] is taken by value and the
3139 /// [`Integer`] by reference. An [`Ordering`] is also returned, indicating whether the rounded
3140 /// power is less than, equal to, or greater than the exact power. Although `NaN`s are not
3141 /// comparable to any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
3142 ///
3143 /// See [`RoundingMode`] for a description of the possible rounding modes.
3144 ///
3145 /// $$
3146 /// f(x,n,p,m) = x^n+\varepsilon.
3147 /// $$
3148 /// - If $x^n$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
3149 /// - If $x^n$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
3150 /// 2^{\lfloor\log_2 |x^n|\rfloor-p+1}$.
3151 /// - If $x^n$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
3152 /// 2^{\lfloor\log_2 |x^n|\rfloor-p}$.
3153 ///
3154 /// See the [`Float::pow_integer_prec_round_ref_ref`] documentation for information on special
3155 /// cases, overflow, and underflow.
3156 ///
3157 /// # Worst-case complexity
3158 /// $T(n) = O(n^{3/2} \log n \log\log n)$
3159 ///
3160 /// $M(n) = O(n \log n)$
3161 ///
3162 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
3163 /// other.significant_bits())`.
3164 ///
3165 /// # Panics
3166 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the given
3167 /// precision.
3168 ///
3169 /// # Examples
3170 /// ```
3171 /// use malachite_base::rounding_modes::RoundingMode::*;
3172 /// use malachite_float::Float;
3173 /// use malachite_nz::integer::Integer;
3174 /// use std::cmp::Ordering::*;
3175 ///
3176 /// let (p, o) = Float::from(3).pow_integer_prec_round_val_ref(&Integer::from(5), 20, Floor);
3177 /// assert_eq!(p.to_string(), "243.00000");
3178 /// assert_eq!(o, Equal);
3179 ///
3180 /// let (p, o) = Float::from(3).pow_integer_prec_round_val_ref(&Integer::from(-2), 10, Ceiling);
3181 /// assert_eq!(p.to_string(), "0.11121");
3182 /// assert_eq!(o, Greater);
3183 /// ```
3184 #[inline]
3185 pub fn pow_integer_prec_round_val_ref(
3186 self,
3187 other: &Integer,
3188 prec: u64,
3189 rm: RoundingMode,
3190 ) -> (Self, Ordering) {
3191 self.pow_prec_round(integer_to_exact_float(other.clone()), prec, rm)
3192 }
3193
3194 /// Raises a [`Float`] to the power of an [`Integer`], rounding the result to the specified
3195 /// precision and with the specified rounding mode. The [`Float`] is taken by reference and the
3196 /// [`Integer`] by value. An [`Ordering`] is also returned, indicating whether the rounded power
3197 /// is less than, equal to, or greater than the exact power. Although `NaN`s are not comparable
3198 /// to any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
3199 ///
3200 /// See [`RoundingMode`] for a description of the possible rounding modes.
3201 ///
3202 /// $$
3203 /// f(x,n,p,m) = x^n+\varepsilon.
3204 /// $$
3205 /// - If $x^n$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
3206 /// - If $x^n$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
3207 /// 2^{\lfloor\log_2 |x^n|\rfloor-p+1}$.
3208 /// - If $x^n$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
3209 /// 2^{\lfloor\log_2 |x^n|\rfloor-p}$.
3210 ///
3211 /// See the [`Float::pow_integer_prec_round_ref_ref`] documentation for information on special
3212 /// cases, overflow, and underflow.
3213 ///
3214 /// # Worst-case complexity
3215 /// $T(n) = O(n^{3/2} \log n \log\log n)$
3216 ///
3217 /// $M(n) = O(n \log n)$
3218 ///
3219 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
3220 /// other.significant_bits())`.
3221 ///
3222 /// # Panics
3223 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the given
3224 /// precision.
3225 ///
3226 /// # Examples
3227 /// ```
3228 /// use malachite_base::rounding_modes::RoundingMode::*;
3229 /// use malachite_float::Float;
3230 /// use malachite_nz::integer::Integer;
3231 /// use std::cmp::Ordering::*;
3232 ///
3233 /// let (p, o) = (&Float::from(3)).pow_integer_prec_round_ref_val(Integer::from(5), 20, Floor);
3234 /// assert_eq!(p.to_string(), "243.00000");
3235 /// assert_eq!(o, Equal);
3236 ///
3237 /// let x = Float::from(3);
3238 /// let (p, o) = (&x).pow_integer_prec_round_ref_val(Integer::from(-2), 10, Ceiling);
3239 /// assert_eq!(p.to_string(), "0.11121");
3240 /// assert_eq!(o, Greater);
3241 /// ```
3242 #[inline]
3243 pub fn pow_integer_prec_round_ref_val(
3244 &self,
3245 other: Integer,
3246 prec: u64,
3247 rm: RoundingMode,
3248 ) -> (Self, Ordering) {
3249 self.pow_prec_round_ref_val(integer_to_exact_float(other), prec, rm)
3250 }
3251
3252 /// Raises a [`Float`] to the power of an [`Integer`], rounding the result to the specified
3253 /// precision and with the specified rounding mode. Both are taken by reference. An [`Ordering`]
3254 /// is also returned, indicating whether the rounded power is less than, equal to, or greater
3255 /// than the exact power. Although `NaN`s are not comparable to any [`Float`], whenever this
3256 /// function returns a `NaN` it also returns `Equal`.
3257 ///
3258 /// See [`RoundingMode`] for a description of the possible rounding modes.
3259 ///
3260 /// $$
3261 /// f(x,n,p,m) = x^n+\varepsilon.
3262 /// $$
3263 /// - If $x^n$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
3264 /// - If $x^n$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
3265 /// 2^{\lfloor\log_2 |x^n|\rfloor-p+1}$.
3266 /// - If $x^n$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
3267 /// 2^{\lfloor\log_2 |x^n|\rfloor-p}$.
3268 ///
3269 /// Special cases:
3270 /// - $f(x,0)=1.0$ for any $x$, even `NaN`
3271 /// - $f(1.0,n)=1.0$
3272 /// - $f(\text{NaN},n)=\text{NaN}$ if $n \neq 0$
3273 /// - $f(-1.0,n)=1.0$ if $n$ is even, and $-1.0$ if $n$ is odd
3274 /// - $f(\infty,n)=\infty$ if $n>0$, and $0.0$ if $n<0$
3275 /// - $f(-\infty,n)=-\infty$ if $n$ is positive and odd, $\infty$ if $n$ is positive and even,
3276 /// $-0.0$ if $n$ is negative and odd, and $0.0$ if $n$ is negative and even
3277 /// - $f(0.0,n)=0.0$ if $n>0$, and $\infty$ if $n<0$
3278 /// - $f(-0.0,n)=-0.0$ if $n$ is positive and odd, $0.0$ if $n$ is positive and even, $-\infty$
3279 /// if $n$ is negative and odd, and $\infty$ if $n$ is negative and even
3280 ///
3281 /// Overflow and underflow:
3282 /// - If $f(x,n,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
3283 /// returned instead.
3284 /// - 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}$
3285 /// is returned instead.
3286 /// - If $0<f(x,n,p,m)<2^{-2^{30}}$ and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
3287 /// - If $0<f(x,n,p,m)<2^{-2^{30}}$ and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
3288 /// instead.
3289 /// - If $0<f(x,n,p,m)\leq2^{-2^{30}-1}$ and $m$ is `Nearest`, $0.0$ is returned instead.
3290 /// - If $2^{-2^{30}-1}<f(x,n,p,m)<2^{-2^{30}}$ and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
3291 /// instead.
3292 /// - Negative results (from negative $x$ and odd $n$) mirror the bullets above, with the
3293 /// rounding directions reflected.
3294 ///
3295 /// # Worst-case complexity
3296 /// $T(n) = O(n^{3/2} \log n \log\log n)$
3297 ///
3298 /// $M(n) = O(n \log n)$
3299 ///
3300 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
3301 /// other.significant_bits())`.
3302 ///
3303 /// # Panics
3304 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the given
3305 /// precision.
3306 ///
3307 /// # Examples
3308 /// ```
3309 /// use malachite_base::rounding_modes::RoundingMode::*;
3310 /// use malachite_float::Float;
3311 /// use malachite_nz::integer::Integer;
3312 /// use std::cmp::Ordering::*;
3313 ///
3314 /// let (p, o) = (&Float::from(3)).pow_integer_prec_round_ref_ref(&Integer::from(5), 20, Floor);
3315 /// assert_eq!(p.to_string(), "243.00000");
3316 /// assert_eq!(o, Equal);
3317 ///
3318 /// let x = Float::from(3);
3319 /// let (p, o) = (&x).pow_integer_prec_round_ref_ref(&Integer::from(-2), 10, Ceiling);
3320 /// assert_eq!(p.to_string(), "0.11121");
3321 /// assert_eq!(o, Greater);
3322 /// ```
3323 #[inline]
3324 pub fn pow_integer_prec_round_ref_ref(
3325 &self,
3326 other: &Integer,
3327 prec: u64,
3328 rm: RoundingMode,
3329 ) -> (Self, Ordering) {
3330 self.pow_prec_round_ref_val(integer_to_exact_float(other.clone()), prec, rm)
3331 }
3332
3333 /// Raises a [`Float`] to the power of an [`Integer`], rounding the result to the specified
3334 /// precision and to the nearest value. Both are taken by value. An [`Ordering`] is also
3335 /// returned, indicating whether the rounded power is less than, equal to, or greater than the
3336 /// exact power. Although `NaN`s are not comparable to any [`Float`], whenever this function
3337 /// returns a `NaN` it also returns `Equal`.
3338 ///
3339 /// If the power is equidistant from two [`Float`]s with the specified precision, the [`Float`]
3340 /// with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of
3341 /// the `Nearest` rounding mode.
3342 ///
3343 /// $$
3344 /// f(x,n,p) = x^n+\varepsilon.
3345 /// $$
3346 /// - If $x^n$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
3347 /// - If $x^n$ is finite and nonzero, then $|\varepsilon| \leq 2^{\lfloor\log_2
3348 /// |x^n|\rfloor-p}$.
3349 ///
3350 /// See the [`Float::pow_integer_prec_round_ref_ref`] documentation for information on special
3351 /// cases, overflow, and underflow.
3352 ///
3353 /// If you want to use a rounding mode other than `Nearest`, consider using
3354 /// [`Float::pow_integer_prec_round`] instead.
3355 ///
3356 /// # Worst-case complexity
3357 /// $T(n) = O(n^{3/2} \log n \log\log n)$
3358 ///
3359 /// $M(n) = O(n \log n)$
3360 ///
3361 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
3362 /// other.significant_bits())`.
3363 ///
3364 /// # Examples
3365 /// ```
3366 /// use malachite_float::Float;
3367 /// use malachite_nz::integer::Integer;
3368 /// use std::cmp::Ordering::*;
3369 ///
3370 /// let (p, o) = Float::from(3).pow_integer_prec(Integer::from(5), 20);
3371 /// assert_eq!(p.to_string(), "243.00000");
3372 /// assert_eq!(o, Equal);
3373 ///
3374 /// let (p, o) = Float::from(3).pow_integer_prec(Integer::from(-2), 10);
3375 /// assert_eq!(p.to_string(), "0.11108");
3376 /// assert_eq!(o, Less);
3377 /// ```
3378 #[inline]
3379 pub fn pow_integer_prec(self, other: Integer, prec: u64) -> (Self, Ordering) {
3380 self.pow_integer_prec_round(other, prec, Nearest)
3381 }
3382
3383 /// Raises a [`Float`] to the power of an [`Integer`], rounding the result to the specified
3384 /// precision and to the nearest value. The [`Float`] is taken by value and the [`Integer`] by
3385 /// reference. An [`Ordering`] is also returned, indicating whether the rounded power is less
3386 /// than, equal to, or greater than the exact power. Although `NaN`s are not comparable to any
3387 /// [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
3388 ///
3389 /// If the power is equidistant from two [`Float`]s with the specified precision, the [`Float`]
3390 /// with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of
3391 /// the `Nearest` rounding mode.
3392 ///
3393 /// $$
3394 /// f(x,n,p) = x^n+\varepsilon.
3395 /// $$
3396 /// - If $x^n$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
3397 /// - If $x^n$ is finite and nonzero, then $|\varepsilon| \leq 2^{\lfloor\log_2
3398 /// |x^n|\rfloor-p}$.
3399 ///
3400 /// See the [`Float::pow_integer_prec_round_ref_ref`] documentation for information on special
3401 /// cases, overflow, and underflow.
3402 ///
3403 /// If you want to use a rounding mode other than `Nearest`, consider using
3404 /// [`Float::pow_integer_prec_round_val_ref`] instead.
3405 ///
3406 /// # Worst-case complexity
3407 /// $T(n) = O(n^{3/2} \log n \log\log n)$
3408 ///
3409 /// $M(n) = O(n \log n)$
3410 ///
3411 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
3412 /// other.significant_bits())`.
3413 ///
3414 /// # Examples
3415 /// ```
3416 /// use malachite_float::Float;
3417 /// use malachite_nz::integer::Integer;
3418 /// use std::cmp::Ordering::*;
3419 ///
3420 /// let (p, o) = Float::from(3).pow_integer_prec_val_ref(&Integer::from(5), 20);
3421 /// assert_eq!(p.to_string(), "243.00000");
3422 /// assert_eq!(o, Equal);
3423 ///
3424 /// let (p, o) = Float::from(3).pow_integer_prec_val_ref(&Integer::from(-2), 10);
3425 /// assert_eq!(p.to_string(), "0.11108");
3426 /// assert_eq!(o, Less);
3427 /// ```
3428 #[inline]
3429 pub fn pow_integer_prec_val_ref(self, other: &Integer, prec: u64) -> (Self, Ordering) {
3430 self.pow_integer_prec_round_val_ref(other, prec, Nearest)
3431 }
3432
3433 /// Raises a [`Float`] to the power of an [`Integer`], rounding the result to the specified
3434 /// precision and to the nearest value. The [`Float`] is taken by reference and the [`Integer`]
3435 /// by value. An [`Ordering`] is also returned, indicating whether the rounded power is less
3436 /// than, equal to, or greater than the exact power. Although `NaN`s are not comparable to any
3437 /// [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
3438 ///
3439 /// If the power is equidistant from two [`Float`]s with the specified precision, the [`Float`]
3440 /// with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of
3441 /// the `Nearest` rounding mode.
3442 ///
3443 /// $$
3444 /// f(x,n,p) = x^n+\varepsilon.
3445 /// $$
3446 /// - If $x^n$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
3447 /// - If $x^n$ is finite and nonzero, then $|\varepsilon| \leq 2^{\lfloor\log_2
3448 /// |x^n|\rfloor-p}$.
3449 ///
3450 /// See the [`Float::pow_integer_prec_round_ref_ref`] documentation for information on special
3451 /// cases, overflow, and underflow.
3452 ///
3453 /// If you want to use a rounding mode other than `Nearest`, consider using
3454 /// [`Float::pow_integer_prec_round_ref_val`] instead.
3455 ///
3456 /// # Worst-case complexity
3457 /// $T(n) = O(n^{3/2} \log n \log\log n)$
3458 ///
3459 /// $M(n) = O(n \log n)$
3460 ///
3461 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
3462 /// other.significant_bits())`.
3463 ///
3464 /// # Examples
3465 /// ```
3466 /// use malachite_float::Float;
3467 /// use malachite_nz::integer::Integer;
3468 /// use std::cmp::Ordering::*;
3469 ///
3470 /// let (p, o) = (&Float::from(3)).pow_integer_prec_ref_val(Integer::from(5), 20);
3471 /// assert_eq!(p.to_string(), "243.00000");
3472 /// assert_eq!(o, Equal);
3473 ///
3474 /// let (p, o) = (&Float::from(3)).pow_integer_prec_ref_val(Integer::from(-2), 10);
3475 /// assert_eq!(p.to_string(), "0.11108");
3476 /// assert_eq!(o, Less);
3477 /// ```
3478 #[inline]
3479 pub fn pow_integer_prec_ref_val(&self, other: Integer, prec: u64) -> (Self, Ordering) {
3480 self.pow_integer_prec_round_ref_val(other, prec, Nearest)
3481 }
3482
3483 /// Raises a [`Float`] to the power of an [`Integer`], rounding the result to the specified
3484 /// precision and to the nearest value. Both are taken by reference. An [`Ordering`] is also
3485 /// returned, indicating whether the rounded power is less than, equal to, or greater than the
3486 /// exact power. Although `NaN`s are not comparable to any [`Float`], whenever this function
3487 /// returns a `NaN` it also returns `Equal`.
3488 ///
3489 /// If the power is equidistant from two [`Float`]s with the specified precision, the [`Float`]
3490 /// with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of
3491 /// the `Nearest` rounding mode.
3492 ///
3493 /// $$
3494 /// f(x,n,p) = x^n+\varepsilon.
3495 /// $$
3496 /// - If $x^n$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
3497 /// - If $x^n$ is finite and nonzero, then $|\varepsilon| \leq 2^{\lfloor\log_2
3498 /// |x^n|\rfloor-p}$.
3499 ///
3500 /// See the [`Float::pow_integer_prec_round_ref_ref`] documentation for information on special
3501 /// cases, overflow, and underflow.
3502 ///
3503 /// If you want to use a rounding mode other than `Nearest`, consider using
3504 /// [`Float::pow_integer_prec_round_ref_ref`] instead.
3505 ///
3506 /// # Worst-case complexity
3507 /// $T(n) = O(n^{3/2} \log n \log\log n)$
3508 ///
3509 /// $M(n) = O(n \log n)$
3510 ///
3511 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
3512 /// other.significant_bits())`.
3513 ///
3514 /// # Examples
3515 /// ```
3516 /// use malachite_float::Float;
3517 /// use malachite_nz::integer::Integer;
3518 /// use std::cmp::Ordering::*;
3519 ///
3520 /// let (p, o) = (&Float::from(3)).pow_integer_prec_ref_ref(&Integer::from(5), 20);
3521 /// assert_eq!(p.to_string(), "243.00000");
3522 /// assert_eq!(o, Equal);
3523 ///
3524 /// let (p, o) = (&Float::from(3)).pow_integer_prec_ref_ref(&Integer::from(-2), 10);
3525 /// assert_eq!(p.to_string(), "0.11108");
3526 /// assert_eq!(o, Less);
3527 /// ```
3528 #[inline]
3529 pub fn pow_integer_prec_ref_ref(&self, other: &Integer, prec: u64) -> (Self, Ordering) {
3530 self.pow_integer_prec_round_ref_ref(other, prec, Nearest)
3531 }
3532
3533 /// Raises a [`Float`] to the power of an [`Integer`], rounding the result to the precision of
3534 /// the base and with the specified rounding mode. Both are taken by value. An [`Ordering`] is
3535 /// also returned, indicating whether the rounded power is less than, equal to, or greater than
3536 /// the exact power. Although `NaN`s are not comparable to any [`Float`], whenever this function
3537 /// returns a `NaN` it also returns `Equal`.
3538 ///
3539 /// The output precision is the precision of `self`. See [`RoundingMode`] for a description of
3540 /// the possible rounding modes.
3541 ///
3542 /// $$
3543 /// f(x,n,p,m) = x^n+\varepsilon.
3544 /// $$
3545 /// - If $x^n$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
3546 /// - If $x^n$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
3547 /// 2^{\lfloor\log_2 |x^n|\rfloor-p+1}$.
3548 /// - If $x^n$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
3549 /// 2^{\lfloor\log_2 |x^n|\rfloor-p}$.
3550 ///
3551 /// See the [`Float::pow_integer_prec_round_ref_ref`] documentation for information on special
3552 /// cases, overflow, and underflow.
3553 ///
3554 /// If you want to specify an output precision, consider using [`Float::pow_integer_prec_round`]
3555 /// instead.
3556 ///
3557 /// # Worst-case complexity
3558 /// $T(n) = O(n^{3/2} \log n \log\log n)$
3559 ///
3560 /// $M(n) = O(n \log n)$
3561 ///
3562 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
3563 /// other.significant_bits())`.
3564 ///
3565 /// # Panics
3566 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the base's
3567 /// precision.
3568 ///
3569 /// # Examples
3570 /// ```
3571 /// use malachite_base::rounding_modes::RoundingMode::*;
3572 /// use malachite_float::Float;
3573 /// use malachite_nz::integer::Integer;
3574 /// use std::cmp::Ordering::*;
3575 ///
3576 /// let (p, o) = Float::from(3).pow_integer_round(Integer::from(5), Floor);
3577 /// assert_eq!(p.to_string(), "1.9e2");
3578 /// assert_eq!(o, Less);
3579 ///
3580 /// let (p, o) = Float::from(3).pow_integer_round(Integer::from(5), Ceiling);
3581 /// assert_eq!(p.to_string(), "2.6e2");
3582 /// assert_eq!(o, Greater);
3583 /// ```
3584 #[inline]
3585 pub fn pow_integer_round(self, other: Integer, rm: RoundingMode) -> (Self, Ordering) {
3586 let prec = self.significant_bits();
3587 self.pow_integer_prec_round(other, prec, rm)
3588 }
3589
3590 /// Raises a [`Float`] to the power of an [`Integer`], rounding the result to the precision of
3591 /// the base and with the specified rounding mode. The [`Float`] is taken by value and the
3592 /// [`Integer`] by reference. An [`Ordering`] is also returned, indicating whether the rounded
3593 /// power is less than, equal to, or greater than the exact power. Although `NaN`s are not
3594 /// comparable to any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
3595 ///
3596 /// The output precision is the precision of `self`. See [`RoundingMode`] for a description of
3597 /// the possible rounding modes.
3598 ///
3599 /// $$
3600 /// f(x,n,p,m) = x^n+\varepsilon.
3601 /// $$
3602 /// - If $x^n$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
3603 /// - If $x^n$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
3604 /// 2^{\lfloor\log_2 |x^n|\rfloor-p+1}$.
3605 /// - If $x^n$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
3606 /// 2^{\lfloor\log_2 |x^n|\rfloor-p}$.
3607 ///
3608 /// See the [`Float::pow_integer_prec_round_ref_ref`] documentation for information on special
3609 /// cases, overflow, and underflow.
3610 ///
3611 /// If you want to specify an output precision, consider using
3612 /// [`Float::pow_integer_prec_round_val_ref`] instead.
3613 ///
3614 /// # Worst-case complexity
3615 /// $T(n) = O(n^{3/2} \log n \log\log n)$
3616 ///
3617 /// $M(n) = O(n \log n)$
3618 ///
3619 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
3620 /// other.significant_bits())`.
3621 ///
3622 /// # Panics
3623 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the base's
3624 /// precision.
3625 ///
3626 /// # Examples
3627 /// ```
3628 /// use malachite_base::rounding_modes::RoundingMode::*;
3629 /// use malachite_float::Float;
3630 /// use malachite_nz::integer::Integer;
3631 /// use std::cmp::Ordering::*;
3632 ///
3633 /// let (p, o) = Float::from(3).pow_integer_round_val_ref(&Integer::from(5), Floor);
3634 /// assert_eq!(p.to_string(), "1.9e2");
3635 /// assert_eq!(o, Less);
3636 ///
3637 /// let (p, o) = Float::from(3).pow_integer_round_val_ref(&Integer::from(5), Ceiling);
3638 /// assert_eq!(p.to_string(), "2.6e2");
3639 /// assert_eq!(o, Greater);
3640 /// ```
3641 #[inline]
3642 pub fn pow_integer_round_val_ref(self, other: &Integer, rm: RoundingMode) -> (Self, Ordering) {
3643 let prec = self.significant_bits();
3644 self.pow_integer_prec_round_val_ref(other, prec, rm)
3645 }
3646
3647 /// Raises a [`Float`] to the power of an [`Integer`], rounding the result to the precision of
3648 /// the base and with the specified rounding mode. The [`Float`] is taken by reference and the
3649 /// [`Integer`] by value. An [`Ordering`] is also returned, indicating whether the rounded power
3650 /// is less than, equal to, or greater than the exact power. Although `NaN`s are not comparable
3651 /// to any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
3652 ///
3653 /// The output precision is the precision of `self`. See [`RoundingMode`] for a description of
3654 /// the possible rounding modes.
3655 ///
3656 /// $$
3657 /// f(x,n,p,m) = x^n+\varepsilon.
3658 /// $$
3659 /// - If $x^n$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
3660 /// - If $x^n$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
3661 /// 2^{\lfloor\log_2 |x^n|\rfloor-p+1}$.
3662 /// - If $x^n$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
3663 /// 2^{\lfloor\log_2 |x^n|\rfloor-p}$.
3664 ///
3665 /// See the [`Float::pow_integer_prec_round_ref_ref`] documentation for information on special
3666 /// cases, overflow, and underflow.
3667 ///
3668 /// If you want to specify an output precision, consider using
3669 /// [`Float::pow_integer_prec_round_ref_val`] instead.
3670 ///
3671 /// # Worst-case complexity
3672 /// $T(n) = O(n^{3/2} \log n \log\log n)$
3673 ///
3674 /// $M(n) = O(n \log n)$
3675 ///
3676 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
3677 /// other.significant_bits())`.
3678 ///
3679 /// # Panics
3680 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the base's
3681 /// precision.
3682 ///
3683 /// # Examples
3684 /// ```
3685 /// use malachite_base::rounding_modes::RoundingMode::*;
3686 /// use malachite_float::Float;
3687 /// use malachite_nz::integer::Integer;
3688 /// use std::cmp::Ordering::*;
3689 ///
3690 /// let (p, o) = (&Float::from(3)).pow_integer_round_ref_val(Integer::from(5), Floor);
3691 /// assert_eq!(p.to_string(), "1.9e2");
3692 /// assert_eq!(o, Less);
3693 ///
3694 /// let (p, o) = (&Float::from(3)).pow_integer_round_ref_val(Integer::from(5), Ceiling);
3695 /// assert_eq!(p.to_string(), "2.6e2");
3696 /// assert_eq!(o, Greater);
3697 /// ```
3698 #[inline]
3699 pub fn pow_integer_round_ref_val(&self, other: Integer, rm: RoundingMode) -> (Self, Ordering) {
3700 let prec = self.significant_bits();
3701 self.pow_integer_prec_round_ref_val(other, prec, rm)
3702 }
3703
3704 /// Raises a [`Float`] to the power of an [`Integer`], rounding the result to the precision of
3705 /// the base and with the specified rounding mode. Both are taken by reference. An [`Ordering`]
3706 /// is also returned, indicating whether the rounded power is less than, equal to, or greater
3707 /// than the exact power. Although `NaN`s are not comparable to any [`Float`], whenever this
3708 /// function returns a `NaN` it also returns `Equal`.
3709 ///
3710 /// The output precision is the precision of `self`. See [`RoundingMode`] for a description of
3711 /// the possible rounding modes.
3712 ///
3713 /// $$
3714 /// f(x,n,p,m) = x^n+\varepsilon.
3715 /// $$
3716 /// - If $x^n$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
3717 /// - If $x^n$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
3718 /// 2^{\lfloor\log_2 |x^n|\rfloor-p+1}$.
3719 /// - If $x^n$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
3720 /// 2^{\lfloor\log_2 |x^n|\rfloor-p}$.
3721 ///
3722 /// See the [`Float::pow_integer_prec_round_ref_ref`] documentation for information on special
3723 /// cases, overflow, and underflow.
3724 ///
3725 /// If you want to specify an output precision, consider using
3726 /// [`Float::pow_integer_prec_round_ref_ref`] instead.
3727 ///
3728 /// # Worst-case complexity
3729 /// $T(n) = O(n^{3/2} \log n \log\log n)$
3730 ///
3731 /// $M(n) = O(n \log n)$
3732 ///
3733 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
3734 /// other.significant_bits())`.
3735 ///
3736 /// # Panics
3737 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the base's
3738 /// precision.
3739 ///
3740 /// # Examples
3741 /// ```
3742 /// use malachite_base::rounding_modes::RoundingMode::*;
3743 /// use malachite_float::Float;
3744 /// use malachite_nz::integer::Integer;
3745 /// use std::cmp::Ordering::*;
3746 ///
3747 /// let (p, o) = (&Float::from(3)).pow_integer_round_ref_ref(&Integer::from(5), Floor);
3748 /// assert_eq!(p.to_string(), "1.9e2");
3749 /// assert_eq!(o, Less);
3750 ///
3751 /// let (p, o) = (&Float::from(3)).pow_integer_round_ref_ref(&Integer::from(5), Ceiling);
3752 /// assert_eq!(p.to_string(), "2.6e2");
3753 /// assert_eq!(o, Greater);
3754 /// ```
3755 #[inline]
3756 pub fn pow_integer_round_ref_ref(&self, other: &Integer, rm: RoundingMode) -> (Self, Ordering) {
3757 let prec = self.significant_bits();
3758 self.pow_integer_prec_round_ref_ref(other, prec, rm)
3759 }
3760
3761 /// Raises a [`Float`] to the power of an [`Integer`] in place, taking the [`Integer`] by value.
3762 ///
3763 /// See the [`Float::pow_integer_prec_round_ref_ref`] documentation for information on special
3764 /// cases, overflow, and underflow.
3765 ///
3766 /// # Worst-case complexity
3767 /// $T(n) = O(n^{3/2} \log n \log\log n)$
3768 ///
3769 /// $M(n) = O(n \log n)$
3770 ///
3771 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
3772 /// other.significant_bits())`.
3773 ///
3774 /// # Panics
3775 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the given
3776 /// precision.
3777 ///
3778 /// # Examples
3779 /// ```
3780 /// use malachite_base::rounding_modes::RoundingMode::*;
3781 /// use malachite_float::Float;
3782 /// use malachite_nz::integer::Integer;
3783 /// use std::cmp::Ordering::*;
3784 ///
3785 /// let mut x = Float::from(3);
3786 /// let o = x.pow_integer_prec_round_assign(Integer::from(5), 20, Floor);
3787 /// assert_eq!(x.to_string(), "243.00000");
3788 /// assert_eq!(o, Equal);
3789 /// ```
3790 #[inline]
3791 pub fn pow_integer_prec_round_assign(
3792 &mut self,
3793 other: Integer,
3794 prec: u64,
3795 rm: RoundingMode,
3796 ) -> Ordering {
3797 self.pow_prec_round_assign(integer_to_exact_float(other), prec, rm)
3798 }
3799
3800 /// Raises a [`Float`] to the power of an [`Integer`] in place, taking the [`Integer`] by
3801 /// reference.
3802 ///
3803 /// See the [`Float::pow_integer_prec_round_ref_ref`] documentation for information on special
3804 /// cases, overflow, and underflow.
3805 ///
3806 /// # Worst-case complexity
3807 /// $T(n) = O(n^{3/2} \log n \log\log n)$
3808 ///
3809 /// $M(n) = O(n \log n)$
3810 ///
3811 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
3812 /// other.significant_bits())`.
3813 ///
3814 /// # Panics
3815 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the given
3816 /// precision.
3817 ///
3818 /// # Examples
3819 /// ```
3820 /// use malachite_base::rounding_modes::RoundingMode::*;
3821 /// use malachite_float::Float;
3822 /// use malachite_nz::integer::Integer;
3823 /// use std::cmp::Ordering::*;
3824 ///
3825 /// let mut x = Float::from(3);
3826 /// let o = x.pow_integer_prec_round_assign_ref(&Integer::from(5), 20, Floor);
3827 /// assert_eq!(x.to_string(), "243.00000");
3828 /// assert_eq!(o, Equal);
3829 /// ```
3830 #[inline]
3831 pub fn pow_integer_prec_round_assign_ref(
3832 &mut self,
3833 other: &Integer,
3834 prec: u64,
3835 rm: RoundingMode,
3836 ) -> Ordering {
3837 self.pow_prec_round_assign(integer_to_exact_float(other.clone()), prec, rm)
3838 }
3839
3840 /// Raises a [`Float`] to the power of an [`Integer`] in place, taking the [`Integer`] by value.
3841 ///
3842 /// See the [`Float::pow_integer_prec_round_ref_ref`] documentation for information on special
3843 /// cases, overflow, and underflow.
3844 ///
3845 /// # Worst-case complexity
3846 /// $T(n) = O(n^{3/2} \log n \log\log n)$
3847 ///
3848 /// $M(n) = O(n \log n)$
3849 ///
3850 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
3851 /// other.significant_bits())`.
3852 ///
3853 /// # Examples
3854 /// ```
3855 /// use malachite_float::Float;
3856 /// use malachite_nz::integer::Integer;
3857 /// use std::cmp::Ordering::*;
3858 ///
3859 /// let mut x = Float::from(3);
3860 /// let o = x.pow_integer_prec_assign(Integer::from(5), 20);
3861 /// assert_eq!(x.to_string(), "243.00000");
3862 /// assert_eq!(o, Equal);
3863 /// ```
3864 #[inline]
3865 pub fn pow_integer_prec_assign(&mut self, other: Integer, prec: u64) -> Ordering {
3866 self.pow_prec_assign(integer_to_exact_float(other), prec)
3867 }
3868
3869 /// Raises a [`Float`] to the power of an [`Integer`] in place, taking the [`Integer`] by
3870 /// reference.
3871 ///
3872 /// See the [`Float::pow_integer_prec_round_ref_ref`] documentation for information on special
3873 /// cases, overflow, and underflow.
3874 ///
3875 /// # Worst-case complexity
3876 /// $T(n) = O(n^{3/2} \log n \log\log n)$
3877 ///
3878 /// $M(n) = O(n \log n)$
3879 ///
3880 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
3881 /// other.significant_bits())`.
3882 ///
3883 /// # Examples
3884 /// ```
3885 /// use malachite_float::Float;
3886 /// use malachite_nz::integer::Integer;
3887 /// use std::cmp::Ordering::*;
3888 ///
3889 /// let mut x = Float::from(3);
3890 /// let o = x.pow_integer_prec_assign_ref(&Integer::from(5), 20);
3891 /// assert_eq!(x.to_string(), "243.00000");
3892 /// assert_eq!(o, Equal);
3893 /// ```
3894 #[inline]
3895 pub fn pow_integer_prec_assign_ref(&mut self, other: &Integer, prec: u64) -> Ordering {
3896 self.pow_prec_assign(integer_to_exact_float(other.clone()), prec)
3897 }
3898
3899 /// Raises a [`Float`] to the power of an [`Integer`] in place, taking the [`Integer`] by value.
3900 ///
3901 /// See the [`Float::pow_integer_prec_round_ref_ref`] documentation for information on special
3902 /// cases, overflow, and underflow.
3903 ///
3904 /// # Worst-case complexity
3905 /// $T(n) = O(n^{3/2} \log n \log\log n)$
3906 ///
3907 /// $M(n) = O(n \log n)$
3908 ///
3909 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
3910 /// other.significant_bits())`.
3911 ///
3912 /// # Panics
3913 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the base's
3914 /// precision.
3915 ///
3916 /// # Examples
3917 /// ```
3918 /// use malachite_base::rounding_modes::RoundingMode::*;
3919 /// use malachite_float::Float;
3920 /// use malachite_nz::integer::Integer;
3921 /// use std::cmp::Ordering::*;
3922 ///
3923 /// let mut x = Float::from(3);
3924 /// let o = x.pow_integer_round_assign(Integer::from(5), Floor);
3925 /// assert_eq!(x.to_string(), "1.9e2");
3926 /// assert_eq!(o, Less);
3927 /// ```
3928 pub fn pow_integer_round_assign(&mut self, other: Integer, rm: RoundingMode) -> Ordering {
3929 let prec = self.significant_bits();
3930 self.pow_prec_round_assign(integer_to_exact_float(other), prec, rm)
3931 }
3932
3933 /// Raises a [`Float`] to the power of an [`Integer`] in place, taking the [`Integer`] by
3934 /// reference.
3935 ///
3936 /// See the [`Float::pow_integer_prec_round_ref_ref`] documentation for information on special
3937 /// cases, overflow, and underflow.
3938 ///
3939 /// # Worst-case complexity
3940 /// $T(n) = O(n^{3/2} \log n \log\log n)$
3941 ///
3942 /// $M(n) = O(n \log n)$
3943 ///
3944 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
3945 /// other.significant_bits())`.
3946 ///
3947 /// # Panics
3948 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the base's
3949 /// precision.
3950 ///
3951 /// # Examples
3952 /// ```
3953 /// use malachite_base::rounding_modes::RoundingMode::*;
3954 /// use malachite_float::Float;
3955 /// use malachite_nz::integer::Integer;
3956 /// use std::cmp::Ordering::*;
3957 ///
3958 /// let mut x = Float::from(3);
3959 /// let o = x.pow_integer_round_assign_ref(&Integer::from(5), Floor);
3960 /// assert_eq!(x.to_string(), "1.9e2");
3961 /// assert_eq!(o, Less);
3962 /// ```
3963 pub fn pow_integer_round_assign_ref(&mut self, other: &Integer, rm: RoundingMode) -> Ordering {
3964 let prec = self.significant_bits();
3965 self.pow_prec_round_assign(integer_to_exact_float(other.clone()), prec, rm)
3966 }
3967}
3968
3969impl Pow<Integer> for Float {
3970 type Output = Self;
3971
3972 /// Raises a [`Float`] to the power of an [`Integer`], rounding the result to the nearest value.
3973 /// Both are taken by value.
3974 ///
3975 /// The output precision is the precision of the base. If the power is equidistant from two
3976 /// [`Float`]s with that precision, the [`Float`] with fewer 1s in its binary expansion is
3977 /// chosen. See [`RoundingMode`] for a description of the `Nearest` rounding mode.
3978 ///
3979 /// $$
3980 /// f(x,n) = x^n+\varepsilon.
3981 /// $$
3982 /// - If $x^n$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
3983 /// - If $x^n$ is finite and nonzero, then $|\varepsilon| \leq 2^{\lfloor\log_2
3984 /// |x^n|\rfloor-p}$, where $p$ is the precision of the base.
3985 ///
3986 /// See the [`Float::pow_integer_prec_round_ref_ref`] documentation for information on special
3987 /// cases, overflow, and underflow.
3988 ///
3989 /// If you want to specify an output precision, consider using [`Float::pow_integer_prec`]
3990 /// instead. If you want to specify the output precision and the rounding mode, consider using
3991 /// [`Float::pow_integer_prec_round`] instead.
3992 ///
3993 /// # Worst-case complexity
3994 /// $T(n) = O(n^{3/2} \log n \log\log n)$
3995 ///
3996 /// $M(n) = O(n \log n)$
3997 ///
3998 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
3999 /// other.significant_bits())`.
4000 ///
4001 /// # Examples
4002 /// ```
4003 /// use malachite_base::num::arithmetic::traits::Pow;
4004 /// use malachite_base::num::basic::traits::Two;
4005 /// use malachite_float::Float;
4006 /// use malachite_nz::integer::Integer;
4007 ///
4008 /// assert_eq!(Float::TWO.pow(Integer::from(10)).to_string(), "1.0e3");
4009 /// assert_eq!(Float::TWO.pow(Integer::from(-3)).to_string(), "0.12");
4010 /// ```
4011 #[inline]
4012 fn pow(self, other: Integer) -> Self {
4013 let prec = self.significant_bits();
4014 self.pow_integer_prec(other, prec).0
4015 }
4016}
4017
4018impl Pow<&Integer> for Float {
4019 type Output = Self;
4020
4021 /// Raises a [`Float`] to the power of an [`Integer`], rounding the result to the nearest value.
4022 /// The [`Float`] is taken by value and the [`Integer`] by reference.
4023 ///
4024 /// The output precision is the precision of the base. If the power is equidistant from two
4025 /// [`Float`]s with that precision, the [`Float`] with fewer 1s in its binary expansion is
4026 /// chosen. See [`RoundingMode`] for a description of the `Nearest` rounding mode.
4027 ///
4028 /// $$
4029 /// f(x,n) = x^n+\varepsilon.
4030 /// $$
4031 /// - If $x^n$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
4032 /// - If $x^n$ is finite and nonzero, then $|\varepsilon| \leq 2^{\lfloor\log_2
4033 /// |x^n|\rfloor-p}$, where $p$ is the precision of the base.
4034 ///
4035 /// See the [`Float::pow_integer_prec_round_ref_ref`] documentation for information on special
4036 /// cases, overflow, and underflow.
4037 ///
4038 /// If you want to specify an output precision, consider using [`Float::pow_integer_prec`]
4039 /// instead. If you want to specify the output precision and the rounding mode, consider using
4040 /// [`Float::pow_integer_prec_round`] instead.
4041 ///
4042 /// # Worst-case complexity
4043 /// $T(n) = O(n^{3/2} \log n \log\log n)$
4044 ///
4045 /// $M(n) = O(n \log n)$
4046 ///
4047 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
4048 /// other.significant_bits())`.
4049 ///
4050 /// # Examples
4051 /// ```
4052 /// use malachite_base::num::arithmetic::traits::Pow;
4053 /// use malachite_base::num::basic::traits::Two;
4054 /// use malachite_float::Float;
4055 /// use malachite_nz::integer::Integer;
4056 ///
4057 /// assert_eq!(Float::TWO.pow(&Integer::from(10)).to_string(), "1.0e3");
4058 /// assert_eq!(Float::TWO.pow(&Integer::from(-3)).to_string(), "0.12");
4059 /// ```
4060 #[inline]
4061 fn pow(self, other: &Integer) -> Self {
4062 let prec = self.significant_bits();
4063 self.pow_integer_prec_val_ref(other, prec).0
4064 }
4065}
4066
4067impl Pow<Integer> for &Float {
4068 type Output = Float;
4069
4070 /// Raises a [`Float`] to the power of an [`Integer`], rounding the result to the nearest value.
4071 /// The [`Float`] is taken by reference and the [`Integer`] by value.
4072 ///
4073 /// The output precision is the precision of the base. If the power is equidistant from two
4074 /// [`Float`]s with that precision, the [`Float`] with fewer 1s in its binary expansion is
4075 /// chosen. See [`RoundingMode`] for a description of the `Nearest` rounding mode.
4076 ///
4077 /// $$
4078 /// f(x,n) = x^n+\varepsilon.
4079 /// $$
4080 /// - If $x^n$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
4081 /// - If $x^n$ is finite and nonzero, then $|\varepsilon| \leq 2^{\lfloor\log_2
4082 /// |x^n|\rfloor-p}$, where $p$ is the precision of the base.
4083 ///
4084 /// See the [`Float::pow_integer_prec_round_ref_ref`] documentation for information on special
4085 /// cases, overflow, and underflow.
4086 ///
4087 /// If you want to specify an output precision, consider using [`Float::pow_integer_prec`]
4088 /// instead. If you want to specify the output precision and the rounding mode, consider using
4089 /// [`Float::pow_integer_prec_round`] instead.
4090 ///
4091 /// # Worst-case complexity
4092 /// $T(n) = O(n^{3/2} \log n \log\log n)$
4093 ///
4094 /// $M(n) = O(n \log n)$
4095 ///
4096 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
4097 /// other.significant_bits())`.
4098 ///
4099 /// # Examples
4100 /// ```
4101 /// use malachite_base::num::arithmetic::traits::Pow;
4102 /// use malachite_base::num::basic::traits::Two;
4103 /// use malachite_float::Float;
4104 /// use malachite_nz::integer::Integer;
4105 ///
4106 /// assert_eq!((&Float::TWO).pow(Integer::from(10)).to_string(), "1.0e3");
4107 /// assert_eq!((&Float::TWO).pow(Integer::from(-3)).to_string(), "0.12");
4108 /// ```
4109 #[inline]
4110 fn pow(self, other: Integer) -> Float {
4111 let prec = self.significant_bits();
4112 self.pow_integer_prec_ref_val(other, prec).0
4113 }
4114}
4115
4116impl Pow<&Integer> for &Float {
4117 type Output = Float;
4118
4119 /// Raises a [`Float`] to the power of an [`Integer`], rounding the result to the nearest value.
4120 /// Both are taken by reference.
4121 ///
4122 /// The output precision is the precision of the base. If the power is equidistant from two
4123 /// [`Float`]s with that precision, the [`Float`] with fewer 1s in its binary expansion is
4124 /// chosen. See [`RoundingMode`] for a description of the `Nearest` rounding mode.
4125 ///
4126 /// $$
4127 /// f(x,n) = x^n+\varepsilon.
4128 /// $$
4129 /// - If $x^n$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
4130 /// - If $x^n$ is finite and nonzero, then $|\varepsilon| \leq 2^{\lfloor\log_2
4131 /// |x^n|\rfloor-p}$, where $p$ is the precision of the base.
4132 ///
4133 /// See the [`Float::pow_integer_prec_round_ref_ref`] documentation for information on special
4134 /// cases, overflow, and underflow.
4135 ///
4136 /// If you want to specify an output precision, consider using [`Float::pow_integer_prec`]
4137 /// instead. If you want to specify the output precision and the rounding mode, consider using
4138 /// [`Float::pow_integer_prec_round`] instead.
4139 ///
4140 /// # Worst-case complexity
4141 /// $T(n) = O(n^{3/2} \log n \log\log n)$
4142 ///
4143 /// $M(n) = O(n \log n)$
4144 ///
4145 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
4146 /// other.significant_bits())`.
4147 ///
4148 /// # Examples
4149 /// ```
4150 /// use malachite_base::num::arithmetic::traits::Pow;
4151 /// use malachite_base::num::basic::traits::Two;
4152 /// use malachite_float::Float;
4153 /// use malachite_nz::integer::Integer;
4154 ///
4155 /// assert_eq!((&Float::TWO).pow(&Integer::from(10)).to_string(), "1.0e3");
4156 /// assert_eq!((&Float::TWO).pow(&Integer::from(-3)).to_string(), "0.12");
4157 /// ```
4158 #[inline]
4159 fn pow(self, other: &Integer) -> Float {
4160 let prec = self.significant_bits();
4161 self.pow_integer_prec_ref_ref(other, prec).0
4162 }
4163}
4164
4165impl PowAssign<Integer> for Float {
4166 /// Raises a [`Float`] to the power of an [`Integer`] in place, taking the [`Integer`] by value,
4167 /// and rounding the result to the nearest value.
4168 ///
4169 /// The output precision is the precision of the base. See the
4170 /// [`Float::pow_integer_prec_round_ref_ref`] documentation for information on special cases,
4171 /// overflow, and underflow.
4172 ///
4173 /// # Worst-case complexity
4174 /// $T(n) = O(n^{3/2} \log n \log\log n)$
4175 ///
4176 /// $M(n) = O(n \log n)$
4177 ///
4178 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
4179 /// other.significant_bits())`.
4180 ///
4181 /// # Examples
4182 /// ```
4183 /// use malachite_base::num::arithmetic::traits::PowAssign;
4184 /// use malachite_base::num::basic::traits::Two;
4185 /// use malachite_float::Float;
4186 /// use malachite_nz::integer::Integer;
4187 ///
4188 /// let mut x = Float::TWO;
4189 /// x.pow_assign(Integer::from(10));
4190 /// assert_eq!(x.to_string(), "1.0e3");
4191 /// ```
4192 #[inline]
4193 fn pow_assign(&mut self, other: Integer) {
4194 let prec = self.significant_bits();
4195 self.pow_integer_prec_assign(other, prec);
4196 }
4197}
4198
4199impl PowAssign<&Integer> for Float {
4200 /// Raises a [`Float`] to the power of an [`Integer`] in place, taking the [`Integer`] by
4201 /// reference, and rounding the result to the nearest value.
4202 ///
4203 /// The output precision is the precision of the base. See the
4204 /// [`Float::pow_integer_prec_round_ref_ref`] documentation for information on special cases,
4205 /// overflow, and underflow.
4206 ///
4207 /// # Worst-case complexity
4208 /// $T(n) = O(n^{3/2} \log n \log\log n)$
4209 ///
4210 /// $M(n) = O(n \log n)$
4211 ///
4212 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
4213 /// other.significant_bits())`.
4214 ///
4215 /// # Examples
4216 /// ```
4217 /// use malachite_base::num::arithmetic::traits::PowAssign;
4218 /// use malachite_base::num::basic::traits::Two;
4219 /// use malachite_float::Float;
4220 /// use malachite_nz::integer::Integer;
4221 ///
4222 /// let mut x = Float::TWO;
4223 /// x.pow_assign(&Integer::from(10));
4224 /// assert_eq!(x.to_string(), "1.0e3");
4225 /// ```
4226 #[inline]
4227 fn pow_assign(&mut self, other: &Integer) {
4228 let prec = self.significant_bits();
4229 self.pow_integer_prec_assign_ref(other, prec);
4230 }
4231}
4232
4233impl Float {
4234 /// Raises a [`Float`] to the power of a [`u64`], rounding the result to the specified precision
4235 /// and with the specified rounding mode. The [`Float`] is taken by value. An [`Ordering`] is
4236 /// also returned, indicating whether the rounded power is less than, equal to, or greater than
4237 /// the exact power. Although `NaN`s are not comparable to any [`Float`], whenever this function
4238 /// returns a `NaN` it also returns `Equal`.
4239 ///
4240 /// See [`RoundingMode`] for a description of the possible rounding modes.
4241 ///
4242 /// $$
4243 /// f(x,n,p,m) = x^n+\varepsilon.
4244 /// $$
4245 /// - If $x^n$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
4246 /// - If $x^n$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
4247 /// 2^{\lfloor\log_2 |x^n|\rfloor-p+1}$.
4248 /// - If $x^n$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
4249 /// 2^{\lfloor\log_2 |x^n|\rfloor-p}$.
4250 ///
4251 /// Special cases:
4252 /// - $f(x,0)=1.0$ for any $x$, even `NaN`
4253 /// - $f(1.0,n)=1.0$
4254 /// - $f(\text{NaN},n)=\text{NaN}$ if $n \neq 0$
4255 /// - $f(-1.0,n)=1.0$ if $n$ is even, and $-1.0$ if $n$ is odd
4256 /// - $f(\infty,n)=\infty$ if $n>0$
4257 /// - $f(-\infty,n)=\infty$ if $n$ is positive and even, and $-\infty$ if $n$ is odd
4258 /// - $f(0.0,n)=0.0$ if $n>0$
4259 /// - $f(-0.0,n)=0.0$ if $n$ is positive and even, and $-0.0$ if $n$ is odd
4260 ///
4261 /// Overflow and underflow:
4262 /// - If $f(x,n,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
4263 /// returned instead.
4264 /// - 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}$
4265 /// is returned instead.
4266 /// - If $0<f(x,n,p,m)<2^{-2^{30}}$ and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
4267 /// - If $0<f(x,n,p,m)<2^{-2^{30}}$ and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
4268 /// instead.
4269 /// - If $0<f(x,n,p,m)\leq2^{-2^{30}-1}$ and $m$ is `Nearest`, $0.0$ is returned instead.
4270 /// - If $2^{-2^{30}-1}<f(x,n,p,m)<2^{-2^{30}}$ and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
4271 /// instead.
4272 /// - Negative results (from negative $x$ and odd $n$) mirror the bullets above, with the
4273 /// rounding directions reflected.
4274 ///
4275 /// # Worst-case complexity
4276 /// $T(n, m) = O(mn \log n \log\log n)$
4277 ///
4278 /// $M(n) = O(n \log n)$
4279 ///
4280 /// where $T$ is time, $M$ is additional memory, $n$ is `max(prec, self.significant_bits())`,
4281 /// and $m$ is the number of significant bits of the exponent `n`.
4282 ///
4283 /// # Panics
4284 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the given
4285 /// precision.
4286 ///
4287 /// # Examples
4288 /// ```
4289 /// use malachite_base::rounding_modes::RoundingMode::*;
4290 /// use malachite_float::Float;
4291 /// use std::cmp::Ordering::*;
4292 ///
4293 /// let (p, o) = Float::from(3).pow_u_prec_round(5, 20, Floor);
4294 /// assert_eq!(p.to_string(), "243.00000");
4295 /// assert_eq!(o, Equal);
4296 ///
4297 /// let (p, o) = Float::from(3).pow_u_prec_round(5, 2, Ceiling);
4298 /// assert_eq!(p.to_string(), "2.6e2");
4299 /// assert_eq!(o, Greater);
4300 /// ```
4301 #[inline]
4302 pub fn pow_u_prec_round(self, n: u64, prec: u64, rm: RoundingMode) -> (Self, Ordering) {
4303 pow_u(self, n, prec, rm)
4304 }
4305
4306 /// Raises a [`Float`] to the power of a [`u64`], rounding the result to the specified precision
4307 /// and with the specified rounding mode. The [`Float`] is taken by reference. An [`Ordering`]
4308 /// is also returned, indicating whether the rounded power is less than, equal to, or greater
4309 /// than the exact power. Although `NaN`s are not comparable to any [`Float`], whenever this
4310 /// function returns a `NaN` it also returns `Equal`.
4311 ///
4312 /// See [`RoundingMode`] for a description of the possible rounding modes.
4313 ///
4314 /// $$
4315 /// f(x,n,p,m) = x^n+\varepsilon.
4316 /// $$
4317 /// - If $x^n$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
4318 /// - If $x^n$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
4319 /// 2^{\lfloor\log_2 |x^n|\rfloor-p+1}$.
4320 /// - If $x^n$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
4321 /// 2^{\lfloor\log_2 |x^n|\rfloor-p}$.
4322 ///
4323 /// See the [`Float::pow_u_prec_round`] documentation for information on special cases,
4324 /// overflow, and underflow.
4325 ///
4326 /// # Worst-case complexity
4327 /// $T(n, m) = O(mn \log n \log\log n)$
4328 ///
4329 /// $M(n) = O(n \log n)$
4330 ///
4331 /// where $T$ is time, $M$ is additional memory, $n$ is `max(prec, self.significant_bits())`,
4332 /// and $m$ is the number of significant bits of the exponent `n`.
4333 ///
4334 /// # Panics
4335 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the given
4336 /// precision.
4337 ///
4338 /// # Examples
4339 /// ```
4340 /// use malachite_base::rounding_modes::RoundingMode::*;
4341 /// use malachite_float::Float;
4342 /// use std::cmp::Ordering::*;
4343 ///
4344 /// let (p, o) = (&Float::from(3)).pow_u_prec_round_ref(5, 20, Floor);
4345 /// assert_eq!(p.to_string(), "243.00000");
4346 /// assert_eq!(o, Equal);
4347 ///
4348 /// let (p, o) = (&Float::from(3)).pow_u_prec_round_ref(5, 2, Ceiling);
4349 /// assert_eq!(p.to_string(), "2.6e2");
4350 /// assert_eq!(o, Greater);
4351 /// ```
4352 #[inline]
4353 pub fn pow_u_prec_round_ref(&self, n: u64, prec: u64, rm: RoundingMode) -> (Self, Ordering) {
4354 pow_u_ref(self, n, prec, rm)
4355 }
4356
4357 /// Raises a [`Float`] to the power of a [`u64`], rounding the result to the specified precision
4358 /// and to the nearest value. The [`Float`] is taken by value. An [`Ordering`] is also returned,
4359 /// indicating whether the rounded power is less than, equal to, or greater than the exact
4360 /// power. Although `NaN`s are not comparable to any [`Float`], whenever this function returns a
4361 /// `NaN` it also returns `Equal`.
4362 ///
4363 /// If the power is equidistant from two [`Float`]s with the specified precision, the [`Float`]
4364 /// with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of
4365 /// the `Nearest` rounding mode.
4366 ///
4367 /// $$
4368 /// f(x,n,p) = x^n+\varepsilon.
4369 /// $$
4370 /// - If $x^n$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
4371 /// - If $x^n$ is finite and nonzero, then $|\varepsilon| \leq 2^{\lfloor\log_2
4372 /// |x^n|\rfloor-p}$.
4373 ///
4374 /// See the [`Float::pow_u_prec_round`] documentation for information on special cases,
4375 /// overflow, and underflow.
4376 ///
4377 /// If you want to use a rounding mode other than `Nearest`, consider using
4378 /// [`Float::pow_u_prec_round`] instead.
4379 ///
4380 /// # Worst-case complexity
4381 /// $T(n, m) = O(mn \log n \log\log n)$
4382 ///
4383 /// $M(n) = O(n \log n)$
4384 ///
4385 /// where $T$ is time, $M$ is additional memory, $n$ is `max(prec, self.significant_bits())`,
4386 /// and $m$ is the number of significant bits of the exponent `n`.
4387 ///
4388 /// # Examples
4389 /// ```
4390 /// use malachite_float::Float;
4391 /// use std::cmp::Ordering::*;
4392 ///
4393 /// let (p, o) = Float::from(3).pow_u_prec(5, 20);
4394 /// assert_eq!(p.to_string(), "243.00000");
4395 /// assert_eq!(o, Equal);
4396 ///
4397 /// let (p, o) = Float::from(3).pow_u_prec(5, 2);
4398 /// assert_eq!(p.to_string(), "2.6e2");
4399 /// assert_eq!(o, Greater);
4400 /// ```
4401 #[inline]
4402 pub fn pow_u_prec(self, n: u64, prec: u64) -> (Self, Ordering) {
4403 pow_u(self, n, prec, Nearest)
4404 }
4405
4406 /// Raises a [`Float`] to the power of a [`u64`], rounding the result to the specified precision
4407 /// and to the nearest value. The [`Float`] is taken by reference. An [`Ordering`] is also
4408 /// returned, indicating whether the rounded power is less than, equal to, or greater than the
4409 /// exact power. Although `NaN`s are not comparable to any [`Float`], whenever this function
4410 /// returns a `NaN` it also returns `Equal`.
4411 ///
4412 /// If the power is equidistant from two [`Float`]s with the specified precision, the [`Float`]
4413 /// with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of
4414 /// the `Nearest` rounding mode.
4415 ///
4416 /// $$
4417 /// f(x,n,p) = x^n+\varepsilon.
4418 /// $$
4419 /// - If $x^n$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
4420 /// - If $x^n$ is finite and nonzero, then $|\varepsilon| \leq 2^{\lfloor\log_2
4421 /// |x^n|\rfloor-p}$.
4422 ///
4423 /// See the [`Float::pow_u_prec_round`] documentation for information on special cases,
4424 /// overflow, and underflow.
4425 ///
4426 /// If you want to use a rounding mode other than `Nearest`, consider using
4427 /// [`Float::pow_u_prec_round_ref`] instead.
4428 ///
4429 /// # Worst-case complexity
4430 /// $T(n, m) = O(mn \log n \log\log n)$
4431 ///
4432 /// $M(n) = O(n \log n)$
4433 ///
4434 /// where $T$ is time, $M$ is additional memory, $n$ is `max(prec, self.significant_bits())`,
4435 /// and $m$ is the number of significant bits of the exponent `n`.
4436 ///
4437 /// # Examples
4438 /// ```
4439 /// use malachite_float::Float;
4440 /// use std::cmp::Ordering::*;
4441 ///
4442 /// let (p, o) = (&Float::from(3)).pow_u_prec_ref(5, 20);
4443 /// assert_eq!(p.to_string(), "243.00000");
4444 /// assert_eq!(o, Equal);
4445 ///
4446 /// let (p, o) = (&Float::from(3)).pow_u_prec_ref(5, 2);
4447 /// assert_eq!(p.to_string(), "2.6e2");
4448 /// assert_eq!(o, Greater);
4449 /// ```
4450 #[inline]
4451 pub fn pow_u_prec_ref(&self, n: u64, prec: u64) -> (Self, Ordering) {
4452 pow_u_ref(self, n, prec, Nearest)
4453 }
4454
4455 /// Raises a [`Float`] to the power of a [`u64`], rounding the result to the precision of the
4456 /// base and with the specified rounding mode. The [`Float`] is taken by value. An [`Ordering`]
4457 /// is also returned, indicating whether the rounded power is less than, equal to, or greater
4458 /// than the exact power. Although `NaN`s are not comparable to any [`Float`], whenever this
4459 /// function returns a `NaN` it also returns `Equal`.
4460 ///
4461 /// The output precision is the precision of `self`. See [`RoundingMode`] for a description of
4462 /// the possible rounding modes.
4463 ///
4464 /// $$
4465 /// f(x,n,p,m) = x^n+\varepsilon.
4466 /// $$
4467 /// - If $x^n$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
4468 /// - If $x^n$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
4469 /// 2^{\lfloor\log_2 |x^n|\rfloor-p+1}$.
4470 /// - If $x^n$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
4471 /// 2^{\lfloor\log_2 |x^n|\rfloor-p}$.
4472 ///
4473 /// See the [`Float::pow_u_prec_round`] documentation for information on special cases,
4474 /// overflow, and underflow.
4475 ///
4476 /// If you want to specify an output precision, consider using [`Float::pow_u_prec_round`]
4477 /// instead.
4478 ///
4479 /// # Worst-case complexity
4480 /// $T(n) = O(n \log n \log\log n)$
4481 ///
4482 /// $M(n) = O(n \log n)$
4483 ///
4484 /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
4485 ///
4486 /// # Panics
4487 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the base's
4488 /// precision.
4489 ///
4490 /// # Examples
4491 /// ```
4492 /// use malachite_base::rounding_modes::RoundingMode::*;
4493 /// use malachite_float::Float;
4494 /// use std::cmp::Ordering::*;
4495 ///
4496 /// let (p, o) = Float::from(3).pow_u_round(5, Floor);
4497 /// assert_eq!(p.to_string(), "1.9e2");
4498 /// assert_eq!(o, Less);
4499 ///
4500 /// let (p, o) = Float::from(3).pow_u_round(5, Ceiling);
4501 /// assert_eq!(p.to_string(), "2.6e2");
4502 /// assert_eq!(o, Greater);
4503 /// ```
4504 #[inline]
4505 pub fn pow_u_round(self, n: u64, rm: RoundingMode) -> (Self, Ordering) {
4506 let prec = self.significant_bits();
4507 pow_u(self, n, prec, rm)
4508 }
4509
4510 /// Raises a [`Float`] to the power of a [`u64`], rounding the result to the precision of the
4511 /// base and with the specified rounding mode. The [`Float`] is taken by reference. An
4512 /// [`Ordering`] is also returned, indicating whether the rounded power is less than, equal to,
4513 /// or greater than the exact power. Although `NaN`s are not comparable to any [`Float`],
4514 /// whenever this function returns a `NaN` it also returns `Equal`.
4515 ///
4516 /// The output precision is the precision of `self`. See [`RoundingMode`] for a description of
4517 /// the possible rounding modes.
4518 ///
4519 /// $$
4520 /// f(x,n,p,m) = x^n+\varepsilon.
4521 /// $$
4522 /// - If $x^n$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
4523 /// - If $x^n$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
4524 /// 2^{\lfloor\log_2 |x^n|\rfloor-p+1}$.
4525 /// - If $x^n$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
4526 /// 2^{\lfloor\log_2 |x^n|\rfloor-p}$.
4527 ///
4528 /// See the [`Float::pow_u_prec_round`] documentation for information on special cases,
4529 /// overflow, and underflow.
4530 ///
4531 /// If you want to specify an output precision, consider using [`Float::pow_u_prec_round_ref`]
4532 /// instead.
4533 ///
4534 /// # Worst-case complexity
4535 /// $T(n) = O(n \log n \log\log n)$
4536 ///
4537 /// $M(n) = O(n \log n)$
4538 ///
4539 /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
4540 ///
4541 /// # Panics
4542 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the base's
4543 /// precision.
4544 ///
4545 /// # Examples
4546 /// ```
4547 /// use malachite_base::rounding_modes::RoundingMode::*;
4548 /// use malachite_float::Float;
4549 /// use std::cmp::Ordering::*;
4550 ///
4551 /// let (p, o) = (&Float::from(3)).pow_u_round_ref(5, Floor);
4552 /// assert_eq!(p.to_string(), "1.9e2");
4553 /// assert_eq!(o, Less);
4554 ///
4555 /// let (p, o) = (&Float::from(3)).pow_u_round_ref(5, Ceiling);
4556 /// assert_eq!(p.to_string(), "2.6e2");
4557 /// assert_eq!(o, Greater);
4558 /// ```
4559 #[inline]
4560 pub fn pow_u_round_ref(&self, n: u64, rm: RoundingMode) -> (Self, Ordering) {
4561 pow_u_ref(self, n, self.significant_bits(), rm)
4562 }
4563
4564 /// Raises a [`Float`] to the power of a [`u64`] in place, rounding the result to the specified
4565 /// precision and with the specified rounding mode.
4566 ///
4567 /// See the [`Float::pow_u_prec_round`] documentation for information on special cases,
4568 /// overflow, and underflow.
4569 ///
4570 /// # Worst-case complexity
4571 /// $T(n, m) = O(mn \log n \log\log n)$
4572 ///
4573 /// $M(n) = O(n \log n)$
4574 ///
4575 /// where $T$ is time, $M$ is additional memory, $n$ is `max(prec, self.significant_bits())`,
4576 /// and $m$ is the number of significant bits of the exponent `n`.
4577 ///
4578 /// # Panics
4579 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the given
4580 /// precision.
4581 ///
4582 /// # Examples
4583 /// ```
4584 /// use malachite_base::rounding_modes::RoundingMode::*;
4585 /// use malachite_float::Float;
4586 /// use std::cmp::Ordering::*;
4587 ///
4588 /// let mut x = Float::from(3);
4589 /// let o = x.pow_u_prec_round_assign(5, 20, Floor);
4590 /// assert_eq!(x.to_string(), "243.00000");
4591 /// assert_eq!(o, Equal);
4592 /// ```
4593 pub fn pow_u_prec_round_assign(&mut self, n: u64, prec: u64, rm: RoundingMode) -> Ordering {
4594 let mut x = Self::ZERO;
4595 swap(self, &mut x);
4596 let (result, o) = pow_u(x, n, prec, rm);
4597 *self = result;
4598 o
4599 }
4600
4601 /// Raises a [`Float`] to the power of a [`u64`] in place, rounding the result to the specified
4602 /// precision and to the nearest value.
4603 ///
4604 /// See the [`Float::pow_u_prec_round`] documentation for information on special cases,
4605 /// overflow, and underflow.
4606 ///
4607 /// # Worst-case complexity
4608 /// $T(n, m) = O(mn \log n \log\log n)$
4609 ///
4610 /// $M(n) = O(n \log n)$
4611 ///
4612 /// where $T$ is time, $M$ is additional memory, $n$ is `max(prec, self.significant_bits())`,
4613 /// and $m$ is the number of significant bits of the exponent `n`.
4614 ///
4615 /// # Examples
4616 /// ```
4617 /// use malachite_float::Float;
4618 /// use std::cmp::Ordering::*;
4619 ///
4620 /// let mut x = Float::from(3);
4621 /// let o = x.pow_u_prec_assign(5, 20);
4622 /// assert_eq!(x.to_string(), "243.00000");
4623 /// assert_eq!(o, Equal);
4624 /// ```
4625 #[inline]
4626 pub fn pow_u_prec_assign(&mut self, n: u64, prec: u64) -> Ordering {
4627 self.pow_u_prec_round_assign(n, prec, Nearest)
4628 }
4629
4630 /// Raises a [`Float`] to the power of a [`u64`] in place, rounding the result to the precision
4631 /// of the base and with the specified rounding mode.
4632 ///
4633 /// See the [`Float::pow_u_prec_round`] documentation for information on special cases,
4634 /// overflow, and underflow.
4635 ///
4636 /// # Worst-case complexity
4637 /// $T(n) = O(n \log n \log\log n)$
4638 ///
4639 /// $M(n) = O(n \log n)$
4640 ///
4641 /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
4642 ///
4643 /// # Panics
4644 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the base's
4645 /// precision.
4646 ///
4647 /// # Examples
4648 /// ```
4649 /// use malachite_base::rounding_modes::RoundingMode::*;
4650 /// use malachite_float::Float;
4651 /// use std::cmp::Ordering::*;
4652 ///
4653 /// let mut x = Float::from(3);
4654 /// let o = x.pow_u_round_assign(5, Floor);
4655 /// assert_eq!(x.to_string(), "1.9e2");
4656 /// assert_eq!(o, Less);
4657 /// ```
4658 #[inline]
4659 pub fn pow_u_round_assign(&mut self, n: u64, rm: RoundingMode) -> Ordering {
4660 let prec = self.significant_bits();
4661 self.pow_u_prec_round_assign(n, prec, rm)
4662 }
4663}
4664
4665impl Pow<u64> for Float {
4666 type Output = Self;
4667
4668 /// Raises a [`Float`] to the power of a [`i64`], rounding the result to the nearest value at
4669 /// the precision of the base. The [`Float`] is taken by value.
4670 ///
4671 /// If the power is equidistant from two [`Float`]s with that precision, the [`Float`] with
4672 /// fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of the
4673 /// `Nearest` rounding mode.
4674 ///
4675 /// $$
4676 /// f(x,n) = x^n+\varepsilon.
4677 /// $$
4678 /// - If $x^n$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
4679 /// - If $x^n$ is finite and nonzero, then $|\varepsilon| \leq 2^{\lfloor\log_2
4680 /// |x^n|\rfloor-p}$, where $p$ is the precision of the base.
4681 ///
4682 /// See the [`Float::pow_s_prec_round`] documentation for information on special cases,
4683 /// overflow, and underflow.
4684 ///
4685 /// If you want to specify an output precision, consider using [`Float::pow_s_prec`] instead. If
4686 /// you want to specify the output precision and the rounding mode, consider using
4687 /// [`Float::pow_s_prec_round`] instead.
4688 ///
4689 /// # Worst-case complexity
4690 /// $T(n) = O(n \log n \log\log n)$
4691 ///
4692 /// $M(n) = O(n \log n)$
4693 ///
4694 /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
4695 ///
4696 /// # Examples
4697 /// ```
4698 /// use malachite_base::num::arithmetic::traits::Pow;
4699 /// use malachite_base::num::basic::traits::Two;
4700 /// use malachite_float::Float;
4701 ///
4702 /// assert_eq!(Float::TWO.pow(10i64).to_string(), "1.0e3");
4703 /// assert_eq!(Float::from(0.5).pow(-1i64).to_string(), "2.0");
4704 /// ```
4705 #[inline]
4706 fn pow(self, n: u64) -> Self {
4707 let prec = self.significant_bits();
4708 pow_u(self, n, prec, Nearest).0
4709 }
4710}
4711
4712impl Pow<u64> for &Float {
4713 type Output = Float;
4714
4715 /// Raises a [`Float`] to the power of a [`i64`], rounding the result to the nearest value at
4716 /// the precision of the base. The [`Float`] is taken by reference.
4717 ///
4718 /// If the power is equidistant from two [`Float`]s with that precision, the [`Float`] with
4719 /// fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of the
4720 /// `Nearest` rounding mode.
4721 ///
4722 /// $$
4723 /// f(x,n) = x^n+\varepsilon.
4724 /// $$
4725 /// - If $x^n$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
4726 /// - If $x^n$ is finite and nonzero, then $|\varepsilon| \leq 2^{\lfloor\log_2
4727 /// |x^n|\rfloor-p}$, where $p$ is the precision of the base.
4728 ///
4729 /// See the [`Float::pow_s_prec_round`] documentation for information on special cases,
4730 /// overflow, and underflow.
4731 ///
4732 /// If you want to specify an output precision, consider using [`Float::pow_s_prec`] instead. If
4733 /// you want to specify the output precision and the rounding mode, consider using
4734 /// [`Float::pow_s_prec_round`] instead.
4735 ///
4736 /// # Worst-case complexity
4737 /// $T(n) = O(n \log n \log\log n)$
4738 ///
4739 /// $M(n) = O(n \log n)$
4740 ///
4741 /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
4742 ///
4743 /// # Examples
4744 /// ```
4745 /// use malachite_base::num::arithmetic::traits::Pow;
4746 /// use malachite_base::num::basic::traits::Two;
4747 /// use malachite_float::Float;
4748 ///
4749 /// assert_eq!((&Float::TWO).pow(10i64).to_string(), "1.0e3");
4750 /// assert_eq!(Float::from(0.5).pow(-1i64).to_string(), "2.0");
4751 /// ```
4752 #[inline]
4753 fn pow(self, n: u64) -> Float {
4754 pow_u_ref(self, n, self.significant_bits(), Nearest).0
4755 }
4756}
4757
4758impl PowAssign<u64> for Float {
4759 /// Raises a [`Float`] to the power of a [`i64`] in place, rounding the result to the nearest
4760 /// value at the precision of the base.
4761 ///
4762 /// See the [`Float::pow_s_prec_round`] documentation for information on special cases,
4763 /// overflow, and underflow.
4764 ///
4765 /// # Worst-case complexity
4766 /// $T(n) = O(n \log n \log\log n)$
4767 ///
4768 /// $M(n) = O(n \log n)$
4769 ///
4770 /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
4771 ///
4772 /// # Examples
4773 /// ```
4774 /// use malachite_base::num::arithmetic::traits::PowAssign;
4775 /// use malachite_base::num::basic::traits::Two;
4776 /// use malachite_float::Float;
4777 ///
4778 /// let mut x = Float::TWO;
4779 /// x.pow_assign(10i64);
4780 /// assert_eq!(x.to_string(), "1.0e3");
4781 /// ```
4782 #[inline]
4783 fn pow_assign(&mut self, n: u64) {
4784 let prec = self.significant_bits();
4785 self.pow_u_prec_assign(n, prec);
4786 }
4787}
4788
4789impl Float {
4790 /// Raises a [`Float`] to the power of a [`i64`], rounding the result to the specified precision
4791 /// and with the specified rounding mode. The [`Float`] is taken by value. An [`Ordering`] is
4792 /// also returned, indicating whether the rounded power is less than, equal to, or greater than
4793 /// the exact power. Although `NaN`s are not comparable to any [`Float`], whenever this function
4794 /// returns a `NaN` it also returns `Equal`.
4795 ///
4796 /// See [`RoundingMode`] for a description of the possible rounding modes.
4797 ///
4798 /// $$
4799 /// f(x,n,p,m) = x^n+\varepsilon.
4800 /// $$
4801 /// - If $x^n$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
4802 /// - If $x^n$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
4803 /// 2^{\lfloor\log_2 |x^n|\rfloor-p+1}$.
4804 /// - If $x^n$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
4805 /// 2^{\lfloor\log_2 |x^n|\rfloor-p}$.
4806 ///
4807 /// Special cases:
4808 /// - $f(x,0)=1.0$ for any $x$, even `NaN`
4809 /// - $f(1.0,n)=1.0$
4810 /// - $f(\text{NaN},n)=\text{NaN}$ if $n \neq 0$
4811 /// - $f(-1.0,n)=1.0$ if $n$ is even, and $-1.0$ if $n$ is odd
4812 /// - $f(\infty,n)=\infty$ if $n>0$, and $0.0$ if $n<0$
4813 /// - $f(-\infty,n)=\infty$ if $n$ is positive and even, $-\infty$ if $n$ is positive and odd,
4814 /// $0.0$ if $n$ is negative and even, and $-0.0$ if $n$ is negative and odd
4815 /// - $f(0.0,n)=0.0$ if $n>0$, and $\infty$ if $n<0$
4816 /// - $f(-0.0,n)=0.0$ if $n$ is positive and even, $-0.0$ if $n$ is positive and odd, $\infty$
4817 /// if $n$ is negative and even, and $-\infty$ if $n$ is negative and odd
4818 ///
4819 /// Overflow and underflow:
4820 /// - If $f(x,n,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
4821 /// returned instead.
4822 /// - 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}$
4823 /// is returned instead.
4824 /// - If $0<f(x,n,p,m)<2^{-2^{30}}$ and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
4825 /// - If $0<f(x,n,p,m)<2^{-2^{30}}$ and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
4826 /// instead.
4827 /// - If $0<f(x,n,p,m)\leq2^{-2^{30}-1}$ and $m$ is `Nearest`, $0.0$ is returned instead.
4828 /// - If $2^{-2^{30}-1}<f(x,n,p,m)<2^{-2^{30}}$ and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
4829 /// instead.
4830 /// - Negative results (from negative $x$ and odd $n$) mirror the bullets above, with the
4831 /// rounding directions reflected.
4832 ///
4833 /// # Worst-case complexity
4834 /// $T(n, m) = O(mn \log n \log\log n)$
4835 ///
4836 /// $M(n) = O(n \log n)$
4837 ///
4838 /// where $T$ is time, $M$ is additional memory, $n$ is `max(prec, self.significant_bits())`,
4839 /// and $m$ is the number of significant bits of the exponent `n`.
4840 ///
4841 /// # Panics
4842 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the given
4843 /// precision.
4844 ///
4845 /// # Examples
4846 /// ```
4847 /// use malachite_base::rounding_modes::RoundingMode::*;
4848 /// use malachite_float::Float;
4849 /// use std::cmp::Ordering::*;
4850 ///
4851 /// let (p, o) = Float::from(3).pow_s_prec_round(5, 20, Floor);
4852 /// assert_eq!(p.to_string(), "243.00000");
4853 /// assert_eq!(o, Equal);
4854 ///
4855 /// let (p, o) = Float::from(3).pow_s_prec_round(-2, 10, Ceiling);
4856 /// assert_eq!(p.to_string(), "0.11121");
4857 /// assert_eq!(o, Greater);
4858 /// ```
4859 #[inline]
4860 pub fn pow_s_prec_round(self, n: i64, prec: u64, rm: RoundingMode) -> (Self, Ordering) {
4861 pow_s(self, n, prec, rm)
4862 }
4863
4864 /// Raises a [`Float`] to the power of a [`i64`], rounding the result to the specified precision
4865 /// and with the specified rounding mode. The [`Float`] is taken by reference. An [`Ordering`]
4866 /// is also returned, indicating whether the rounded power is less than, equal to, or greater
4867 /// than the exact power. Although `NaN`s are not comparable to any [`Float`], whenever this
4868 /// function returns a `NaN` it also returns `Equal`.
4869 ///
4870 /// See [`RoundingMode`] for a description of the possible rounding modes.
4871 ///
4872 /// $$
4873 /// f(x,n,p,m) = x^n+\varepsilon.
4874 /// $$
4875 /// - If $x^n$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
4876 /// - If $x^n$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
4877 /// 2^{\lfloor\log_2 |x^n|\rfloor-p+1}$.
4878 /// - If $x^n$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
4879 /// 2^{\lfloor\log_2 |x^n|\rfloor-p}$.
4880 ///
4881 /// See the [`Float::pow_s_prec_round`] documentation for information on special cases,
4882 /// overflow, and underflow.
4883 ///
4884 /// # Worst-case complexity
4885 /// $T(n, m) = O(mn \log n \log\log n)$
4886 ///
4887 /// $M(n) = O(n \log n)$
4888 ///
4889 /// where $T$ is time, $M$ is additional memory, $n$ is `max(prec, self.significant_bits())`,
4890 /// and $m$ is the number of significant bits of the exponent `n`.
4891 ///
4892 /// # Panics
4893 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the given
4894 /// precision.
4895 ///
4896 /// # Examples
4897 /// ```
4898 /// use malachite_base::rounding_modes::RoundingMode::*;
4899 /// use malachite_float::Float;
4900 /// use std::cmp::Ordering::*;
4901 ///
4902 /// let (p, o) = (&Float::from(3)).pow_s_prec_round_ref(5, 20, Floor);
4903 /// assert_eq!(p.to_string(), "243.00000");
4904 /// assert_eq!(o, Equal);
4905 ///
4906 /// let (p, o) = (&Float::from(3)).pow_s_prec_round_ref(-2, 10, Ceiling);
4907 /// assert_eq!(p.to_string(), "0.11121");
4908 /// assert_eq!(o, Greater);
4909 /// ```
4910 #[inline]
4911 pub fn pow_s_prec_round_ref(&self, n: i64, prec: u64, rm: RoundingMode) -> (Self, Ordering) {
4912 pow_s_ref(self, n, prec, rm)
4913 }
4914
4915 /// Raises a [`Float`] to the power of a [`i64`], rounding the result to the specified precision
4916 /// and to the nearest value. The [`Float`] is taken by value. An [`Ordering`] is also returned,
4917 /// indicating whether the rounded power is less than, equal to, or greater than the exact
4918 /// power. Although `NaN`s are not comparable to any [`Float`], whenever this function returns a
4919 /// `NaN` it also returns `Equal`.
4920 ///
4921 /// If the power is equidistant from two [`Float`]s with the specified precision, the [`Float`]
4922 /// with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of
4923 /// the `Nearest` rounding mode.
4924 ///
4925 /// $$
4926 /// f(x,n,p) = x^n+\varepsilon.
4927 /// $$
4928 /// - If $x^n$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
4929 /// - If $x^n$ is finite and nonzero, then $|\varepsilon| \leq 2^{\lfloor\log_2
4930 /// |x^n|\rfloor-p}$.
4931 ///
4932 /// See the [`Float::pow_s_prec_round`] documentation for information on special cases,
4933 /// overflow, and underflow.
4934 ///
4935 /// If you want to use a rounding mode other than `Nearest`, consider using
4936 /// [`Float::pow_s_prec_round`] instead.
4937 ///
4938 /// # Worst-case complexity
4939 /// $T(n, m) = O(mn \log n \log\log n)$
4940 ///
4941 /// $M(n) = O(n \log n)$
4942 ///
4943 /// where $T$ is time, $M$ is additional memory, $n$ is `max(prec, self.significant_bits())`,
4944 /// and $m$ is the number of significant bits of the exponent `n`.
4945 ///
4946 /// # Examples
4947 /// ```
4948 /// use malachite_float::Float;
4949 /// use std::cmp::Ordering::*;
4950 ///
4951 /// let (p, o) = Float::from(3).pow_s_prec(5, 20);
4952 /// assert_eq!(p.to_string(), "243.00000");
4953 /// assert_eq!(o, Equal);
4954 ///
4955 /// let (p, o) = Float::from(3).pow_s_prec(-2, 10);
4956 /// assert_eq!(p.to_string(), "0.11108");
4957 /// assert_eq!(o, Less);
4958 /// ```
4959 #[inline]
4960 pub fn pow_s_prec(self, n: i64, prec: u64) -> (Self, Ordering) {
4961 pow_s(self, n, prec, Nearest)
4962 }
4963
4964 /// Raises a [`Float`] to the power of a [`i64`], rounding the result to the specified precision
4965 /// and to the nearest value. The [`Float`] is taken by reference. An [`Ordering`] is also
4966 /// returned, indicating whether the rounded power is less than, equal to, or greater than the
4967 /// exact power. Although `NaN`s are not comparable to any [`Float`], whenever this function
4968 /// returns a `NaN` it also returns `Equal`.
4969 ///
4970 /// If the power is equidistant from two [`Float`]s with the specified precision, the [`Float`]
4971 /// with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of
4972 /// the `Nearest` rounding mode.
4973 ///
4974 /// $$
4975 /// f(x,n,p) = x^n+\varepsilon.
4976 /// $$
4977 /// - If $x^n$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
4978 /// - If $x^n$ is finite and nonzero, then $|\varepsilon| \leq 2^{\lfloor\log_2
4979 /// |x^n|\rfloor-p}$.
4980 ///
4981 /// See the [`Float::pow_s_prec_round`] documentation for information on special cases,
4982 /// overflow, and underflow.
4983 ///
4984 /// If you want to use a rounding mode other than `Nearest`, consider using
4985 /// [`Float::pow_s_prec_round_ref`] instead.
4986 ///
4987 /// # Worst-case complexity
4988 /// $T(n, m) = O(mn \log n \log\log n)$
4989 ///
4990 /// $M(n) = O(n \log n)$
4991 ///
4992 /// where $T$ is time, $M$ is additional memory, $n$ is `max(prec, self.significant_bits())`,
4993 /// and $m$ is the number of significant bits of the exponent `n`.
4994 ///
4995 /// # Examples
4996 /// ```
4997 /// use malachite_float::Float;
4998 /// use std::cmp::Ordering::*;
4999 ///
5000 /// let (p, o) = (&Float::from(3)).pow_s_prec_ref(5, 20);
5001 /// assert_eq!(p.to_string(), "243.00000");
5002 /// assert_eq!(o, Equal);
5003 ///
5004 /// let (p, o) = (&Float::from(3)).pow_s_prec_ref(-2, 10);
5005 /// assert_eq!(p.to_string(), "0.11108");
5006 /// assert_eq!(o, Less);
5007 /// ```
5008 #[inline]
5009 pub fn pow_s_prec_ref(&self, n: i64, prec: u64) -> (Self, Ordering) {
5010 pow_s_ref(self, n, prec, Nearest)
5011 }
5012
5013 /// Raises a [`Float`] to the power of a [`i64`], rounding the result to the precision of the
5014 /// base and with the specified rounding mode. The [`Float`] is taken by value. An [`Ordering`]
5015 /// is also returned, indicating whether the rounded power is less than, equal to, or greater
5016 /// than the exact power. Although `NaN`s are not comparable to any [`Float`], whenever this
5017 /// function returns a `NaN` it also returns `Equal`.
5018 ///
5019 /// The output precision is the precision of `self`. See [`RoundingMode`] for a description of
5020 /// the possible rounding modes.
5021 ///
5022 /// $$
5023 /// f(x,n,p,m) = x^n+\varepsilon.
5024 /// $$
5025 /// - If $x^n$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
5026 /// - If $x^n$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
5027 /// 2^{\lfloor\log_2 |x^n|\rfloor-p+1}$.
5028 /// - If $x^n$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
5029 /// 2^{\lfloor\log_2 |x^n|\rfloor-p}$.
5030 ///
5031 /// See the [`Float::pow_s_prec_round`] documentation for information on special cases,
5032 /// overflow, and underflow.
5033 ///
5034 /// If you want to specify an output precision, consider using [`Float::pow_s_prec_round`]
5035 /// instead.
5036 ///
5037 /// # Worst-case complexity
5038 /// $T(n) = O(n \log n \log\log n)$
5039 ///
5040 /// $M(n) = O(n \log n)$
5041 ///
5042 /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
5043 ///
5044 /// # Panics
5045 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the base's
5046 /// precision.
5047 ///
5048 /// # Examples
5049 /// ```
5050 /// use malachite_base::rounding_modes::RoundingMode::*;
5051 /// use malachite_float::Float;
5052 /// use std::cmp::Ordering::*;
5053 ///
5054 /// let (p, o) = Float::from(3).pow_s_round(5, Floor);
5055 /// assert_eq!(p.to_string(), "1.9e2");
5056 /// assert_eq!(o, Less);
5057 ///
5058 /// let (p, o) = Float::from(3).pow_s_round(5, Ceiling);
5059 /// assert_eq!(p.to_string(), "2.6e2");
5060 /// assert_eq!(o, Greater);
5061 /// ```
5062 #[inline]
5063 pub fn pow_s_round(self, n: i64, rm: RoundingMode) -> (Self, Ordering) {
5064 let prec = self.significant_bits();
5065 pow_s(self, n, prec, rm)
5066 }
5067
5068 /// Raises a [`Float`] to the power of a [`i64`], rounding the result to the precision of the
5069 /// base and with the specified rounding mode. The [`Float`] is taken by reference. An
5070 /// [`Ordering`] is also returned, indicating whether the rounded power is less than, equal to,
5071 /// or greater than the exact power. Although `NaN`s are not comparable to any [`Float`],
5072 /// whenever this function returns a `NaN` it also returns `Equal`.
5073 ///
5074 /// The output precision is the precision of `self`. See [`RoundingMode`] for a description of
5075 /// the possible rounding modes.
5076 ///
5077 /// $$
5078 /// f(x,n,p,m) = x^n+\varepsilon.
5079 /// $$
5080 /// - If $x^n$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
5081 /// - If $x^n$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
5082 /// 2^{\lfloor\log_2 |x^n|\rfloor-p+1}$.
5083 /// - If $x^n$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
5084 /// 2^{\lfloor\log_2 |x^n|\rfloor-p}$.
5085 ///
5086 /// See the [`Float::pow_s_prec_round`] documentation for information on special cases,
5087 /// overflow, and underflow.
5088 ///
5089 /// If you want to specify an output precision, consider using [`Float::pow_s_prec_round_ref`]
5090 /// instead.
5091 ///
5092 /// # Worst-case complexity
5093 /// $T(n) = O(n \log n \log\log n)$
5094 ///
5095 /// $M(n) = O(n \log n)$
5096 ///
5097 /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
5098 ///
5099 /// # Panics
5100 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the base's
5101 /// precision.
5102 ///
5103 /// # Examples
5104 /// ```
5105 /// use malachite_base::rounding_modes::RoundingMode::*;
5106 /// use malachite_float::Float;
5107 /// use std::cmp::Ordering::*;
5108 ///
5109 /// let (p, o) = (&Float::from(3)).pow_s_round_ref(5, Floor);
5110 /// assert_eq!(p.to_string(), "1.9e2");
5111 /// assert_eq!(o, Less);
5112 ///
5113 /// let (p, o) = (&Float::from(3)).pow_s_round_ref(5, Ceiling);
5114 /// assert_eq!(p.to_string(), "2.6e2");
5115 /// assert_eq!(o, Greater);
5116 /// ```
5117 #[inline]
5118 pub fn pow_s_round_ref(&self, n: i64, rm: RoundingMode) -> (Self, Ordering) {
5119 pow_s_ref(self, n, self.significant_bits(), rm)
5120 }
5121
5122 /// Raises a [`Float`] to the power of a [`i64`] in place, rounding the result to the specified
5123 /// precision and with the specified rounding mode.
5124 ///
5125 /// See the [`Float::pow_s_prec_round`] documentation for information on special cases,
5126 /// overflow, and underflow.
5127 ///
5128 /// # Worst-case complexity
5129 /// $T(n, m) = O(mn \log n \log\log n)$
5130 ///
5131 /// $M(n) = O(n \log n)$
5132 ///
5133 /// where $T$ is time, $M$ is additional memory, $n$ is `max(prec, self.significant_bits())`,
5134 /// and $m$ is the number of significant bits of the exponent `n`.
5135 ///
5136 /// # Panics
5137 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the given
5138 /// precision.
5139 ///
5140 /// # Examples
5141 /// ```
5142 /// use malachite_base::rounding_modes::RoundingMode::*;
5143 /// use malachite_float::Float;
5144 /// use std::cmp::Ordering::*;
5145 ///
5146 /// let mut x = Float::from(3);
5147 /// let o = x.pow_s_prec_round_assign(5, 20, Floor);
5148 /// assert_eq!(x.to_string(), "243.00000");
5149 /// assert_eq!(o, Equal);
5150 /// ```
5151 pub fn pow_s_prec_round_assign(&mut self, n: i64, prec: u64, rm: RoundingMode) -> Ordering {
5152 let mut x = Self::ZERO;
5153 swap(self, &mut x);
5154 let (result, o) = pow_s(x, n, prec, rm);
5155 *self = result;
5156 o
5157 }
5158
5159 /// Raises a [`Float`] to the power of a [`i64`] in place, rounding the result to the specified
5160 /// precision and to the nearest value.
5161 ///
5162 /// See the [`Float::pow_s_prec_round`] documentation for information on special cases,
5163 /// overflow, and underflow.
5164 ///
5165 /// # Worst-case complexity
5166 /// $T(n, m) = O(mn \log n \log\log n)$
5167 ///
5168 /// $M(n) = O(n \log n)$
5169 ///
5170 /// where $T$ is time, $M$ is additional memory, $n$ is `max(prec, self.significant_bits())`,
5171 /// and $m$ is the number of significant bits of the exponent `n`.
5172 ///
5173 /// # Examples
5174 /// ```
5175 /// use malachite_float::Float;
5176 /// use std::cmp::Ordering::*;
5177 ///
5178 /// let mut x = Float::from(3);
5179 /// let o = x.pow_s_prec_assign(5, 20);
5180 /// assert_eq!(x.to_string(), "243.00000");
5181 /// assert_eq!(o, Equal);
5182 /// ```
5183 #[inline]
5184 pub fn pow_s_prec_assign(&mut self, n: i64, prec: u64) -> Ordering {
5185 self.pow_s_prec_round_assign(n, prec, Nearest)
5186 }
5187
5188 /// Raises a [`Float`] to the power of a [`i64`] in place, rounding the result to the precision
5189 /// of the base and with the specified rounding mode.
5190 ///
5191 /// See the [`Float::pow_s_prec_round`] documentation for information on special cases,
5192 /// overflow, and underflow.
5193 ///
5194 /// # Worst-case complexity
5195 /// $T(n) = O(n \log n \log\log n)$
5196 ///
5197 /// $M(n) = O(n \log n)$
5198 ///
5199 /// where $T$ is time, $M$ is additional memory, and $n$ is `self.significant_bits()`.
5200 ///
5201 /// # Panics
5202 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the base's
5203 /// precision.
5204 ///
5205 /// # Examples
5206 /// ```
5207 /// use malachite_base::rounding_modes::RoundingMode::*;
5208 /// use malachite_float::Float;
5209 /// use std::cmp::Ordering::*;
5210 ///
5211 /// let mut x = Float::from(3);
5212 /// let o = x.pow_s_round_assign(5, Floor);
5213 /// assert_eq!(x.to_string(), "1.9e2");
5214 /// assert_eq!(o, Less);
5215 /// ```
5216 #[inline]
5217 pub fn pow_s_round_assign(&mut self, n: i64, rm: RoundingMode) -> Ordering {
5218 let prec = self.significant_bits();
5219 self.pow_s_prec_round_assign(n, prec, rm)
5220 }
5221}
5222
5223impl Pow<i64> for Float {
5224 type Output = Self;
5225
5226 /// Raises a [`Float`] to an [`i64`] power, rounding the result to the nearest value at the
5227 /// precision of the base. The [`Float`] is taken by value.
5228 #[inline]
5229 fn pow(self, n: i64) -> Self {
5230 let prec = self.significant_bits();
5231 pow_s(self, n, prec, Nearest).0
5232 }
5233}
5234
5235impl Pow<i64> for &Float {
5236 type Output = Float;
5237
5238 /// Raises a [`Float`] to an [`i64`] power, rounding the result to the nearest value at the
5239 /// precision of the base. The [`Float`] is taken by reference.
5240 #[inline]
5241 fn pow(self, n: i64) -> Float {
5242 pow_s_ref(self, n, self.significant_bits(), Nearest).0
5243 }
5244}
5245
5246impl PowAssign<i64> for Float {
5247 /// Raises a [`Float`] to an [`i64`] power in place, rounding the result to the nearest value at
5248 /// the precision of the base.
5249 #[inline]
5250 fn pow_assign(&mut self, n: i64) {
5251 let prec = self.significant_bits();
5252 self.pow_s_prec_assign(n, prec);
5253 }
5254}
5255
5256impl Float {
5257 /// Raises a [`u64`] to the power of a [`u64`], returning a [`Float`] rounded to the specified
5258 /// precision and with the specified rounding mode. An [`Ordering`] is also returned, indicating
5259 /// whether the rounded power is less than, equal to, or greater than the exact power.
5260 ///
5261 /// See [`RoundingMode`] for a description of the possible rounding modes.
5262 ///
5263 /// $$
5264 /// f(x,y,p,m) = x^y+\varepsilon.
5265 /// $$
5266 /// - If $x^y$ is zero, $\varepsilon$ may be ignored or assumed to be 0.
5267 /// - If $x^y$ is nonzero, and $m$ is not `Nearest`, then $|\varepsilon| < 2^{\lfloor\log_2
5268 /// x^y\rfloor-p+1}$.
5269 /// - If $x^y$ is nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq 2^{\lfloor\log_2
5270 /// x^y\rfloor-p}$.
5271 ///
5272 /// The result is always nonnegative, so it never underflows.
5273 ///
5274 /// Special cases:
5275 /// - $f(x,0,p,m)=1.0$ for any $x$
5276 /// - $f(0,y,p,m)=0.0$ if $y>0$
5277 /// - $f(1,y,p,m)=1.0$
5278 ///
5279 /// Overflow:
5280 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
5281 /// returned instead.
5282 /// - 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}$
5283 /// is returned instead.
5284 ///
5285 /// # Worst-case complexity
5286 /// $T(n, m) = O(mn \log n \log\log n)$
5287 ///
5288 /// $M(n) = O(n \log n)$
5289 ///
5290 /// where $T$ is time, $M$ is additional memory, $n$ is `prec`, and $m$ is the number of
5291 /// significant bits of the exponent.
5292 ///
5293 /// # Panics
5294 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the given
5295 /// precision.
5296 ///
5297 /// # Examples
5298 /// ```
5299 /// use malachite_base::rounding_modes::RoundingMode::*;
5300 /// use malachite_float::Float;
5301 /// use std::cmp::Ordering::*;
5302 ///
5303 /// let (p, o) = Float::unsigned_pow_unsigned_prec_round(3, 5, 20, Floor);
5304 /// assert_eq!(p.to_string(), "243.00000");
5305 /// assert_eq!(o, Equal);
5306 ///
5307 /// let (p, o) = Float::unsigned_pow_unsigned_prec_round(3, 5, 2, Ceiling);
5308 /// assert_eq!(p.to_string(), "2.6e2");
5309 /// assert_eq!(o, Greater);
5310 /// ```
5311 #[inline]
5312 pub fn unsigned_pow_unsigned_prec_round(
5313 x: u64,
5314 y: u64,
5315 prec: u64,
5316 rm: RoundingMode,
5317 ) -> (Self, Ordering) {
5318 unsigned_pow_unsigned(x, y, prec, rm)
5319 }
5320
5321 /// Raises a [`u64`] to the power of a [`u64`], returning a [`Float`] rounded to the specified
5322 /// precision and to the nearest value. An [`Ordering`] is also returned, indicating whether the
5323 /// rounded power is less than, equal to, or greater than the exact power.
5324 ///
5325 /// If the power is equidistant from two [`Float`]s with the specified precision, the [`Float`]
5326 /// with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of
5327 /// the `Nearest` rounding mode.
5328 ///
5329 /// $$
5330 /// f(x,y,p) = x^y+\varepsilon.
5331 /// $$
5332 /// - If $x^y$ is zero, $\varepsilon$ may be ignored or assumed to be 0.
5333 /// - If $x^y$ is nonzero, then $|\varepsilon| \leq 2^{\lfloor\log_2 x^y\rfloor-p}$.
5334 ///
5335 /// See the [`Float::unsigned_pow_unsigned_prec_round`] documentation for information on special
5336 /// cases and overflow.
5337 ///
5338 /// If you want to use a rounding mode other than `Nearest`, consider using
5339 /// [`Float::unsigned_pow_unsigned_prec_round`] instead.
5340 ///
5341 /// # Worst-case complexity
5342 /// $T(n, m) = O(mn \log n \log\log n)$
5343 ///
5344 /// $M(n) = O(n \log n)$
5345 ///
5346 /// where $T$ is time, $M$ is additional memory, $n$ is `prec`, and $m$ is the number of
5347 /// significant bits of the exponent.
5348 ///
5349 /// # Examples
5350 /// ```
5351 /// use malachite_float::Float;
5352 /// use std::cmp::Ordering::*;
5353 ///
5354 /// let (p, o) = Float::unsigned_pow_unsigned_prec(3, 5, 20);
5355 /// assert_eq!(p.to_string(), "243.00000");
5356 /// assert_eq!(o, Equal);
5357 ///
5358 /// let (p, o) = Float::unsigned_pow_unsigned_prec(3, 5, 2);
5359 /// assert_eq!(p.to_string(), "2.6e2");
5360 /// assert_eq!(o, Greater);
5361 /// ```
5362 #[inline]
5363 pub fn unsigned_pow_unsigned_prec(x: u64, y: u64, prec: u64) -> (Self, Ordering) {
5364 unsigned_pow_unsigned(x, y, prec, Nearest)
5365 }
5366
5367 /// Raises a [`u64`] to the power of a [`Float`], returning a [`Float`] rounded to the specified
5368 /// precision and with the specified rounding mode. The [`Float`] exponent is taken by value. An
5369 /// [`Ordering`] is also returned, indicating whether the rounded power is less than, equal to,
5370 /// or greater than the exact power. Although `NaN`s are not comparable to any [`Float`],
5371 /// whenever this function returns a `NaN` it also returns `Equal`.
5372 ///
5373 /// See [`RoundingMode`] for a description of the possible rounding modes.
5374 ///
5375 /// $$
5376 /// f(x,y,p,m) = x^y+\varepsilon.
5377 /// $$
5378 /// - If $x^y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
5379 /// - If $x^y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
5380 /// 2^{\lfloor\log_2 x^y\rfloor-p+1}$.
5381 /// - If $x^y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
5382 /// 2^{\lfloor\log_2 x^y\rfloor-p}$.
5383 ///
5384 /// Special cases:
5385 /// - $f(x,0.0,p,m)=1.0$ for any $x$
5386 /// - $f(1,y,p,m)=1.0$ for any $y$, even `NaN`
5387 /// - $f(x,\text{NaN},p,m)=\text{NaN}$ if $x \neq 1$
5388 /// - $f(x,\infty,p,m)=\infty$ if $x>1$, and $0.0$ if $x=0$
5389 /// - $f(x,-\infty,p,m)=0.0$ if $x>1$, and $\infty$ if $x=0$
5390 /// - $f(0,y,p,m)=0.0$ if $y>0$, and $\infty$ if $y<0$
5391 ///
5392 /// Overflow and underflow:
5393 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
5394 /// returned instead.
5395 /// - 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}$
5396 /// is returned instead.
5397 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
5398 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
5399 /// instead.
5400 /// - If $0<f(x,y,p,m)\leq2^{-2^{30}-1}$ and $m$ is `Nearest`, $0.0$ is returned instead.
5401 /// - If $2^{-2^{30}-1}<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
5402 /// instead.
5403 ///
5404 /// # Worst-case complexity
5405 /// $T(n) = O(n^{3/2} \log n \log\log n)$
5406 ///
5407 /// $M(n) = O(n \log n)$
5408 ///
5409 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(prec, y.significant_bits())`.
5410 ///
5411 /// # Panics
5412 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the given
5413 /// precision.
5414 ///
5415 /// # Examples
5416 /// ```
5417 /// use malachite_base::rounding_modes::RoundingMode::*;
5418 /// use malachite_float::Float;
5419 /// use std::cmp::Ordering::*;
5420 ///
5421 /// let (p, o) = Float::unsigned_pow_prec_round(2, Float::from(0.5), 53, Nearest);
5422 /// assert_eq!(p.to_string(), "1.4142135623730951");
5423 /// assert_eq!(o, Greater);
5424 ///
5425 /// let (p, o) = Float::unsigned_pow_prec_round(3, Float::from(2.5), 53, Floor);
5426 /// assert_eq!(p.to_string(), "15.588457268119894");
5427 /// assert_eq!(o, Less);
5428 /// ```
5429 ///
5430 /// This is equivalent to `mpfr_ui_pow` from `ui_pow.c`, MPFR 4.3.0, which likewise converts the
5431 /// integer exactly and delegates to `mpfr_pow`.
5432 #[inline]
5433 pub fn unsigned_pow_prec_round(
5434 x: u64,
5435 y: Self,
5436 prec: u64,
5437 rm: RoundingMode,
5438 ) -> (Self, Ordering) {
5439 Self::from(x).pow_prec_round(y, prec, rm)
5440 }
5441
5442 /// Raises a [`u64`] to the power of a [`Float`], returning a [`Float`] rounded to the specified
5443 /// precision and with the specified rounding mode. The [`Float`] exponent is taken by
5444 /// reference. An [`Ordering`] is also returned, indicating whether the rounded power is less
5445 /// than, equal to, or greater than the exact power. Although `NaN`s are not comparable to any
5446 /// [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
5447 ///
5448 /// See [`RoundingMode`] for a description of the possible rounding modes.
5449 ///
5450 /// $$
5451 /// f(x,y,p,m) = x^y+\varepsilon.
5452 /// $$
5453 /// - If $x^y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
5454 /// - If $x^y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
5455 /// 2^{\lfloor\log_2 x^y\rfloor-p+1}$.
5456 /// - If $x^y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
5457 /// 2^{\lfloor\log_2 x^y\rfloor-p}$.
5458 ///
5459 /// See the [`Float::unsigned_pow_prec_round`] documentation for information on special cases,
5460 /// overflow, and underflow.
5461 ///
5462 /// # Worst-case complexity
5463 /// $T(n) = O(n^{3/2} \log n \log\log n)$
5464 ///
5465 /// $M(n) = O(n \log n)$
5466 ///
5467 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(prec, y.significant_bits())`.
5468 ///
5469 /// # Panics
5470 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the given
5471 /// precision.
5472 ///
5473 /// # Examples
5474 /// ```
5475 /// use malachite_base::rounding_modes::RoundingMode::*;
5476 /// use malachite_float::Float;
5477 /// use std::cmp::Ordering::*;
5478 ///
5479 /// let (p, o) = Float::unsigned_pow_prec_round_ref(2, &Float::from(0.5), 53, Nearest);
5480 /// assert_eq!(p.to_string(), "1.4142135623730951");
5481 /// assert_eq!(o, Greater);
5482 ///
5483 /// let (p, o) = Float::unsigned_pow_prec_round_ref(3, &Float::from(2.5), 53, Floor);
5484 /// assert_eq!(p.to_string(), "15.588457268119894");
5485 /// assert_eq!(o, Less);
5486 /// ```
5487 #[inline]
5488 pub fn unsigned_pow_prec_round_ref(
5489 x: u64,
5490 y: &Self,
5491 prec: u64,
5492 rm: RoundingMode,
5493 ) -> (Self, Ordering) {
5494 Self::from(x).pow_prec_round_val_ref(y, prec, rm)
5495 }
5496
5497 /// Raises a [`u64`] to the power of a [`Float`], returning a [`Float`] rounded to the specified
5498 /// precision and to the nearest value. The [`Float`] exponent is taken by value. An
5499 /// [`Ordering`] is also returned, indicating whether the rounded power is less than, equal to,
5500 /// or greater than the exact power. Although `NaN`s are not comparable to any [`Float`],
5501 /// whenever this function returns a `NaN` it also returns `Equal`.
5502 ///
5503 /// If the power is equidistant from two [`Float`]s with the specified precision, the [`Float`]
5504 /// with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of
5505 /// the `Nearest` rounding mode.
5506 ///
5507 /// $$
5508 /// f(x,y,p) = x^y+\varepsilon.
5509 /// $$
5510 /// - If $x^y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
5511 /// - If $x^y$ is finite and nonzero, then $|\varepsilon| \leq 2^{\lfloor\log_2 x^y\rfloor-p}$.
5512 ///
5513 /// See the [`Float::unsigned_pow_prec_round`] documentation for information on special cases,
5514 /// overflow, and underflow.
5515 ///
5516 /// If you want to use a rounding mode other than `Nearest`, consider using
5517 /// [`Float::unsigned_pow_prec_round`] instead.
5518 ///
5519 /// # Worst-case complexity
5520 /// $T(n) = O(n^{3/2} \log n \log\log n)$
5521 ///
5522 /// $M(n) = O(n \log n)$
5523 ///
5524 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(prec, y.significant_bits())`.
5525 ///
5526 /// # Examples
5527 /// ```
5528 /// use malachite_float::Float;
5529 /// use std::cmp::Ordering::*;
5530 ///
5531 /// let (p, o) = Float::unsigned_pow_prec(2, Float::from(0.5), 53);
5532 /// assert_eq!(p.to_string(), "1.4142135623730951");
5533 /// assert_eq!(o, Greater);
5534 ///
5535 /// let (p, o) = Float::unsigned_pow_prec(3, Float::from(2.5), 53);
5536 /// assert_eq!(p.to_string(), "15.588457268119896");
5537 /// assert_eq!(o, Greater);
5538 /// ```
5539 #[inline]
5540 pub fn unsigned_pow_prec(x: u64, y: Self, prec: u64) -> (Self, Ordering) {
5541 Self::unsigned_pow_prec_round(x, y, prec, Nearest)
5542 }
5543
5544 /// Raises a [`u64`] to the power of a [`Float`], returning a [`Float`] rounded to the specified
5545 /// precision and to the nearest value. The [`Float`] exponent is taken by reference. An
5546 /// [`Ordering`] is also returned, indicating whether the rounded power is less than, equal to,
5547 /// or greater than the exact power. Although `NaN`s are not comparable to any [`Float`],
5548 /// whenever this function returns a `NaN` it also returns `Equal`.
5549 ///
5550 /// If the power is equidistant from two [`Float`]s with the specified precision, the [`Float`]
5551 /// with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of
5552 /// the `Nearest` rounding mode.
5553 ///
5554 /// $$
5555 /// f(x,y,p) = x^y+\varepsilon.
5556 /// $$
5557 /// - If $x^y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
5558 /// - If $x^y$ is finite and nonzero, then $|\varepsilon| \leq 2^{\lfloor\log_2 x^y\rfloor-p}$.
5559 ///
5560 /// See the [`Float::unsigned_pow_prec_round`] documentation for information on special cases,
5561 /// overflow, and underflow.
5562 ///
5563 /// If you want to use a rounding mode other than `Nearest`, consider using
5564 /// [`Float::unsigned_pow_prec_round_ref`] instead.
5565 ///
5566 /// # Worst-case complexity
5567 /// $T(n) = O(n^{3/2} \log n \log\log n)$
5568 ///
5569 /// $M(n) = O(n \log n)$
5570 ///
5571 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(prec, y.significant_bits())`.
5572 ///
5573 /// # Examples
5574 /// ```
5575 /// use malachite_float::Float;
5576 /// use std::cmp::Ordering::*;
5577 ///
5578 /// let (p, o) = Float::unsigned_pow_prec_ref(2, &Float::from(0.5), 53);
5579 /// assert_eq!(p.to_string(), "1.4142135623730951");
5580 /// assert_eq!(o, Greater);
5581 ///
5582 /// let (p, o) = Float::unsigned_pow_prec_ref(3, &Float::from(2.5), 53);
5583 /// assert_eq!(p.to_string(), "15.588457268119896");
5584 /// assert_eq!(o, Greater);
5585 /// ```
5586 #[inline]
5587 pub fn unsigned_pow_prec_ref(x: u64, y: &Self, prec: u64) -> (Self, Ordering) {
5588 Self::unsigned_pow_prec_round_ref(x, y, prec, Nearest)
5589 }
5590
5591 /// Raises a [`u64`] to the power of a [`Rational`], returning a [`Float`] rounded to the
5592 /// specified precision and with the specified rounding mode. The [`Rational`] exponent is taken
5593 /// by value. An [`Ordering`] is also returned, indicating whether the rounded power is less
5594 /// than, equal to, or greater than the exact power.
5595 ///
5596 /// See [`RoundingMode`] for a description of the possible rounding modes.
5597 ///
5598 /// $$
5599 /// f(x,y,p,m) = x^y+\varepsilon.
5600 /// $$
5601 /// - If $x^y$ is zero or infinite, $\varepsilon$ may be ignored or assumed to be 0.
5602 /// - If $x^y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
5603 /// 2^{\lfloor\log_2 x^y\rfloor-p+1}$.
5604 /// - If $x^y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
5605 /// 2^{\lfloor\log_2 x^y\rfloor-p}$.
5606 ///
5607 /// Special cases:
5608 /// - $f(x,0,p,m)=1.0$ for any $x$
5609 /// - $f(1,y,p,m)=1.0$ for any $y$
5610 /// - $f(0,y,p,m)=0.0$ if $y>0$, and $\infty$ if $y<0$
5611 ///
5612 /// Overflow and underflow:
5613 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
5614 /// returned instead.
5615 /// - 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}$
5616 /// is returned instead.
5617 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
5618 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
5619 /// instead.
5620 /// - If $0<f(x,y,p,m)\leq2^{-2^{30}-1}$ and $m$ is `Nearest`, $0.0$ is returned instead.
5621 /// - If $2^{-2^{30}-1}<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
5622 /// instead.
5623 ///
5624 /// # Worst-case complexity
5625 /// $T(n) = O(n^{3/2} \log n \log\log n)$
5626 ///
5627 /// $M(n) = O(n \log n)$
5628 ///
5629 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(prec, y.significant_bits())`.
5630 ///
5631 /// # Panics
5632 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the given
5633 /// precision.
5634 ///
5635 /// # Examples
5636 /// ```
5637 /// use malachite_base::num::basic::traits::OneHalf;
5638 /// use malachite_base::rounding_modes::RoundingMode::*;
5639 /// use malachite_float::Float;
5640 /// use malachite_q::Rational;
5641 /// use std::cmp::Ordering::*;
5642 ///
5643 /// let (p, o) =
5644 /// Float::unsigned_pow_rational_prec_round(8, Rational::from_signeds(1, 3), 20, Floor);
5645 /// assert_eq!(p.to_string(), "2.0000000");
5646 /// assert_eq!(o, Equal);
5647 ///
5648 /// let (p, o) = Float::unsigned_pow_rational_prec_round(3, Rational::ONE_HALF, 2, Floor);
5649 /// assert_eq!(p.to_string(), "1.5");
5650 /// assert_eq!(o, Less);
5651 /// ```
5652 #[allow(clippy::needless_pass_by_value)]
5653 #[inline]
5654 pub fn unsigned_pow_rational_prec_round(
5655 x: u64,
5656 y: Rational,
5657 prec: u64,
5658 rm: RoundingMode,
5659 ) -> (Self, Ordering) {
5660 unsigned_pow_rational(x, &y, prec, rm)
5661 }
5662
5663 /// Raises a [`u64`] to the power of a [`Rational`], returning a [`Float`] rounded to the
5664 /// specified precision and with the specified rounding mode. The [`Rational`] exponent is taken
5665 /// by reference. An [`Ordering`] is also returned, indicating whether the rounded power is less
5666 /// than, equal to, or greater than the exact power.
5667 ///
5668 /// See [`RoundingMode`] for a description of the possible rounding modes.
5669 ///
5670 /// $$
5671 /// f(x,y,p,m) = x^y+\varepsilon.
5672 /// $$
5673 /// - If $x^y$ is zero or infinite, $\varepsilon$ may be ignored or assumed to be 0.
5674 /// - If $x^y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
5675 /// 2^{\lfloor\log_2 x^y\rfloor-p+1}$.
5676 /// - If $x^y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
5677 /// 2^{\lfloor\log_2 x^y\rfloor-p}$.
5678 ///
5679 /// See the [`Float::unsigned_pow_rational_prec_round`] documentation for information on special
5680 /// cases, overflow, and underflow.
5681 ///
5682 /// # Worst-case complexity
5683 /// $T(n) = O(n^{3/2} \log n \log\log n)$
5684 ///
5685 /// $M(n) = O(n \log n)$
5686 ///
5687 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(prec, y.significant_bits())`.
5688 ///
5689 /// # Panics
5690 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the given
5691 /// precision.
5692 ///
5693 /// # Examples
5694 /// ```
5695 /// use malachite_base::num::basic::traits::OneHalf;
5696 /// use malachite_base::rounding_modes::RoundingMode::*;
5697 /// use malachite_float::Float;
5698 /// use malachite_q::Rational;
5699 /// use std::cmp::Ordering::*;
5700 ///
5701 /// let (p, o) = Float::unsigned_pow_rational_prec_round_ref(
5702 /// 8,
5703 /// &Rational::from_signeds(1, 3),
5704 /// 20,
5705 /// Floor,
5706 /// );
5707 /// assert_eq!(p.to_string(), "2.0000000");
5708 /// assert_eq!(o, Equal);
5709 ///
5710 /// let (p, o) =
5711 /// Float::unsigned_pow_rational_prec_round_ref(3, &Rational::ONE_HALF, 2, Ceiling);
5712 /// assert_eq!(p.to_string(), "2.0");
5713 /// assert_eq!(o, Greater);
5714 /// ```
5715 #[inline]
5716 pub fn unsigned_pow_rational_prec_round_ref(
5717 x: u64,
5718 y: &Rational,
5719 prec: u64,
5720 rm: RoundingMode,
5721 ) -> (Self, Ordering) {
5722 unsigned_pow_rational(x, y, prec, rm)
5723 }
5724
5725 /// Raises a [`u64`] to the power of a [`Rational`], returning a [`Float`] rounded to the
5726 /// specified precision and to the nearest value. The [`Rational`] exponent is taken by value.
5727 /// An [`Ordering`] is also returned, indicating whether the rounded power is less than, equal
5728 /// to, or greater than the exact power.
5729 ///
5730 /// If the power is equidistant from two [`Float`]s with the specified precision, the [`Float`]
5731 /// with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of
5732 /// the `Nearest` rounding mode.
5733 ///
5734 /// $$
5735 /// f(x,y,p) = x^y+\varepsilon.
5736 /// $$
5737 /// - If $x^y$ is zero or infinite, $\varepsilon$ may be ignored or assumed to be 0.
5738 /// - If $x^y$ is finite and nonzero, then $|\varepsilon| \leq 2^{\lfloor\log_2 x^y\rfloor-p}$.
5739 ///
5740 /// See the [`Float::unsigned_pow_rational_prec_round`] documentation for information on special
5741 /// cases, overflow, and underflow.
5742 ///
5743 /// If you want to use a rounding mode other than `Nearest`, consider using
5744 /// [`Float::unsigned_pow_rational_prec_round`] instead.
5745 ///
5746 /// # Worst-case complexity
5747 /// $T(n) = O(n^{3/2} \log n \log\log n)$
5748 ///
5749 /// $M(n) = O(n \log n)$
5750 ///
5751 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(prec, y.significant_bits())`.
5752 ///
5753 /// # Examples
5754 /// ```
5755 /// use malachite_base::num::basic::traits::OneHalf;
5756 /// use malachite_float::Float;
5757 /// use malachite_q::Rational;
5758 /// use std::cmp::Ordering::*;
5759 ///
5760 /// let (p, o) = Float::unsigned_pow_rational_prec(8, Rational::from_signeds(1, 3), 20);
5761 /// assert_eq!(p.to_string(), "2.0000000");
5762 /// assert_eq!(o, Equal);
5763 ///
5764 /// let (p, o) = Float::unsigned_pow_rational_prec(3, Rational::ONE_HALF, 53);
5765 /// assert_eq!(p.to_string(), "1.7320508075688772");
5766 /// assert_eq!(o, Less);
5767 /// ```
5768 #[inline]
5769 #[allow(clippy::needless_pass_by_value)]
5770 pub fn unsigned_pow_rational_prec(x: u64, y: Rational, prec: u64) -> (Self, Ordering) {
5771 unsigned_pow_rational(x, &y, prec, Nearest)
5772 }
5773
5774 /// Raises a [`u64`] to the power of a [`Rational`], returning a [`Float`] rounded to the
5775 /// specified precision and to the nearest value. The [`Rational`] exponent is taken by
5776 /// reference. An [`Ordering`] is also returned, indicating whether the rounded power is less
5777 /// than, equal to, or greater than the exact power.
5778 ///
5779 /// If the power is equidistant from two [`Float`]s with the specified precision, the [`Float`]
5780 /// with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of
5781 /// the `Nearest` rounding mode.
5782 ///
5783 /// $$
5784 /// f(x,y,p) = x^y+\varepsilon.
5785 /// $$
5786 /// - If $x^y$ is zero or infinite, $\varepsilon$ may be ignored or assumed to be 0.
5787 /// - If $x^y$ is finite and nonzero, then $|\varepsilon| \leq 2^{\lfloor\log_2 x^y\rfloor-p}$.
5788 ///
5789 /// See the [`Float::unsigned_pow_rational_prec_round`] documentation for information on special
5790 /// cases, overflow, and underflow.
5791 ///
5792 /// If you want to use a rounding mode other than `Nearest`, consider using
5793 /// [`Float::unsigned_pow_rational_prec_round_ref`] instead.
5794 ///
5795 /// # Worst-case complexity
5796 /// $T(n) = O(n^{3/2} \log n \log\log n)$
5797 ///
5798 /// $M(n) = O(n \log n)$
5799 ///
5800 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(prec, y.significant_bits())`.
5801 ///
5802 /// # Examples
5803 /// ```
5804 /// use malachite_base::num::basic::traits::OneHalf;
5805 /// use malachite_float::Float;
5806 /// use malachite_q::Rational;
5807 /// use std::cmp::Ordering::*;
5808 ///
5809 /// let (p, o) = Float::unsigned_pow_rational_prec_ref(27, &Rational::from_signeds(1, 3), 20);
5810 /// assert_eq!(p.to_string(), "3.0000000");
5811 /// assert_eq!(o, Equal);
5812 ///
5813 /// let (p, o) = Float::unsigned_pow_rational_prec_ref(3, &Rational::ONE_HALF, 53);
5814 /// assert_eq!(p.to_string(), "1.7320508075688772");
5815 /// assert_eq!(o, Less);
5816 /// ```
5817 #[inline]
5818 pub fn unsigned_pow_rational_prec_ref(x: u64, y: &Rational, prec: u64) -> (Self, Ordering) {
5819 unsigned_pow_rational(x, y, prec, Nearest)
5820 }
5821}
5822
5823// k^q for a u64 k and Rational q. Since MPFR has no rational-exponent power, this is not a port:
5824// the value is 2^(q * log2(k)). Exact-rational results (k a perfect b-th power) and a power-of-2
5825// base are peeled off first (a Ziv-style squeeze never converges on an exactly-representable
5826// result); the remaining results are irrational and are bracketed by squeezing 2^(q * log2(k))
5827// between exact Rationals.
5828fn unsigned_pow_rational(k: u64, q: &Rational, prec: u64, rm: RoundingMode) -> (Float, Ordering) {
5829 assert_ne!(prec, 0);
5830 // Exact rounding: compute with Floor and demand exactness.
5831 if rm == Exact {
5832 let (result, o) = unsigned_pow_rational(k, q, prec, Floor);
5833 assert_eq!(o, Equal, "Inexact unsigned_pow_rational");
5834 return (result, Equal);
5835 }
5836 // k^0 = 1 for any k, even 0; 1^q = 1 for any q
5837 if *q == 0u32 || k == 1 {
5838 return (Float::one_prec(prec), Equal);
5839 }
5840 // 0^q = 0 for q > 0, and +Inf for q < 0
5841 if k == 0 {
5842 return if *q > 0u32 {
5843 (Float::ZERO, Equal)
5844 } else {
5845 (Float::INFINITY, Equal)
5846 };
5847 }
5848 // k = 2^s: k^q = 2^(s * q), and `power_of_2_rational_prec_round` handles all exactness,
5849 // overflow, and underflow.
5850 if k.is_power_of_2() {
5851 return Float::power_of_2_rational_prec_round(
5852 Rational::from(k.trailing_zeros()) * q,
5853 prec,
5854 rm,
5855 );
5856 }
5857 // k = j^b (with q = a / b in lowest terms): k^q = j^a is an exact rational, obtained by raising
5858 // the exact Float j to the integer power a.
5859 if let Ok(b) = u64::try_from(q.denominator_ref())
5860 && let Some(j) = k.checked_root(b)
5861 {
5862 let a = Integer::from_sign_and_abs_ref(*q >= 0u32, q.numerator_ref());
5863 return Float::from(j).pow_integer_prec_round(a, prec, rm);
5864 }
5865 // The remaining results are irrational. When `q` is tiny enough that `k ^ q` is within a few
5866 // ulps of 1, evaluating it as `2 ^ (q * log2(k))` would compute `log2(k)` to nearly `prec` bits
5867 // needlessly; a dedicated near-1 path handles that case far more cheaply.
5868 if let Some(result) = unsigned_pow_rational_near_one(k, q, prec, rm) {
5869 return result;
5870 }
5871 // Otherwise squeeze 2^(q * log2(k)) between exact Rationals. Since k >= 2, log2(k) >= 1, so
5872 // there is no sub-`MIN_EXPONENT` logarithm to contend with.
5873 pow_squeeze_t(&Rational::from(k), 0, q, prec, rm)
5874}
5875
5876/// Raises a primitive float to a primitive float power, returning a primitive float.
5877///
5878/// The result is correctly rounded to the nearest value, unlike [`f32::powf`] and [`f64::powf`],
5879/// which are not guaranteed to be correctly rounded.
5880///
5881/// $$
5882/// f(x,y) = x^y+\varepsilon.
5883/// $$
5884/// - If $x^y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
5885/// - If $x^y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x^y|\rfloor-p}$, where
5886/// $p$ is the precision of the output (typically 24 if `T` is a [`f32`] and 53 if `T` is a
5887/// [`f64`], but less if the output is subnormal).
5888///
5889/// Special cases:
5890/// - $f(x,\pm0.0)=1.0$ for any $x$, even `NaN`
5891/// - $f(1.0,y)=1.0$ for any $y$, even `NaN`
5892/// - $f(\text{NaN},y)=f(x,\text{NaN})=\text{NaN}$ otherwise
5893/// - $f(x,\infty)=\infty$ if $|x|>1$, and $0.0$ if $|x|<1$
5894/// - $f(x,-\infty)=0.0$ if $|x|>1$, and $\infty$ if $|x|<1$
5895/// - $f(-1.0,\pm\infty)=1.0$
5896/// - $f(-1.0,y)=1.0$ if $y$ is an even integer, and $-1.0$ if $y$ is an odd integer
5897/// - $f(\infty,y)=\infty$ if $y>0$, and $0.0$ if $y<0$
5898/// - $f(-\infty,y)=-\infty$ if $y$ is a positive odd integer, $\infty$ if $y$ is positive and not
5899/// an odd integer, $-0.0$ if $y$ is a negative odd integer, and $0.0$ if $y$ is negative and not
5900/// an odd integer
5901/// - $f(0.0,y)=0.0$ if $y>0$, and $\infty$ if $y<0$
5902/// - $f(-0.0,y)=-0.0$ if $y$ is a positive odd integer, $0.0$ if $y$ is positive and not an odd
5903/// integer, $-\infty$ if $y$ is a negative odd integer, and $\infty$ if $y$ is negative and not
5904/// an odd integer
5905/// - $f(x,y)=\text{NaN}$ if $x$ is finite and negative and $y$ is finite and not an integer
5906///
5907/// If the result overflows, $\pm\infty$ is returned, and if it underflows, $\pm0.0$ is returned.
5908///
5909/// # Worst-case complexity
5910/// Constant time and additional memory.
5911///
5912/// # Examples
5913/// ```
5914/// use malachite_base::num::float::NiceFloat;
5915/// use malachite_float::float::arithmetic::pow::primitive_float_pow;
5916///
5917/// assert_eq!(
5918/// NiceFloat(primitive_float_pow(3.0, 2.5)),
5919/// NiceFloat(15.588457268119896)
5920/// );
5921/// assert_eq!(
5922/// NiceFloat(primitive_float_pow(2.0, 0.5)),
5923/// NiceFloat(1.4142135623730951)
5924/// );
5925/// assert_eq!(
5926/// NiceFloat(primitive_float_pow(10.0, -0.5)),
5927/// NiceFloat(0.31622776601683794)
5928/// );
5929/// ```
5930#[allow(clippy::type_repetition_in_bounds)]
5931#[inline]
5932pub fn primitive_float_pow<T: PrimitiveFloat>(x: T, y: T) -> T
5933where
5934 Float: From<T> + PartialOrd<T>,
5935 for<'a> T: ExactFrom<&'a Float> + RoundingFrom<&'a Float>,
5936{
5937 emulate_float_float_to_float_fn(Float::pow_prec, x, y)
5938}
5939
5940/// Raises a [`Rational`] to a primitive float power, returning a primitive float.
5941///
5942/// The result is correctly rounded to the nearest value. Unlike a primitive-float base, a
5943/// [`Rational`] base may lie outside the primitive float's exponent range or so close to 1 that its
5944/// logarithm is unrepresentable; both are handled exactly, by working with the base as an exact
5945/// [`Rational`].
5946///
5947/// $$
5948/// f(x,y) = x^y+\varepsilon.
5949/// $$
5950/// - If $x^y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
5951/// - If $x^y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x^y|\rfloor-p}$, where
5952/// $p$ is the precision of the output (typically 24 if `T` is a [`f32`] and 53 if `T` is a
5953/// [`f64`], but less if the output is subnormal).
5954///
5955/// Special cases:
5956/// - $f(x,\pm0.0)=1.0$ for any $x$
5957/// - $f(1,y)=1.0$ for any $y$, even `NaN`
5958/// - $f(x,\text{NaN})=\text{NaN}$ otherwise
5959/// - $f(x,\infty)=\infty$ if $|x|>1$, and $0.0$ if $|x|<1$
5960/// - $f(x,-\infty)=0.0$ if $|x|>1$, and $\infty$ if $|x|<1$
5961/// - $f(\pm1,\pm\infty)=1.0$
5962/// - $f(-1,y)=1.0$ if $y$ is an even integer, and $-1.0$ if $y$ is an odd integer
5963/// - $f(0,y)=0.0$ if $y>0$, and $\infty$ if $y<0$; a [`Rational`] zero is unsigned, so the results
5964/// take positive signs
5965/// - $f(x,y)=\text{NaN}$ if $x<0$ and $y$ is finite and not an integer
5966///
5967/// If the result overflows, $\pm\infty$ is returned, and if it underflows, $\pm0.0$ is returned.
5968///
5969/// # Worst-case complexity
5970/// $T(m) = O(m \log m \log\log m)$
5971///
5972/// $M(m) = O(m \log m)$
5973///
5974/// where $T$ is time, $M$ is additional memory, and $m$ is `x.significant_bits()`.
5975///
5976/// # Examples
5977/// ```
5978/// use malachite_base::num::float::NiceFloat;
5979/// use malachite_float::float::arithmetic::pow::primitive_float_rational_pow;
5980/// use malachite_q::Rational;
5981///
5982/// assert_eq!(
5983/// NiceFloat(primitive_float_rational_pow(
5984/// &Rational::from_unsigneds(3u32, 2u32),
5985/// 2.5
5986/// )),
5987/// NiceFloat(2.7556759606310752)
5988/// );
5989/// assert_eq!(
5990/// NiceFloat(primitive_float_rational_pow(
5991/// &Rational::from_unsigneds(9u32, 4u32),
5992/// 0.5
5993/// )),
5994/// NiceFloat(1.5)
5995/// );
5996/// assert!(
5997/// primitive_float_rational_pow::<f64>(&-Rational::from_unsigneds(3u32, 2u32), 0.5).is_nan()
5998/// );
5999/// ```
6000#[allow(clippy::type_repetition_in_bounds)]
6001#[inline]
6002pub fn primitive_float_rational_pow<T: PrimitiveFloat>(x: &Rational, y: T) -> T
6003where
6004 Float: From<T> + PartialOrd<T>,
6005 for<'a> T: ExactFrom<&'a Float> + RoundingFrom<&'a Float>,
6006{
6007 emulate_float_to_float_fn(|y2, prec| Float::rational_pow_prec_ref_val(x, y2, prec), y)
6008}
6009
6010/// Raises a primitive float to a [`Rational`] power, returning a primitive float.
6011///
6012/// The result is correctly rounded to the nearest value. Unlike a primitive-float exponent, the
6013/// exact [`Rational`] exponent selects a definite branch of the power, so results that are exactly
6014/// representable (such as roots of perfect powers) come out exactly.
6015///
6016/// $$
6017/// f(x,y) = x^y+\varepsilon.
6018/// $$
6019/// - If $x^y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
6020/// - If $x^y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x^y|\rfloor-p}$, where
6021/// $p$ is the precision of the output (typically 24 if `T` is a [`f32`] and 53 if `T` is a
6022/// [`f64`], but less if the output is subnormal).
6023///
6024/// Special cases:
6025/// - $f(x,0)=1.0$ for any $x$, even `NaN`
6026/// - $f(1.0,y)=1.0$
6027/// - $f(\text{NaN},y)=\text{NaN}$ if $y \neq 0$
6028/// - $f(x,y)=\text{NaN}$ if $x<0$ and $y$ is not an integer
6029/// - $f(-1.0,y)=1.0$ if $y$ is an even integer, and $-1.0$ if $y$ is an odd integer
6030/// - $f(\infty,y)=\infty$ if $y>0$, and $0.0$ if $y<0$
6031/// - $f(-\infty,y)=-\infty$ if $y$ is a positive odd integer, $\infty$ if $y$ is positive and not
6032/// an odd integer, $-0.0$ if $y$ is a negative odd integer, and $0.0$ if $y$ is negative and not
6033/// an odd integer
6034/// - $f(0.0,y)=0.0$ if $y>0$, and $\infty$ if $y<0$
6035/// - $f(-0.0,y)=-0.0$ if $y$ is a positive odd integer, $0.0$ if $y$ is positive and not an odd
6036/// integer, $-\infty$ if $y$ is a negative odd integer, and $\infty$ if $y$ is negative and not
6037/// an odd integer
6038///
6039/// If the result overflows, $\pm\infty$ is returned, and if it underflows, $\pm0.0$ is returned.
6040///
6041/// # Worst-case complexity
6042/// $T(m) = O(m \log m \log\log m)$
6043///
6044/// $M(m) = O(m \log m)$
6045///
6046/// where $T$ is time, $M$ is additional memory, and $m$ is `y.significant_bits()`.
6047///
6048/// # Examples
6049/// ```
6050/// use malachite_base::num::basic::traits::OneHalf;
6051/// use malachite_base::num::float::NiceFloat;
6052/// use malachite_float::float::arithmetic::pow::primitive_float_pow_rational;
6053/// use malachite_q::Rational;
6054///
6055/// assert_eq!(
6056/// NiceFloat(primitive_float_pow_rational(4.0, &Rational::ONE_HALF)),
6057/// NiceFloat(2.0)
6058/// );
6059/// assert_eq!(
6060/// NiceFloat(primitive_float_pow_rational(
6061/// 2.0,
6062/// &Rational::from_signeds(3, 2)
6063/// )),
6064/// NiceFloat(2.8284271247461903)
6065/// );
6066/// assert_eq!(
6067/// NiceFloat(primitive_float_pow_rational(
6068/// 4.0,
6069/// &Rational::from_signeds(-1, 2)
6070/// )),
6071/// NiceFloat(0.5)
6072/// );
6073/// assert!(primitive_float_pow_rational::<f64>(-8.0, &Rational::from_signeds(1, 3)).is_nan());
6074/// ```
6075#[allow(clippy::type_repetition_in_bounds)]
6076#[inline]
6077pub fn primitive_float_pow_rational<T: PrimitiveFloat>(x: T, y: &Rational) -> T
6078where
6079 Float: From<T> + PartialOrd<T>,
6080 for<'a> T: ExactFrom<&'a Float> + RoundingFrom<&'a Float>,
6081{
6082 emulate_float_to_float_fn(|x, prec| Float::pow_rational_prec_val_ref(x, y, prec), x)
6083}
6084
6085/// Raises a primitive float to the power of an [`Integer`], returning a primitive float.
6086///
6087/// The result is correctly rounded to the nearest value. Unlike a primitive-float exponent, an
6088/// arbitrarily large [`Integer`] exponent is handled exactly.
6089///
6090/// $$
6091/// f(x,n) = x^n+\varepsilon.
6092/// $$
6093/// - If $x^n$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
6094/// - If $x^n$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x^n|\rfloor-p}$, where
6095/// $p$ is the precision of the output (typically 24 if `T` is a [`f32`] and 53 if `T` is a
6096/// [`f64`], but less if the output is subnormal).
6097///
6098/// Special cases:
6099/// - $f(x,0)=1.0$ for any $x$, even `NaN`
6100/// - $f(1,n)=1.0$
6101/// - $f(\text{NaN},n)=\text{NaN}$ if $n \neq 0$
6102/// - $f(-1,n)=1.0$ if $n$ is even, and $-1.0$ if $n$ is odd
6103/// - $f(\infty,n)=\infty$ if $n>0$, and $0.0$ if $n<0$
6104/// - $f(-\infty,n)=-\infty$ if $n$ is positive and odd, $\infty$ if $n$ is positive and even,
6105/// $-0.0$ if $n$ is negative and odd, and $0.0$ if $n$ is negative and even
6106/// - $f(0.0,n)=0.0$ if $n>0$, and $\infty$ if $n<0$
6107/// - $f(-0.0,n)=-0.0$ if $n$ is positive and odd, $0.0$ if $n$ is positive and even, $-\infty$ if
6108/// $n$ is negative and odd, and $\infty$ if $n$ is negative and even
6109///
6110/// If the result overflows, $\pm\infty$ is returned, and if it underflows, $\pm0.0$ is returned.
6111///
6112/// # Worst-case complexity
6113/// $T(m) = O(m)$
6114///
6115/// $M(m) = O(m)$
6116///
6117/// where $T$ is time, $M$ is additional memory, and $m$ is `y.significant_bits()`.
6118///
6119/// # Examples
6120/// ```
6121/// use malachite_base::num::float::NiceFloat;
6122/// use malachite_float::float::arithmetic::pow::primitive_float_pow_integer;
6123/// use malachite_nz::integer::Integer;
6124///
6125/// assert_eq!(
6126/// NiceFloat(primitive_float_pow_integer(3.0, &Integer::from(5))),
6127/// NiceFloat(243.0)
6128/// );
6129/// assert_eq!(
6130/// NiceFloat(primitive_float_pow_integer(2.0, &Integer::from(-3))),
6131/// NiceFloat(0.125)
6132/// );
6133/// assert_eq!(
6134/// NiceFloat(primitive_float_pow_integer(-2.0, &Integer::from(3))),
6135/// NiceFloat(-8.0)
6136/// );
6137/// ```
6138#[allow(clippy::type_repetition_in_bounds)]
6139#[inline]
6140pub fn primitive_float_pow_integer<T: PrimitiveFloat>(x: T, y: &Integer) -> T
6141where
6142 Float: From<T> + PartialOrd<T>,
6143 for<'a> T: ExactFrom<&'a Float> + RoundingFrom<&'a Float>,
6144{
6145 emulate_float_to_float_fn(|x, prec| Float::pow_integer_prec_val_ref(x, y, prec), x)
6146}
6147
6148/// Raises a primitive float to the power of a [`u64`], returning a primitive float.
6149///
6150/// The result is correctly rounded to the nearest value.
6151///
6152/// $$
6153/// f(x,n) = x^n+\varepsilon.
6154/// $$
6155/// - If $x^n$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
6156/// - If $x^n$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 |x^n|\rfloor-p}$, where
6157/// $p$ is the precision of the output (typically 24 if `T` is a [`f32`] and 53 if `T` is a
6158/// [`f64`], but less if the output is subnormal).
6159///
6160/// Special cases:
6161/// - $f(x,0)=1.0$ for any $x$, even `NaN`
6162/// - $f(1.0,n)=1.0$
6163/// - $f(\text{NaN},n)=\text{NaN}$ if $n \neq 0$
6164/// - $f(-1.0,n)=1.0$ if $n$ is even, and $-1.0$ if $n$ is odd
6165/// - $f(\infty,n)=\infty$ if $n>0$
6166/// - $f(-\infty,n)=\infty$ if $n$ is positive and even, and $-\infty$ if $n$ is odd
6167/// - $f(0.0,n)=0.0$ if $n>0$
6168/// - $f(-0.0,n)=0.0$ if $n$ is positive and even, and $-0.0$ if $n$ is odd
6169///
6170/// If the result overflows, $\pm\infty$ is returned, and if it underflows, $\pm0.0$ is returned.
6171///
6172/// # Worst-case complexity
6173/// Constant time and additional memory.
6174///
6175/// # Examples
6176/// ```
6177/// use malachite_base::num::float::NiceFloat;
6178/// use malachite_float::float::arithmetic::pow::primitive_float_pow_u;
6179///
6180/// assert_eq!(NiceFloat(primitive_float_pow_u(3.0, 5)), NiceFloat(243.0));
6181/// assert_eq!(NiceFloat(primitive_float_pow_u(2.0, 10)), NiceFloat(1024.0));
6182/// assert_eq!(NiceFloat(primitive_float_pow_u(-2.0, 3)), NiceFloat(-8.0));
6183/// ```
6184#[allow(clippy::type_repetition_in_bounds)]
6185#[inline]
6186pub fn primitive_float_pow_u<T: PrimitiveFloat>(x: T, n: u64) -> T
6187where
6188 Float: From<T> + PartialOrd<T>,
6189 for<'a> T: ExactFrom<&'a Float> + RoundingFrom<&'a Float>,
6190{
6191 emulate_float_to_float_fn(|x, prec| x.pow_u_prec(n, prec), x)
6192}
6193
6194/// Raises a [`u64`] to the power of a primitive float, returning a primitive float.
6195///
6196/// The result is correctly rounded to the nearest value.
6197///
6198/// $$
6199/// f(x,y) = x^y+\varepsilon.
6200/// $$
6201/// - If $x^y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
6202/// - If $x^y$ is finite and nonzero, then $|\varepsilon| < 2^{\lfloor\log_2 x^y\rfloor-p}$, where
6203/// $p$ is the precision of the output (typically 24 if `T` is a [`f32`] and 53 if `T` is a
6204/// [`f64`], but less if the output is subnormal).
6205///
6206/// Special cases:
6207/// - $f(x,0.0)=1.0$ for any $x$
6208/// - $f(1,y)=1.0$ for any $y$, even `NaN`
6209/// - $f(x,\text{NaN})=\text{NaN}$ if $x \neq 1$
6210/// - $f(x,\infty)=\infty$ if $x>1$, and $0.0$ if $x=0$
6211/// - $f(x,-\infty)=0.0$ if $x>1$, and $\infty$ if $x=0$
6212/// - $f(0,y)=0.0$ if $y>0$, and $\infty$ if $y<0$
6213///
6214/// If the result overflows, $\infty$ is returned, and if it underflows, $0.0$ is returned.
6215///
6216/// # Worst-case complexity
6217/// Constant time and additional memory.
6218///
6219/// # Examples
6220/// ```
6221/// use malachite_base::num::float::NiceFloat;
6222/// use malachite_float::float::arithmetic::pow::primitive_float_unsigned_pow;
6223///
6224/// assert_eq!(
6225/// NiceFloat(primitive_float_unsigned_pow(2, 0.5)),
6226/// NiceFloat(1.4142135623730951)
6227/// );
6228/// assert_eq!(
6229/// NiceFloat(primitive_float_unsigned_pow(3, 2.5)),
6230/// NiceFloat(15.588457268119896)
6231/// );
6232/// assert_eq!(
6233/// NiceFloat(primitive_float_unsigned_pow(2, -1.0)),
6234/// NiceFloat(0.5)
6235/// );
6236/// ```
6237#[allow(clippy::type_repetition_in_bounds)]
6238#[inline]
6239pub fn primitive_float_unsigned_pow<T: PrimitiveFloat>(x: u64, y: T) -> T
6240where
6241 Float: From<T> + PartialOrd<T>,
6242 for<'a> T: ExactFrom<&'a Float> + RoundingFrom<&'a Float>,
6243{
6244 emulate_float_to_float_fn(|y2, prec| Float::unsigned_pow_prec(x, y2, prec), y)
6245}
6246
6247// Brackets of ln(1 + e) for an exact nonzero Rational e with |e| < 1/2, as exact Rationals, to a
6248// relative accuracy of about 2^-wprec. Uses the atanh series ln(1 + e) = 2 atanh(u) with u = e / (2
6249// + e) and |u| < 1/3: atanh(u) = sum_{k>=0} u^(2k+1)/(2k+1), whose tail after the term in u^(2k+1)
6250// is bounded in magnitude by that term times u^2 / (1 - u^2) < that term * 9/8. The partial sum and
6251// the tail both have the sign of e, so the exact value lies between the partial sum and (partial
6252// sum + tail). Splits a positive Rational x as x' * 2^g with g the nearest integer to log2(x) and
6253// x' in [1/sqrt(2), sqrt(2)), so that x' is close to 1 (never near 2, where a Float log would
6254// collapse).
6255fn rational_mantissa_nearest_power_of_2(x: &Rational) -> (Rational, i64) {
6256 let fl = x.floor_log_base_2_abs();
6257 let mant = x >> fl;
6258 let g = if (&mant).square() < 2u32 { fl } else { fl + 1 };
6259 (x >> g, g)
6260}
6261
6262// Whether x^y is a dyadic rational (and therefore possibly exactly representable), for a positive
6263// non-power-of-2 Rational x = (a / b) * 2^e with a, b odd and coprime, and a finite nonzero
6264// non-singular Float y = c * 2^d with c an odd Integer. If so, returns (m, z, pow) such that x^y =
6265// m^z * 2^pow with m an odd Natural and z a positive Integer; otherwise returns None. Since x is
6266// not a power of 2, a Ziv-style squeeze on an exact x^y would never terminate, and a nearest-mode
6267// tie is possible only in the dyadic case, so this decides when the direct route is required.
6268fn rational_pow_exact_decomposition(
6269 a: &Natural,
6270 b: &Natural,
6271 e: i64,
6272 y: &Float,
6273) -> Option<(Natural, Integer, Integer)> {
6274 let (c, d) = float_to_odd_mantissa_and_exponent(y);
6275 let (mut a, mut b) = (a.clone(), b.clone());
6276 let mut e = Integer::from(e);
6277 // Descend the negative powers of 2 in the exponent: x must be a perfect 2^|d|-th power.
6278 if d < 0 {
6279 for _ in 0..-d {
6280 if a != 1u32 {
6281 a = a.checked_sqrt()?;
6282 }
6283 if b != 1u32 {
6284 b = b.checked_sqrt()?;
6285 }
6286 if e.odd() {
6287 return None;
6288 }
6289 e >>= 1u32;
6290 }
6291 } else {
6292 e <<= d;
6293 }
6294 // Now x^y = (a / b)^(c * 2^max(d, 0)) * 2^(e * c), with the power of 2 in the exponent already
6295 // scaled into e. Dyadic requires the denominator (after accounting for c's sign) to be 1.
6296 let pow = e * &c;
6297 let m = if c > 0u32 {
6298 if b != 1u32 {
6299 return None;
6300 }
6301 a
6302 } else {
6303 if a != 1u32 {
6304 return None;
6305 }
6306 b
6307 };
6308 let mut z = Integer::from(c.unsigned_abs());
6309 if d > 0 {
6310 z <<= d;
6311 }
6312 Some((m, z, pow))
6313}
6314
6315// The in-range squeeze: x is positive, not a dyadic rational, and comfortably within the Float
6316// exponent range, and y is finite, nonzero, and not a small integer. Brackets x between dyadic
6317// Floats at growing precision and applies `Float::pow` to both ends, tightening until both ends
6318// round identically. Since x has an odd prime factor in its denominator, x^y is never exactly
6319// representable and never a nearest-mode tie, so the squeeze terminates.
6320fn rational_pow_squeeze_x(
6321 x: &Rational,
6322 y: &Float,
6323 prec: u64,
6324 rm: RoundingMode,
6325) -> (Float, Ordering) {
6326 let mut wprec = prec.saturating_add(TWICE_WIDTH);
6327 let mut increment = Limb::WIDTH;
6328 loop {
6329 let x_lo = Float::from_rational_prec_round_ref(x, wprec, Floor).0;
6330 let x_hi = Float::from_rational_prec_round_ref(x, wprec, Ceiling).0;
6331 let (p_lo, mut o_lo) = x_lo.pow_prec_round_val_ref(y, prec, rm);
6332 let (p_hi, mut o_hi) = x_hi.pow_prec_round_val_ref(y, prec, rm);
6333 // A bracket end that lands exactly on a representable power rounds with `Equal`; the true
6334 // value lies strictly between the ends, so the other end's ordering is the true one.
6335 if o_lo == Equal {
6336 o_lo = o_hi;
6337 }
6338 if o_hi == Equal {
6339 o_hi = o_lo;
6340 }
6341 // `x` is positive, so `Float::pow` yields a positive value at precision `prec` (or `+inf`
6342 // on overflow, `+0.0` on underflow), never `NaN` or `-0.0`, and a plain value comparison
6343 // suffices.
6344 if o_lo == o_hi && p_lo == p_hi {
6345 return (p_lo, o_lo);
6346 }
6347 wprec += increment;
6348 increment = wprec >> 1;
6349 }
6350}
6351
6352// The shared rational-exponent squeeze: computes (x' * 2^e)^y for an exact Rational x' whose binary
6353// logarithm `log_2_rational_brackets` can bracket, an integer e, and an exact Rational exponent y
6354// (finite and nonzero), assuming the true result is irrational. Brackets t = y * (e + log2(x'))
6355// between exact Rationals -- Rationals have no exponent range, so no underflow or overflow can
6356// occur here -- and applies `Float::power_of_2_rational_prec_round` to both ends, which itself
6357// handles results at or beyond the exponent boundaries, growing the working precision until the
6358// ends agree. `rational_pow` reaches this in its extreme regime with x' in [1/sqrt(2), sqrt(2));
6359// `unsigned_pow_rational` reaches it with x' = k and e = 0. Growth past the initial precision is
6360// rare but constructible: 6^(1 + 2^-300) lies within 2^-300 of the rounding boundary 6.0, so the
6361// first bracket straddles it at any target precision below ~300. Fast path for `k ^ q` when the
6362// result is extremely close to 1 (`q` so tiny that `k ^ q = exp(q * ln k)` differs from 1 by at
6363// most a handful of ulps). The general squeeze in `pow_squeeze_t` evaluates `log2(k)` to about
6364// `prec` bits, which is wasteful here; instead bracket `ln(k)` between two `Rational`s from a
6365// single modest-precision `ln(k)` and apply `exp_rational_near_one` to the tiny products `q *
6366// ln(k)`. Returns `None` when the result is not close enough to 1 for this to help (the caller then
6367// squeezes). Mirrors `power_of_2_rational_near_one`, replacing the constant `ln(2)` with `ln(k)`.
6368// `k >= 2` and `q` is a nonzero non-integer, so `k ^ q` is irrational.
6369fn unsigned_pow_rational_near_one(
6370 k: u64,
6371 q: &Rational,
6372 prec: u64,
6373 rm: RoundingMode,
6374) -> Option<(Float, Ordering)> {
6375 // `2 ^ ql <= |q| < 2 ^ (ql + 1)` and `log2(k) < kb <= 2 ^ kbb` (kbb the bit length of kb), so
6376 // `|q * log2(k)| < 2 ^ (ql + 1 + kbb)`. Take this path only when that bound puts `k ^ q` within
6377 // roughly a machine word's worth of ulps of 1: then `exp_rational_near_one` converges in O(1)
6378 // terms and `ln(k)` is needed to only about `prec + t_exp_ub` bits. The `t_exp_ub >= 0` guard
6379 // also keeps `|q * ln(k)| < 1`, which `exp_rational_near_one` requires.
6380 let ql = q.floor_log_base_2_abs();
6381 let kbb = i64::exact_from(k.significant_bits().significant_bits());
6382 let t_exp_ub = ql + 1 + kbb;
6383 if t_exp_ub >= 0 || t_exp_ub > -i64::exact_from(prec) + const { Limb::WIDTH as i64 } {
6384 return None;
6385 }
6386 // `k > 1`, so `k ^ q > 1` exactly when `q > 0`. Because `q * ln(k)` is tiny, `ln(k)` needs only
6387 // about `prec + t_exp_ub` bits to separate the two exp brackets at the target precision -- far
6388 // below `prec`. Start a little above that and let the Ziv loop grow it.
6389 let above = *q > 0u32;
6390 let mut working_prec = u64::saturating_from(i64::exact_from(prec) + t_exp_ub) + Limb::WIDTH;
6391 let mut increment = Limb::WIDTH;
6392 let kf = Float::from(k);
6393 loop {
6394 // `ln_k_lo <= ln(k) <= ln_k_hi`, as exact Rationals, from a single `ln(k)` computation.
6395 let (ln_k_lo, ln_k_hi) = floor_and_ceiling(kf.ln_prec_round_ref(working_prec, Floor));
6396 let ln_k_lo = Rational::exact_from(&ln_k_lo);
6397 let ln_k_hi = Rational::exact_from(&ln_k_hi);
6398 // `q * ln(k)` lies between these two products (which end is smaller depends on the sign of
6399 // `q`), and exp is increasing, so `k ^ q` lies between the exps of the two products.
6400 let (p_lo, p_hi) = if above {
6401 (q * ln_k_lo, q * ln_k_hi)
6402 } else {
6403 (q * ln_k_hi, q * ln_k_lo)
6404 };
6405 let (lo, o_lo) = exp_rational_near_one(&p_lo, prec, rm);
6406 let (hi, o_hi) = exp_rational_near_one(&p_hi, prec, rm);
6407 if o_lo == o_hi && lo == hi {
6408 return Some((lo, o_lo));
6409 }
6410 working_prec += increment;
6411 increment = working_prec >> 1;
6412 }
6413}
6414
6415fn pow_squeeze_t(
6416 xp: &Rational,
6417 e: i64,
6418 y: &Rational,
6419 prec: u64,
6420 rm: RoundingMode,
6421) -> (Float, Ordering) {
6422 let er = Rational::from(e);
6423 let mut wprec = prec.saturating_add(TWICE_WIDTH);
6424 let mut increment = Limb::WIDTH;
6425 loop {
6426 let (l_lo, l_hi) = log_2_rational_brackets(xp, wprec);
6427 let (t_lo, t_hi) = if *y > 0u32 {
6428 (y * (&er + l_lo), y * (&er + l_hi))
6429 } else {
6430 (y * (&er + l_hi), y * (&er + l_lo))
6431 };
6432 let (p_lo, mut o_lo) = Float::power_of_2_rational_prec_round(t_lo, prec, rm);
6433 let (p_hi, mut o_hi) = Float::power_of_2_rational_prec_round(t_hi, prec, rm);
6434 // A bracket end landing exactly on a representable power rounds with `Equal`; the true
6435 // value lies strictly between the ends, so the other end's ordering is the true one.
6436 if o_lo == Equal {
6437 fail_on_untested_path(
6438 "pow_squeeze_t, lo_eq: exact results (t an integer) are caught by each caller's \
6439 exact decomposition before the squeeze, so t is never an integer here; a bracket \
6440 end equalling an integer is a measure-zero coincidence of the log brackets",
6441 );
6442 o_lo = o_hi;
6443 }
6444 if o_hi == Equal {
6445 fail_on_untested_path(
6446 "pow_squeeze_t, hi_eq: as lo_eq -- t is never an integer in the squeeze, so a \
6447 bracket end equalling one is a measure-zero coincidence",
6448 );
6449 o_hi = o_lo;
6450 }
6451 // `power_of_2_rational_prec_round` yields a positive value at precision `prec` (or `+inf`
6452 // on overflow, `+0.0` on underflow), never `NaN` or `-0.0`, so a plain value comparison
6453 // suffices -- no need for `ComparableFloatRef` to force equal precisions or to make `NaN`s
6454 // compare equal.
6455 if o_lo == o_hi && p_lo == p_hi {
6456 return (p_lo, o_lo);
6457 }
6458 wprec += increment;
6459 increment = wprec >> 1;
6460 }
6461}
6462
6463// The exact-dyadic route: x^y = m^z * 2^pow with m odd. If the result's odd part is small enough to
6464// affect prec-bit rounding (or to be a nearest-mode tie), materialize it; otherwise the value is
6465// neither representable nor a tie and the caller may squeeze safely.
6466fn rational_pow_exact(
6467 m: &Natural,
6468 z: &Integer,
6469 pow: &Integer,
6470 prec: u64,
6471 rm: RoundingMode,
6472) -> Option<(Float, Ordering)> {
6473 let zu = u64::try_from(z).ok()?;
6474 // The rejection must use a *lower* bound on the significant bits of m^z: returning `None`
6475 // asserts that the result is neither representable at `prec` nor a `Nearest` tie (both need at
6476 // most prec + 2 significant bits), and the caller then squeezes -- which never terminates on a
6477 // representable value or a tie. Since m >= 2^(sb(m) - 1), m^z >= 2^(z * (sb(m) - 1)), so
6478 // sb(m^z) >= z * (sb(m) - 1) + 1. (An upper bound like z * sb(m) is unsound here: it
6479 // overestimates sb(m^z) by up to z - 1 bits, letting exactly-representable results and ties
6480 // leak into the squeeze.) The materialization below stays cheap: the caller has peeled
6481 // power-of-2 bases, so m is odd and m >= 3, hence sb(m) >= 2 and any admitted z satisfies z <=
6482 // z * (sb(m) - 1) <= prec + 1, giving sb(m^z) <= z * sb(m) <= 2 * prec + 2.
6483 debug_assert!(*m > 1u32 && m.odd());
6484 let bits_lower = (m.significant_bits() - 1).checked_mul(zu)?.checked_add(1)?;
6485 if bits_lower > prec + 2 {
6486 return None;
6487 }
6488 let value = m.clone().pow(zu);
6489 let (result, o) = Float::from_natural_prec_round(value, prec, rm);
6490 // Scale by 2^pow. An exponent beyond i64 with a prec-bit odd part is a definite overflow or
6491 // underflow.
6492 let Ok(shift) = i64::try_from(pow) else {
6493 return Some(if *pow > 0u32 {
6494 fail_on_untested_path(
6495 "rational_pow, ex_pow_overflow: reachable only with a base whose 2-adic \
6496 valuation exceeds i64::MAX / prec while its odd part fits in prec + 2 bits -- \
6497 simultaneously a ~512-MB base and a ~2^31 precision, beyond practical test \
6498 sizes",
6499 );
6500 exp_overflow(prec, rm)
6501 } else {
6502 fail_on_untested_path(
6503 "rational_pow, ex_pow_underflow: as ex_pow_overflow, in the negative-exponent \
6504 direction",
6505 );
6506 exp_underflow(prec, if rm == Nearest { Down } else { rm })
6507 });
6508 };
6509 let (shifted, oo) = result.shl_prec_round(shift, prec, rm);
6510 Some((shifted, if oo == Equal { o } else { oo }))
6511}
6512
6513// Whether the Rational y is an odd integer.
6514fn rational_odd_integer(y: &Rational) -> bool {
6515 *y.denominator_ref() == 1u32 && y.numerator_ref().odd()
6516}
6517
6518// Raises a finite, positive Float x to the power of a finite, nonzero, non-integer Rational y = a /
6519// b (in lowest terms, so b >= 2), returning the result rounded to `prec` bits with `rm`.
6520fn positive_float_pow_rational(
6521 x: &Float,
6522 y: &Rational,
6523 prec: u64,
6524 rm: RoundingMode,
6525) -> (Float, Ordering) {
6526 // x = c * 2^d with c odd (c >= 1).
6527 let (c, d) = float_to_odd_mantissa_and_exponent_natural(x);
6528 // x = 2^d: x^y = 2^(d * y), an exact-Rational exponent that `power_of_2_rational_prec_round`
6529 // handles completely (exactness, overflow, and underflow).
6530 if c == 1u32 {
6531 return Float::power_of_2_rational_prec_round(Rational::from(d) * y, prec, rm);
6532 }
6533 // x^(a/b) is rational exactly when x is a perfect b-th power of a Float, i.e. b | d and the odd
6534 // part c is a perfect b-th power j^b. Then x^(1/b) = j * 2^(d/b) is an exact Float `base`, and
6535 // x^(a/b) = base^a is delegated to `pow_integer`, which correctly rounds the (possibly
6536 // non-dyadic, for a < 0) result and handles overflow and underflow. Otherwise x^(a/b) is
6537 // irrational.
6538 if let Ok(b) = u64::try_from(y.denominator_ref())
6539 && d.unsigned_abs().divisible_by(b)
6540 && let Some(j) = (&c).checked_root(b)
6541 {
6542 let base = Float::exact_from(j) << (d / i64::exact_from(b));
6543 let a = Integer::from_sign_and_abs_ref(*y > 0u32, y.numerator_ref());
6544 return base.pow_integer_prec_round(a, prec, rm);
6545 }
6546 // The result is irrational. First a tiny-result shortcut: if |y * log2(x)| is far below 1, then
6547 // x^y rounds to 1 +/- ulp, sparing the (possibly huge) log2 bracketing. Since |y| < 2^ey and
6548 // |log2(x)| < 2^expb, one has |y * log2(x)| < 2^(ey + expb).
6549 let ex = i64::from(x.get_exponent().unwrap());
6550 let ey = y.floor_log_base_2_abs() + 1;
6551 let above = (*y > 0u32) == (*x > 1u32);
6552 let expb = if ex == 0 || ex == 1 {
6553 // x is in (1/2, 2), close to 1 (and x != 1, since |x| = 1 was handled by the caller): with
6554 // fld = floor(log2|x - 1|), one has |log2(x)| < 2^(fld + 2).
6555 (Rational::exact_from(x) - Rational::ONE).floor_log_base_2_abs() + 2
6556 } else {
6557 // x is bounded away from 1: |log2(x)| <= expx = max(ex, 1 - ex) < 2^ceil(log2(expx)).
6558 let expx = if ex > 1 { ex } else { 1 - ex };
6559 i64::exact_from(u64::exact_from(expx).ceiling_log_base_2())
6560 };
6561 if ey + expb < -i64::exact_from(prec) - 1 {
6562 return float_one_plus_tiny(prec, rm, above);
6563 }
6564 // General squeeze: bracket log2(x) = d + log2(c) between exact Rationals and apply 2^(y * (d +
6565 // log2(c))). Working in the exponent (t-space) stays correct even when x is a sliver of 1,
6566 // where a Float-based log2(x) would underflow below the smallest positive Float.
6567 pow_squeeze_t(&Rational::from(c), d, y, prec, rm)
6568}
6569
6570// Raises a Float to the power of a Rational, returning a Float rounded to `prec` bits with `rm`.
6571fn float_rational_pow(x: &Float, y: &Rational, prec: u64, rm: RoundingMode) -> (Float, Ordering) {
6572 assert_ne!(prec, 0);
6573 // Exact rounding: compute with Nearest and demand exactness.
6574 if rm == Exact {
6575 let (result, o) = float_rational_pow(x, y, prec, Nearest);
6576 assert_eq!(o, Equal, "Inexact pow");
6577 return (result, Equal);
6578 }
6579 // x^0 = 1 for any x, even NaN.
6580 if *y == 0u32 {
6581 return (Float::one_prec(prec), Equal);
6582 }
6583 // Singular x; see Section F.9.4.4 of the C standard. y is a finite nonzero Rational, so the
6584 // singular-y cases (0, NaN, +/-Inf) do not arise.
6585 match x {
6586 float_nan!() => return (Float::NAN, Equal),
6587 Float(Infinity { sign }) => {
6588 let negative = !*sign && rational_odd_integer(y);
6589 return (
6590 match (*y > 0u32, negative) {
6591 (true, false) => Float::INFINITY,
6592 (true, true) => Float::NEGATIVE_INFINITY,
6593 (false, false) => Float::ZERO,
6594 (false, true) => Float::NEGATIVE_ZERO,
6595 },
6596 Equal,
6597 );
6598 }
6599 Float(Zero { sign }) => {
6600 let negative = !*sign && rational_odd_integer(y);
6601 return (
6602 match (*y < 0u32, negative) {
6603 (true, false) => Float::INFINITY,
6604 (true, true) => Float::NEGATIVE_INFINITY,
6605 (false, false) => Float::ZERO,
6606 (false, true) => Float::NEGATIVE_ZERO,
6607 },
6608 Equal,
6609 );
6610 }
6611 _ => {}
6612 }
6613 // x finite and nonzero.
6614 let y_is_integer = *y.denominator_ref() == 1u32;
6615 // x^y for x < 0 and y not an integer is not defined.
6616 if x.is_sign_negative() && !y_is_integer {
6617 return (Float::NAN, Equal);
6618 }
6619 // |x| = 1: (+/-1)^y = +/-1 (the sign is negative only for x = -1 and odd y).
6620 if x.partial_cmp_abs(&Float::ONE).unwrap() == Equal {
6621 let negative = x.is_sign_negative() && rational_odd_integer(y);
6622 return Float::from_float_prec_round(
6623 if negative { -Float::ONE } else { Float::ONE },
6624 prec,
6625 rm,
6626 );
6627 }
6628 // Integer y: the multiplication-based `pow_integer` handles negative x (via parity), overflow,
6629 // and underflow.
6630 if y_is_integer {
6631 return pow_integer(x, &Integer::rounding_from(y, Exact).0, prec, rm);
6632 }
6633 // x > 0 (negative x with non-integer y was rejected above), y = a / b with b >= 2.
6634 positive_float_pow_rational(x, y, prec, rm)
6635}
6636
6637// Whether x^y is a dyadic rational (hence possibly exactly representable), for a positive
6638// non-power-of-2 Rational x = (a / b) * 2^e (a, b odd and coprime) and a finite nonzero non-integer
6639// Rational y = a_y / b_y (in lowest terms, b_y >= 2). If so, returns (m, z, pow) such that x^y =
6640// m^z * 2^pow with m an odd Natural (> 1) and z a positive Integer; otherwise returns None. Since x
6641// is not a power of 2, a Ziv-style squeeze on an exact x^y would never terminate, and a
6642// nearest-mode tie is possible only in the dyadic case, so this decides when the direct route is
6643// required.
6644fn rational_rational_pow_exact_decomposition(
6645 a: &Natural,
6646 b: &Natural,
6647 e: i64,
6648 y: &Rational,
6649) -> Option<(Natural, Integer, Integer)> {
6650 let b_y = u64::try_from(y.denominator_ref()).ok()?;
6651 // 2^(e * a_y / b_y) is dyadic exactly when b_y | e (since gcd(a_y, b_y) = 1).
6652 if !e.unsigned_abs().divisible_by(b_y) {
6653 return None;
6654 }
6655 // (a / b)^(a_y / b_y) is dyadic only if a and b are each perfect b_y-th powers.
6656 let p = a.checked_root(b_y)?;
6657 let q = b.checked_root(b_y)?;
6658 let a_y_abs = y.numerator_ref();
6659 // pow = e * a_y / b_y = (e / b_y) * a_y, an exact integer.
6660 let pow = Integer::from(e / i64::exact_from(b_y))
6661 * Integer::from_sign_and_abs_ref(*y > 0u32, a_y_abs);
6662 if *y > 0u32 {
6663 // 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
6664 // not a power of 2, so a > 1 here).
6665 if q != 1u32 {
6666 return None;
6667 }
6668 Some((p, Integer::from(a_y_abs), pow))
6669 } else {
6670 // q^|a_y| / p^|a_y| is dyadic only when p = 1, i.e. a = 1. Then m = q (> 1).
6671 if p != 1u32 {
6672 return None;
6673 }
6674 Some((q, Integer::from(a_y_abs), pow))
6675 }
6676}
6677
6678// Raises a Rational to a Rational power, returning a Float rounded to `prec` bits with `rm`.
6679fn rational_rational_pow(
6680 x: &Rational,
6681 y: &Rational,
6682 prec: u64,
6683 rm: RoundingMode,
6684) -> (Float, Ordering) {
6685 assert_ne!(prec, 0);
6686 // Exact rounding: compute with Nearest and demand exactness.
6687 if rm == Exact {
6688 let (result, o) = rational_rational_pow(x, y, prec, Nearest);
6689 assert_eq!(o, Equal, "Inexact rational_rational_pow");
6690 return (result, Equal);
6691 }
6692 // x^0 = 1 for any x, even 0.
6693 if *y == 0u32 {
6694 return (Float::one_prec(prec), Equal);
6695 }
6696 // x = 0: a Rational zero is unsigned, so the results take positive signs.
6697 if *x == 0u32 {
6698 return if *y > 0u32 {
6699 (Float::ZERO, Equal)
6700 } else {
6701 (Float::INFINITY, Equal)
6702 };
6703 }
6704 let y_is_integer = *y.denominator_ref() == 1u32;
6705 // Negative x: only an integer y is defined; the sign is that of (-1)^y.
6706 if *x < 0u32 {
6707 if !y_is_integer {
6708 return (Float::NAN, Equal);
6709 }
6710 let negative = rational_odd_integer(y);
6711 let (result, o) = rational_rational_pow(&(-x), y, prec, if negative { -rm } else { rm });
6712 return if negative {
6713 (-result, o.reverse())
6714 } else {
6715 (result, o)
6716 };
6717 }
6718 if *x == 1u32 {
6719 return (Float::one_prec(prec), Equal);
6720 }
6721 // x = 2^e exactly: x^y = 2^(e * y) with e * y an exact Rational;
6722 // `power_of_2_rational_prec_round` handles all exactness, overflow, and underflow.
6723 if let Some(e) = x.checked_log_base_2() {
6724 let t = Rational::from(e) * y;
6725 return Float::power_of_2_rational_prec_round(t, prec, rm);
6726 }
6727 // Small integer y with a small base: materialize x^y as an exact Rational;
6728 // `from_rational_prec_round` handles all rounding, including at the range boundaries.
6729 let nbits = x.significant_bits();
6730 if y_is_integer
6731 && let Ok(z) = i64::try_from(y.numerator_ref())
6732 && z.unsigned_abs().saturating_mul(nbits) <= max(65536, prec << 2)
6733 {
6734 let z = if *y > 0u32 { z } else { -z };
6735 return Float::from_rational_prec_round(x.pow(z), prec, rm);
6736 }
6737 let fl = x.floor_log_base_2_abs();
6738 let in_range = fl > Float::MIN_EXPONENT_PLUS_2_I64 && fl < Float::MAX_EXPONENT_MINUS_2_I64;
6739 // A base within a few binades of 1 is a sliver whose logarithm is at or below the smallest
6740 // positive Float; it must go through the exact-Rational t-space squeeze (which brackets log2
6741 // over Rationals) rather than any Float-based route, which would underflow the logarithm. `x`
6742 // is a sliver only when it lies in `(1/2, 2)`, i.e. `fl` is 0 or -1.
6743 let sliver_fld = if fl == 0 || fl == -1 {
6744 Some((x - Rational::ONE).floor_log_base_2_abs())
6745 } else {
6746 None
6747 };
6748 let sliver_of_one = sliver_fld.is_some_and(|fld| fld < Float::MIN_EXPONENT_PLUS_8_I64);
6749 // A dyadic in-range non-sliver base is exactly convertible to a Float; `Float::pow_rational`
6750 // does the rest, exactness and boundary behavior included.
6751 if in_range && !sliver_of_one && x.denominator_ref().is_power_of_2() {
6752 let xf = Float::from_rational_prec_round_ref(x, nbits, Floor).0;
6753 return xf.pow_rational_prec_round_val_ref(y, prec, rm);
6754 }
6755 // Possible exact dyadic results must be handled directly: a Ziv squeeze never terminates on an
6756 // exactly-representable value and can stall on a nearest-mode tie.
6757 let n = x.numerator_ref();
6758 let d = x.denominator_ref();
6759 let alpha = i64::exact_from(n.trailing_zeros().unwrap());
6760 let beta = i64::exact_from(d.trailing_zeros().unwrap());
6761 let a = n >> alpha;
6762 let b = d >> beta;
6763 if let Some((m, z, pow)) = rational_rational_pow_exact_decomposition(&a, &b, alpha - beta, y)
6764 && let Some(result) = rational_pow_exact(&m, &z, &pow, prec, rm)
6765 {
6766 return result;
6767 }
6768 // Tiny-result shortcut for a sliver of 1: if |y * log2(x)| is far below 1, x^y rounds to 1 +/-
6769 // ulp, avoiding the (up to 128-MB) log2 brackets. With fld = floor_log2|x - 1|, one has
6770 // |log2(x)| < 2^(fld + 2), so |y * log2(x)| < 2^(ey + fld + 2).
6771 if let Some(fld) = sliver_fld {
6772 let ey = y.floor_log_base_2_abs() + 1;
6773 if ey + fld + 2 < -i64::exact_from(prec) - 1 {
6774 let above = (*y > 0u32) == (*x > 1u32);
6775 return float_one_plus_tiny(prec, rm, above);
6776 }
6777 }
6778 // The result is irrational (or a non-dyadic rational): squeeze 2^(y * log2(x)) in the exponent
6779 // (t-space) over exact Rationals. Splitting off the odd part keeps the log2 bracketing exact
6780 // for extreme or sliver bases, where a Float logarithm would underflow.
6781 let xp = Rational::from(a) / Rational::from(b);
6782 pow_squeeze_t(&xp, alpha - beta, y, prec, rm)
6783}
6784
6785impl Float {
6786 /// Raises a [`Rational`] to a [`Rational`] power, returning the result as a [`Float`] rounded
6787 /// to the specified precision and with the specified rounding mode. Both [`Rational`]s are
6788 /// taken by value. An [`Ordering`] is also returned, indicating whether the rounded power is
6789 /// less than, equal to, or greater than the exact power. Although `NaN`s are not comparable to
6790 /// any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
6791 ///
6792 /// See [`RoundingMode`] for a description of the possible rounding modes.
6793 ///
6794 /// $$
6795 /// f(x,y,p,m) = x^y+\varepsilon.
6796 /// $$
6797 /// - If $x^y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
6798 /// - If $x^y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
6799 /// 2^{\lfloor\log_2 |x^y|\rfloor-p+1}$.
6800 /// - If $x^y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
6801 /// 2^{\lfloor\log_2 |x^y|\rfloor-p}$.
6802 ///
6803 /// If the output has a precision, it is `prec`.
6804 ///
6805 /// Special cases:
6806 /// - $f(x,0,p,m)=1.0$ for any $x$, even $0$
6807 /// - $f(0,y,p,m)=0.0$ if $y>0$, and $\infty$ if $y<0$; a [`Rational`] zero is unsigned, so the
6808 /// results take positive signs
6809 /// - $f(1,y,p,m)=1.0$
6810 /// - $f(-1,y,p,m)=1.0$ if $y$ is an even integer, and $-1.0$ if $y$ is an odd integer
6811 /// - $f(x,y,p,m)=\text{NaN}$ if $x<0$ and $y$ is not an integer
6812 ///
6813 /// Both operands are exact [`Rational`]s, so the exact [`Rational`] exponent selects a definite
6814 /// branch of the power, and results that are exactly representable (such as roots of perfect
6815 /// powers) are detected and rounded exactly.
6816 ///
6817 /// Overflow and underflow:
6818 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
6819 /// returned instead.
6820 /// - 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}$
6821 /// is returned instead.
6822 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
6823 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
6824 /// instead.
6825 /// - If $0<f(x,y,p,m)\leq2^{-2^{30}-1}$ and $m$ is `Nearest`, $0.0$ is returned instead.
6826 /// - If $2^{-2^{30}-1}<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
6827 /// instead.
6828 /// - Negative results (from negative $x$ and odd integer $y$) mirror the bullets above, with
6829 /// the rounding directions reflected.
6830 ///
6831 /// # Worst-case complexity
6832 /// $T(n) = O(n^{3/2} \log n \log\log n)$
6833 ///
6834 /// $M(n) = O(n \log n)$
6835 ///
6836 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(prec, x.significant_bits(),
6837 /// y.significant_bits())`.
6838 ///
6839 /// # Panics
6840 /// Panics if `prec` is zero, or if `rm` is `Exact` but the result cannot be represented exactly
6841 /// with the given precision.
6842 ///
6843 /// # Examples
6844 /// ```
6845 /// use malachite_base::num::basic::traits::OneHalf;
6846 /// use malachite_base::rounding_modes::RoundingMode::*;
6847 /// use malachite_float::Float;
6848 /// use malachite_q::Rational;
6849 /// use std::cmp::Ordering::*;
6850 ///
6851 /// let (p, o) = Float::rational_pow_rational_prec_round(
6852 /// Rational::from_signeds(3, 2),
6853 /// Rational::from_signeds(5, 2),
6854 /// 20,
6855 /// Floor,
6856 /// );
6857 /// assert_eq!(p.to_string(), "2.7556725");
6858 /// assert_eq!(o, Less);
6859 ///
6860 /// let (p, o) = Float::rational_pow_rational_prec_round(
6861 /// Rational::from_signeds(3, 2),
6862 /// Rational::from_signeds(5, 2),
6863 /// 20,
6864 /// Ceiling,
6865 /// );
6866 /// assert_eq!(p.to_string(), "2.7556763");
6867 /// assert_eq!(o, Greater);
6868 ///
6869 /// // (9/4)^(1/2) = 3/2 is exact.
6870 /// let (p, o) = Float::rational_pow_rational_prec_round(
6871 /// Rational::from_signeds(9, 4),
6872 /// Rational::ONE_HALF,
6873 /// 10,
6874 /// Floor,
6875 /// );
6876 /// assert_eq!(p.to_string(), "1.5000");
6877 /// assert_eq!(o, Equal);
6878 /// ```
6879 #[inline]
6880 #[allow(clippy::needless_pass_by_value)]
6881 pub fn rational_pow_rational_prec_round(
6882 x: Rational,
6883 y: Rational,
6884 prec: u64,
6885 rm: RoundingMode,
6886 ) -> (Self, Ordering) {
6887 rational_rational_pow(&x, &y, prec, rm)
6888 }
6889
6890 /// Raises a [`Rational`] to a [`Rational`] power, returning the result as a [`Float`] rounded
6891 /// to the specified precision and with the specified rounding mode. Both [`Rational`]s are
6892 /// taken by reference. An [`Ordering`] is also returned, indicating whether the rounded power
6893 /// is less than, equal to, or greater than the exact power. Although `NaN`s are not comparable
6894 /// to any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
6895 ///
6896 /// See [`Float::rational_pow_rational_prec_round`] for special cases, overflow, and underflow.
6897 ///
6898 /// # Worst-case complexity
6899 /// $T(n) = O(n^{3/2} \log n \log\log n)$
6900 ///
6901 /// $M(n) = O(n \log n)$
6902 ///
6903 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(prec, x.significant_bits(),
6904 /// y.significant_bits())`.
6905 ///
6906 /// # Panics
6907 /// Panics if `prec` is zero, or if `rm` is `Exact` but the result cannot be represented exactly
6908 /// with the given precision.
6909 ///
6910 /// # Examples
6911 /// ```
6912 /// use malachite_base::rounding_modes::RoundingMode::*;
6913 /// use malachite_float::Float;
6914 /// use malachite_q::Rational;
6915 /// use std::cmp::Ordering::*;
6916 ///
6917 /// let (p, o) = Float::rational_pow_rational_prec_round_ref(
6918 /// &Rational::from_signeds(2, 3),
6919 /// &Rational::from_signeds(-1, 2),
6920 /// 20,
6921 /// Ceiling,
6922 /// );
6923 /// assert_eq!(p.to_string(), "1.2247467");
6924 /// assert_eq!(o, Greater);
6925 /// ```
6926 #[inline]
6927 pub fn rational_pow_rational_prec_round_ref(
6928 x: &Rational,
6929 y: &Rational,
6930 prec: u64,
6931 rm: RoundingMode,
6932 ) -> (Self, Ordering) {
6933 rational_rational_pow(x, y, prec, rm)
6934 }
6935
6936 /// Raises a [`Rational`] to a [`Rational`] power, returning the result as a [`Float`] rounded
6937 /// to the specified precision and to the nearest value. Both [`Rational`]s are taken by value.
6938 /// An [`Ordering`] is also returned, indicating whether the rounded power is less than, equal
6939 /// to, or greater than the exact power. Although `NaN`s are not comparable to any [`Float`],
6940 /// whenever this function returns a `NaN` it also returns `Equal`.
6941 ///
6942 /// If the power is equidistant from two [`Float`]s with the specified precision, the [`Float`]
6943 /// with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of
6944 /// the `Nearest` rounding mode.
6945 ///
6946 /// See [`Float::rational_pow_rational_prec_round`] for special cases, overflow, and underflow.
6947 ///
6948 /// # Worst-case complexity
6949 /// $T(n) = O(n^{3/2} \log n \log\log n)$
6950 ///
6951 /// $M(n) = O(n \log n)$
6952 ///
6953 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(prec, x.significant_bits(),
6954 /// y.significant_bits())`.
6955 ///
6956 /// # Panics
6957 /// Panics if `prec` is zero.
6958 ///
6959 /// # Examples
6960 /// ```
6961 /// use malachite_float::Float;
6962 /// use malachite_q::Rational;
6963 /// use std::cmp::Ordering::*;
6964 ///
6965 /// let (p, o) = Float::rational_pow_rational_prec(
6966 /// Rational::from_signeds(3, 2),
6967 /// Rational::from_signeds(5, 2),
6968 /// 20,
6969 /// );
6970 /// assert_eq!(p.to_string(), "2.7556763");
6971 /// assert_eq!(o, Greater);
6972 ///
6973 /// let (p, o) =
6974 /// Float::rational_pow_rational_prec(Rational::from(8), Rational::from_signeds(1, 3), 10);
6975 /// assert_eq!(p.to_string(), "2.0000");
6976 /// assert_eq!(o, Equal);
6977 /// ```
6978 #[inline]
6979 #[allow(clippy::needless_pass_by_value)]
6980 pub fn rational_pow_rational_prec(x: Rational, y: Rational, prec: u64) -> (Self, Ordering) {
6981 rational_rational_pow(&x, &y, prec, Nearest)
6982 }
6983
6984 /// Raises a [`Rational`] to a [`Rational`] power, returning the result as a [`Float`] rounded
6985 /// to the specified precision and to the nearest value. Both [`Rational`]s are taken by
6986 /// reference. An [`Ordering`] is also returned, indicating whether the rounded power is less
6987 /// than, equal to, or greater than the exact power. Although `NaN`s are not comparable to any
6988 /// [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
6989 ///
6990 /// If the power is equidistant from two [`Float`]s with the specified precision, the [`Float`]
6991 /// with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of
6992 /// the `Nearest` rounding mode.
6993 ///
6994 /// See [`Float::rational_pow_rational_prec_round`] for special cases, overflow, and underflow.
6995 ///
6996 /// # Worst-case complexity
6997 /// $T(n) = O(n^{3/2} \log n \log\log n)$
6998 ///
6999 /// $M(n) = O(n \log n)$
7000 ///
7001 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(prec, x.significant_bits(),
7002 /// y.significant_bits())`.
7003 ///
7004 /// # Panics
7005 /// Panics if `prec` is zero.
7006 ///
7007 /// # Examples
7008 /// ```
7009 /// use malachite_float::Float;
7010 /// use malachite_q::Rational;
7011 /// use std::cmp::Ordering::*;
7012 ///
7013 /// let (p, o) = Float::rational_pow_rational_prec_ref(
7014 /// &Rational::from_signeds(3, 2),
7015 /// &Rational::from_signeds(5, 2),
7016 /// 20,
7017 /// );
7018 /// assert_eq!(p.to_string(), "2.7556763");
7019 /// assert_eq!(o, Greater);
7020 /// ```
7021 #[inline]
7022 pub fn rational_pow_rational_prec_ref(
7023 x: &Rational,
7024 y: &Rational,
7025 prec: u64,
7026 ) -> (Self, Ordering) {
7027 rational_rational_pow(x, y, prec, Nearest)
7028 }
7029}
7030
7031impl Float {
7032 // Raises a Rational to a Float power, returning a Float rounded to the specified precision with
7033 // the specified rounding mode.
7034
7035 /// Raises a [`Rational`] to a [`Float`] power, returning the result as a [`Float`] rounded to
7036 /// the specified precision and with the specified rounding mode. The [`Rational`] and the
7037 /// [`Float`] are both taken by reference. An [`Ordering`] is also returned, indicating whether
7038 /// the rounded power is less than, equal to, or greater than the exact power. Although `NaN`s
7039 /// are not comparable to any [`Float`], whenever this function returns a `NaN` it also returns
7040 /// `Equal`.
7041 ///
7042 /// See [`RoundingMode`] for a description of the possible rounding modes.
7043 ///
7044 /// $$
7045 /// f(x,y,p,m) = x^y+\varepsilon.
7046 /// $$
7047 /// - If $x^y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
7048 /// - If $x^y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
7049 /// 2^{\lfloor\log_2 |x^y|\rfloor-p+1}$.
7050 /// - If $x^y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
7051 /// 2^{\lfloor\log_2 |x^y|\rfloor-p}$.
7052 ///
7053 /// If the output has a precision, it is `prec`.
7054 ///
7055 /// Special cases:
7056 /// - $f(x,\pm0.0,p,m)=1.0$ for any $x$, even $0$
7057 /// - $f(1,y,p,m)=1.0$ for any $y$, even `NaN`
7058 /// - $f(x,\text{NaN},p,m)=\text{NaN}$ otherwise
7059 /// - $f(x,\infty,p,m)=\infty$ if $|x|>1$, and $0.0$ if $|x|<1$
7060 /// - $f(x,-\infty,p,m)=0.0$ if $|x|>1$, and $\infty$ if $|x|<1$
7061 /// - $f(\pm1,\pm\infty,p,m)=1.0$
7062 /// - $f(-1,y,p,m)=1.0$ if $y$ is an even integer, and $-1.0$ if $y$ is an odd integer
7063 /// - $f(0,y,p,m)=0.0$ if $y>0$, and $\infty$ if $y<0$; a [`Rational`] zero is unsigned, so the
7064 /// results take positive signs
7065 /// - $f(x,y,p,m)=\text{NaN}$ if $x<0$ and $y$ is finite and not an integer
7066 ///
7067 /// Unlike a [`Float`] base, a [`Rational`] base may lie outside the [`Float`] exponent range or
7068 /// so close to 1 that no [`Float`] can represent its logarithm; both cases are handled exactly,
7069 /// by working with the base as an exact [`Rational`] throughout.
7070 ///
7071 /// Overflow and underflow:
7072 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
7073 /// returned instead.
7074 /// - 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}$
7075 /// is returned instead.
7076 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
7077 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
7078 /// instead.
7079 /// - If $0<f(x,y,p,m)\leq2^{-2^{30}-1}$ and $m$ is `Nearest`, $0.0$ is returned instead.
7080 /// - If $2^{-2^{30}-1}<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
7081 /// instead.
7082 /// - Negative results (from negative $x$ and odd integer $y$) mirror the bullets above, with
7083 /// the rounding directions reflected.
7084 ///
7085 /// If you know you'll be using `Nearest`, consider using [`Float::rational_pow_prec_ref_ref`]
7086 /// instead.
7087 ///
7088 /// # Worst-case complexity
7089 /// $T(n) = O(n^{3/2} \log n \log\log n)$
7090 ///
7091 /// $M(n) = O(n \log n)$
7092 ///
7093 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(prec, x.significant_bits(),
7094 /// y.significant_bits())`.
7095 ///
7096 /// # Panics
7097 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the given
7098 /// precision.
7099 ///
7100 /// # Examples
7101 /// ```
7102 /// use malachite_base::rounding_modes::RoundingMode::*;
7103 /// use malachite_float::Float;
7104 /// use malachite_q::Rational;
7105 /// use std::cmp::Ordering::*;
7106 ///
7107 /// let (p, o) = Float::rational_pow_prec_round_ref_ref(
7108 /// &Rational::from_unsigneds(3u32, 2u32),
7109 /// &Float::from(2.5),
7110 /// 5,
7111 /// Floor,
7112 /// );
7113 /// assert_eq!(p.to_string(), "2.75");
7114 /// assert_eq!(o, Less);
7115 ///
7116 /// let (p, o) = Float::rational_pow_prec_round_ref_ref(
7117 /// &Rational::from_unsigneds(3u32, 2u32),
7118 /// &Float::from(2.5),
7119 /// 5,
7120 /// Ceiling,
7121 /// );
7122 /// assert_eq!(p.to_string(), "2.88");
7123 /// assert_eq!(o, Greater);
7124 ///
7125 /// let (p, o) = Float::rational_pow_prec_round_ref_ref(
7126 /// &Rational::from_unsigneds(3u32, 2u32),
7127 /// &Float::from(2.5),
7128 /// 5,
7129 /// Nearest,
7130 /// );
7131 /// assert_eq!(p.to_string(), "2.75");
7132 /// assert_eq!(o, Less);
7133 ///
7134 /// let (p, o) = Float::rational_pow_prec_round_ref_ref(
7135 /// &Rational::from_unsigneds(3u32, 2u32),
7136 /// &Float::from(2.5),
7137 /// 20,
7138 /// Floor,
7139 /// );
7140 /// assert_eq!(p.to_string(), "2.7556725");
7141 /// assert_eq!(o, Less);
7142 ///
7143 /// let (p, o) = Float::rational_pow_prec_round_ref_ref(
7144 /// &Rational::from_unsigneds(3u32, 2u32),
7145 /// &Float::from(2.5),
7146 /// 20,
7147 /// Ceiling,
7148 /// );
7149 /// assert_eq!(p.to_string(), "2.7556763");
7150 /// assert_eq!(o, Greater);
7151 ///
7152 /// let (p, o) = Float::rational_pow_prec_round_ref_ref(
7153 /// &Rational::from_unsigneds(3u32, 2u32),
7154 /// &Float::from(2.5),
7155 /// 20,
7156 /// Nearest,
7157 /// );
7158 /// assert_eq!(p.to_string(), "2.7556763");
7159 /// assert_eq!(o, Greater);
7160 /// ```
7161 pub fn rational_pow_prec_round_ref_ref(
7162 x: &Rational,
7163 y: &Self,
7164 prec: u64,
7165 rm: RoundingMode,
7166 ) -> (Self, Ordering) {
7167 assert_ne!(prec, 0);
7168 // Exact rounding: compute with Nearest and demand exactness.
7169 if rm == Exact {
7170 let (result, o) = Self::rational_pow_prec_ref_ref(x, y, prec);
7171 assert_eq!(o, Equal, "Inexact rational_pow");
7172 return (result, Equal);
7173 }
7174 // Singular y; see Section F.9.4.4 of the C standard.
7175 match y {
7176 // x^0 = 1 for any x, even 0
7177 float_either_zero!() => {
7178 return (Self::one_prec(prec), Equal);
7179 }
7180 // 1^y = 1 for any y, even NaN
7181 float_nan!() => {
7182 return if *x == 1u32 {
7183 (Self::one_prec(prec), Equal)
7184 } else {
7185 (Self::NAN, Equal)
7186 };
7187 }
7188 Self(Infinity { sign }) => {
7189 let mut cmp = x.cmp_abs(&Rational::ONE);
7190 if !*sign {
7191 cmp = cmp.reverse();
7192 }
7193 return match cmp {
7194 Greater => (Self::INFINITY, Equal),
7195 Less => (Self::ZERO, Equal),
7196 Equal => (Self::one_prec(prec), Equal),
7197 };
7198 }
7199 _ => {}
7200 }
7201 // x = 0: Rational zero is unsigned, so the results take positive signs.
7202 if *x == 0u32 {
7203 return if *y > 0u32 {
7204 (Self::ZERO, Equal)
7205 } else {
7206 (Self::INFINITY, Equal)
7207 };
7208 }
7209 let y_is_integer = y.is_integer();
7210 // Negative x: only integer y is defined; the sign is that of (-1)^y.
7211 if *x < 0u32 {
7212 if !y_is_integer {
7213 return (Self::NAN, Equal);
7214 }
7215 let negative = float_odd_integer(y);
7216 let (result, o) = Self::rational_pow_prec_round_ref_ref(
7217 &(-x),
7218 y,
7219 prec,
7220 if negative { -rm } else { rm },
7221 );
7222 return if negative {
7223 (-result, o.reverse())
7224 } else {
7225 (result, o)
7226 };
7227 }
7228 if *x == 1u32 {
7229 return (Self::one_prec(prec), Equal);
7230 }
7231 // x = 2^e exactly: x^y = 2^(e * y) with e * y an exact Rational;
7232 // `power_of_2_rational_prec_round` handles all exactness, overflow, and underflow.
7233 if let Some(e) = x.checked_log_base_2() {
7234 let t = Rational::from(e) * Rational::exact_from(y);
7235 return Self::power_of_2_rational_prec_round(t, prec, rm);
7236 }
7237 // Small integer y with a small base: materialize x^y as an exact Rational;
7238 // `from_rational_prec_round` handles all rounding, including at the range boundaries.
7239 let nbits = x.significant_bits();
7240 if y_is_integer && y.get_exponent().unwrap() <= 32 {
7241 let z = i64::rounding_from(y, Nearest).0;
7242 if z.unsigned_abs().saturating_mul(nbits) <= max(65536, prec << 2) {
7243 return Self::from_rational_prec_round(x.pow(z), prec, rm);
7244 }
7245 }
7246 let fl = x.floor_log_base_2_abs();
7247 let in_range = fl > Self::MIN_EXPONENT_PLUS_2_I64 && fl < Self::MAX_EXPONENT_MINUS_2_I64;
7248 // A base within a few binades of 1 (from either side) has a logarithm at or below the
7249 // smallest positive Float, where any Float-based power -- the dyadic shortcut or the
7250 // x-space squeeze below, both of which call `Float::pow` -- would underflow internally
7251 // (`ln` cannot represent the sub-`MIN_EXPONENT` result). Such a base goes through the
7252 // exact-Rational t-space squeeze, which brackets `log2` with the atanh series over
7253 // `Rational`s and never materializes a sub-`MIN_EXPONENT` Float logarithm. `x` is a sliver
7254 // of 1 only when it lies in `(1/2, 2)`, i.e. `fl` is 0 or -1; the exact subtraction is
7255 // skipped otherwise.
7256 let sliver_fld = if fl == 0 || fl == -1 {
7257 if *x == 1u32 {
7258 None
7259 } else {
7260 Some((x - Rational::ONE).floor_log_base_2_abs())
7261 }
7262 } else {
7263 None
7264 };
7265 let sliver_of_one = sliver_fld.is_some_and(|fld| fld < Self::MIN_EXPONENT_PLUS_8_I64);
7266 // A dyadic in-range non-sliver x is exactly convertible; Float::pow does the rest,
7267 // exactness and boundary behavior included.
7268 if in_range && !sliver_of_one && x.denominator_ref().is_power_of_2() {
7269 let xf = Self::from_rational_prec_round_ref(x, nbits, Floor).0;
7270 return xf.pow_prec_round_val_ref(y, prec, rm);
7271 }
7272 // Possible exact dyadic results must be handled directly: a Ziv squeeze never terminates on
7273 // an exactly-representable value and can stall on a nearest-mode tie.
7274 let n = x.numerator_ref();
7275 let d = x.denominator_ref();
7276 let alpha = i64::exact_from(n.trailing_zeros().unwrap());
7277 let beta = i64::exact_from(d.trailing_zeros().unwrap());
7278 let a = n >> alpha;
7279 let b = d >> beta;
7280 if let Some((m, z, pow)) = rational_pow_exact_decomposition(&a, &b, alpha - beta, y)
7281 && let Some(result) = rational_pow_exact(&m, &z, &pow, prec, rm)
7282 {
7283 return result;
7284 }
7285 if in_range && !sliver_of_one {
7286 rational_pow_squeeze_x(x, y, prec, rm)
7287 } else {
7288 // Tiny-result shortcut for a sliver of 1: if |y * log2(x)| is far below 1, x^y rounds
7289 // to 1 +/- ulp, avoiding the (up to 128-MB) log2 brackets. With fld = floor_log2|x -
7290 // 1|, one has |log2(x)| < 2^(fld + 2), so |y * log2(x)| < 2^(ey + fld + 2); when that
7291 // is below 2^(-prec - 1) the result is within half an ulp of 1.
7292 if let Some(fld) = sliver_fld {
7293 let ey = i64::from(y.get_exponent().unwrap());
7294 if ey + fld + 2 < -i64::exact_from(prec) - 1 {
7295 let above = (*y > 0u32) == (*x > 1u32);
7296 return float_one_plus_tiny(prec, rm, above);
7297 }
7298 }
7299 // Extreme x -- beyond the exponent range or a sliver of 1: split off the power of 2
7300 // (rounded to the nearest, so the mantissa is close to 1) and work with exact Rationals
7301 // in the exponent.
7302 let (xp, g) = rational_mantissa_nearest_power_of_2(x);
7303 pow_squeeze_t(&xp, g, &Rational::exact_from(y), prec, rm)
7304 }
7305 }
7306
7307 #[allow(clippy::needless_pass_by_value)]
7308 /// Raises a [`Rational`] to a [`Float`] power, returning the result as a [`Float`] rounded to
7309 /// the specified precision and with the specified rounding mode. The [`Rational`] and the
7310 /// [`Float`] are both taken by value. An [`Ordering`] is also returned, indicating whether the
7311 /// rounded power is less than, equal to, or greater than the exact power. Although `NaN`s are
7312 /// not comparable to any [`Float`], whenever this function returns a `NaN` it also returns
7313 /// `Equal`.
7314 ///
7315 /// See [`RoundingMode`] for a description of the possible rounding modes.
7316 ///
7317 /// $$
7318 /// f(x,y,p,m) = x^y+\varepsilon.
7319 /// $$
7320 /// - If $x^y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
7321 /// - If $x^y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
7322 /// 2^{\lfloor\log_2 |x^y|\rfloor-p+1}$.
7323 /// - If $x^y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
7324 /// 2^{\lfloor\log_2 |x^y|\rfloor-p}$.
7325 ///
7326 /// If the output has a precision, it is `prec`.
7327 ///
7328 /// Special cases:
7329 /// - $f(x,\pm0.0,p,m)=1.0$ for any $x$, even $0$
7330 /// - $f(1,y,p,m)=1.0$ for any $y$, even `NaN`
7331 /// - $f(x,\text{NaN},p,m)=\text{NaN}$ otherwise
7332 /// - $f(x,\infty,p,m)=\infty$ if $|x|>1$, and $0.0$ if $|x|<1$
7333 /// - $f(x,-\infty,p,m)=0.0$ if $|x|>1$, and $\infty$ if $|x|<1$
7334 /// - $f(\pm1,\pm\infty,p,m)=1.0$
7335 /// - $f(-1,y,p,m)=1.0$ if $y$ is an even integer, and $-1.0$ if $y$ is an odd integer
7336 /// - $f(0,y,p,m)=0.0$ if $y>0$, and $\infty$ if $y<0$; a [`Rational`] zero is unsigned, so the
7337 /// results take positive signs
7338 /// - $f(x,y,p,m)=\text{NaN}$ if $x<0$ and $y$ is finite and not an integer
7339 ///
7340 /// Unlike a [`Float`] base, a [`Rational`] base may lie outside the [`Float`] exponent range or
7341 /// so close to 1 that no [`Float`] can represent its logarithm; both cases are handled exactly,
7342 /// by working with the base as an exact [`Rational`] throughout.
7343 ///
7344 /// Overflow and underflow:
7345 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
7346 /// returned instead.
7347 /// - 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}$
7348 /// is returned instead.
7349 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
7350 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
7351 /// instead.
7352 /// - If $0<f(x,y,p,m)\leq2^{-2^{30}-1}$ and $m$ is `Nearest`, $0.0$ is returned instead.
7353 /// - If $2^{-2^{30}-1}<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
7354 /// instead.
7355 /// - Negative results (from negative $x$ and odd integer $y$) mirror the bullets above, with
7356 /// the rounding directions reflected.
7357 ///
7358 /// If you know you'll be using `Nearest`, consider using [`Float::rational_pow_prec`] instead.
7359 ///
7360 /// # Worst-case complexity
7361 /// $T(n) = O(n^{3/2} \log n \log\log n)$
7362 ///
7363 /// $M(n) = O(n \log n)$
7364 ///
7365 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(prec, x.significant_bits(),
7366 /// y.significant_bits())`.
7367 ///
7368 /// # Panics
7369 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the given
7370 /// precision.
7371 ///
7372 /// # Examples
7373 /// ```
7374 /// use malachite_base::rounding_modes::RoundingMode::*;
7375 /// use malachite_float::Float;
7376 /// use malachite_q::Rational;
7377 /// use std::cmp::Ordering::*;
7378 ///
7379 /// let (p, o) = Float::rational_pow_prec_round(
7380 /// Rational::from_unsigneds(3u32, 2u32),
7381 /// Float::from(2.5),
7382 /// 5,
7383 /// Floor,
7384 /// );
7385 /// assert_eq!(p.to_string(), "2.75");
7386 /// assert_eq!(o, Less);
7387 ///
7388 /// let (p, o) = Float::rational_pow_prec_round(
7389 /// Rational::from_unsigneds(3u32, 2u32),
7390 /// Float::from(2.5),
7391 /// 5,
7392 /// Ceiling,
7393 /// );
7394 /// assert_eq!(p.to_string(), "2.88");
7395 /// assert_eq!(o, Greater);
7396 ///
7397 /// let (p, o) = Float::rational_pow_prec_round(
7398 /// Rational::from_unsigneds(3u32, 2u32),
7399 /// Float::from(2.5),
7400 /// 5,
7401 /// Nearest,
7402 /// );
7403 /// assert_eq!(p.to_string(), "2.75");
7404 /// assert_eq!(o, Less);
7405 ///
7406 /// let (p, o) = Float::rational_pow_prec_round(
7407 /// Rational::from_unsigneds(3u32, 2u32),
7408 /// Float::from(2.5),
7409 /// 20,
7410 /// Floor,
7411 /// );
7412 /// assert_eq!(p.to_string(), "2.7556725");
7413 /// assert_eq!(o, Less);
7414 ///
7415 /// let (p, o) = Float::rational_pow_prec_round(
7416 /// Rational::from_unsigneds(3u32, 2u32),
7417 /// Float::from(2.5),
7418 /// 20,
7419 /// Ceiling,
7420 /// );
7421 /// assert_eq!(p.to_string(), "2.7556763");
7422 /// assert_eq!(o, Greater);
7423 ///
7424 /// let (p, o) = Float::rational_pow_prec_round(
7425 /// Rational::from_unsigneds(3u32, 2u32),
7426 /// Float::from(2.5),
7427 /// 20,
7428 /// Nearest,
7429 /// );
7430 /// assert_eq!(p.to_string(), "2.7556763");
7431 /// assert_eq!(o, Greater);
7432 /// ```
7433 #[inline]
7434 pub fn rational_pow_prec_round(
7435 x: Rational,
7436 y: Self,
7437 prec: u64,
7438 rm: RoundingMode,
7439 ) -> (Self, Ordering) {
7440 Self::rational_pow_prec_round_ref_ref(&x, &y, prec, rm)
7441 }
7442
7443 #[allow(clippy::needless_pass_by_value)]
7444 /// Raises a [`Rational`] to a [`Float`] power, returning the result as a [`Float`] rounded to
7445 /// the specified precision and with the specified rounding mode. The [`Rational`] is taken by
7446 /// value and the [`Float`] by reference. An [`Ordering`] is also returned, indicating whether
7447 /// the rounded power is less than, equal to, or greater than the exact power. Although `NaN`s
7448 /// are not comparable to any [`Float`], whenever this function returns a `NaN` it also returns
7449 /// `Equal`.
7450 ///
7451 /// See [`RoundingMode`] for a description of the possible rounding modes.
7452 ///
7453 /// $$
7454 /// f(x,y,p,m) = x^y+\varepsilon.
7455 /// $$
7456 /// - If $x^y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
7457 /// - If $x^y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
7458 /// 2^{\lfloor\log_2 |x^y|\rfloor-p+1}$.
7459 /// - If $x^y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
7460 /// 2^{\lfloor\log_2 |x^y|\rfloor-p}$.
7461 ///
7462 /// If the output has a precision, it is `prec`.
7463 ///
7464 /// Special cases:
7465 /// - $f(x,\pm0.0,p,m)=1.0$ for any $x$, even $0$
7466 /// - $f(1,y,p,m)=1.0$ for any $y$, even `NaN`
7467 /// - $f(x,\text{NaN},p,m)=\text{NaN}$ otherwise
7468 /// - $f(x,\infty,p,m)=\infty$ if $|x|>1$, and $0.0$ if $|x|<1$
7469 /// - $f(x,-\infty,p,m)=0.0$ if $|x|>1$, and $\infty$ if $|x|<1$
7470 /// - $f(\pm1,\pm\infty,p,m)=1.0$
7471 /// - $f(-1,y,p,m)=1.0$ if $y$ is an even integer, and $-1.0$ if $y$ is an odd integer
7472 /// - $f(0,y,p,m)=0.0$ if $y>0$, and $\infty$ if $y<0$; a [`Rational`] zero is unsigned, so the
7473 /// results take positive signs
7474 /// - $f(x,y,p,m)=\text{NaN}$ if $x<0$ and $y$ is finite and not an integer
7475 ///
7476 /// Unlike a [`Float`] base, a [`Rational`] base may lie outside the [`Float`] exponent range or
7477 /// so close to 1 that no [`Float`] can represent its logarithm; both cases are handled exactly,
7478 /// by working with the base as an exact [`Rational`] throughout.
7479 ///
7480 /// Overflow and underflow:
7481 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
7482 /// returned instead.
7483 /// - 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}$
7484 /// is returned instead.
7485 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
7486 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
7487 /// instead.
7488 /// - If $0<f(x,y,p,m)\leq2^{-2^{30}-1}$ and $m$ is `Nearest`, $0.0$ is returned instead.
7489 /// - If $2^{-2^{30}-1}<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
7490 /// instead.
7491 /// - Negative results (from negative $x$ and odd integer $y$) mirror the bullets above, with
7492 /// the rounding directions reflected.
7493 ///
7494 /// If you know you'll be using `Nearest`, consider using [`Float::rational_pow_prec_val_ref`]
7495 /// instead.
7496 ///
7497 /// # Worst-case complexity
7498 /// $T(n) = O(n^{3/2} \log n \log\log n)$
7499 ///
7500 /// $M(n) = O(n \log n)$
7501 ///
7502 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(prec, x.significant_bits(),
7503 /// y.significant_bits())`.
7504 ///
7505 /// # Panics
7506 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the given
7507 /// precision.
7508 ///
7509 /// # Examples
7510 /// ```
7511 /// use malachite_base::rounding_modes::RoundingMode::*;
7512 /// use malachite_float::Float;
7513 /// use malachite_q::Rational;
7514 /// use std::cmp::Ordering::*;
7515 ///
7516 /// let (p, o) = Float::rational_pow_prec_round_val_ref(
7517 /// Rational::from_unsigneds(3u32, 2u32),
7518 /// &Float::from(2.5),
7519 /// 5,
7520 /// Floor,
7521 /// );
7522 /// assert_eq!(p.to_string(), "2.75");
7523 /// assert_eq!(o, Less);
7524 ///
7525 /// let (p, o) = Float::rational_pow_prec_round_val_ref(
7526 /// Rational::from_unsigneds(3u32, 2u32),
7527 /// &Float::from(2.5),
7528 /// 5,
7529 /// Ceiling,
7530 /// );
7531 /// assert_eq!(p.to_string(), "2.88");
7532 /// assert_eq!(o, Greater);
7533 ///
7534 /// let (p, o) = Float::rational_pow_prec_round_val_ref(
7535 /// Rational::from_unsigneds(3u32, 2u32),
7536 /// &Float::from(2.5),
7537 /// 5,
7538 /// Nearest,
7539 /// );
7540 /// assert_eq!(p.to_string(), "2.75");
7541 /// assert_eq!(o, Less);
7542 ///
7543 /// let (p, o) = Float::rational_pow_prec_round_val_ref(
7544 /// Rational::from_unsigneds(3u32, 2u32),
7545 /// &Float::from(2.5),
7546 /// 20,
7547 /// Floor,
7548 /// );
7549 /// assert_eq!(p.to_string(), "2.7556725");
7550 /// assert_eq!(o, Less);
7551 ///
7552 /// let (p, o) = Float::rational_pow_prec_round_val_ref(
7553 /// Rational::from_unsigneds(3u32, 2u32),
7554 /// &Float::from(2.5),
7555 /// 20,
7556 /// Ceiling,
7557 /// );
7558 /// assert_eq!(p.to_string(), "2.7556763");
7559 /// assert_eq!(o, Greater);
7560 ///
7561 /// let (p, o) = Float::rational_pow_prec_round_val_ref(
7562 /// Rational::from_unsigneds(3u32, 2u32),
7563 /// &Float::from(2.5),
7564 /// 20,
7565 /// Nearest,
7566 /// );
7567 /// assert_eq!(p.to_string(), "2.7556763");
7568 /// assert_eq!(o, Greater);
7569 /// ```
7570 #[inline]
7571 pub fn rational_pow_prec_round_val_ref(
7572 x: Rational,
7573 y: &Self,
7574 prec: u64,
7575 rm: RoundingMode,
7576 ) -> (Self, Ordering) {
7577 Self::rational_pow_prec_round_ref_ref(&x, y, prec, rm)
7578 }
7579
7580 #[allow(clippy::needless_pass_by_value)]
7581 /// Raises a [`Rational`] to a [`Float`] power, returning the result as a [`Float`] rounded to
7582 /// the specified precision and with the specified rounding mode. The [`Rational`] is taken by
7583 /// reference and the [`Float`] by value. An [`Ordering`] is also returned, indicating whether
7584 /// the rounded power is less than, equal to, or greater than the exact power. Although `NaN`s
7585 /// are not comparable to any [`Float`], whenever this function returns a `NaN` it also returns
7586 /// `Equal`.
7587 ///
7588 /// See [`RoundingMode`] for a description of the possible rounding modes.
7589 ///
7590 /// $$
7591 /// f(x,y,p,m) = x^y+\varepsilon.
7592 /// $$
7593 /// - If $x^y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
7594 /// - If $x^y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
7595 /// 2^{\lfloor\log_2 |x^y|\rfloor-p+1}$.
7596 /// - If $x^y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
7597 /// 2^{\lfloor\log_2 |x^y|\rfloor-p}$.
7598 ///
7599 /// If the output has a precision, it is `prec`.
7600 ///
7601 /// Special cases:
7602 /// - $f(x,\pm0.0,p,m)=1.0$ for any $x$, even $0$
7603 /// - $f(1,y,p,m)=1.0$ for any $y$, even `NaN`
7604 /// - $f(x,\text{NaN},p,m)=\text{NaN}$ otherwise
7605 /// - $f(x,\infty,p,m)=\infty$ if $|x|>1$, and $0.0$ if $|x|<1$
7606 /// - $f(x,-\infty,p,m)=0.0$ if $|x|>1$, and $\infty$ if $|x|<1$
7607 /// - $f(\pm1,\pm\infty,p,m)=1.0$
7608 /// - $f(-1,y,p,m)=1.0$ if $y$ is an even integer, and $-1.0$ if $y$ is an odd integer
7609 /// - $f(0,y,p,m)=0.0$ if $y>0$, and $\infty$ if $y<0$; a [`Rational`] zero is unsigned, so the
7610 /// results take positive signs
7611 /// - $f(x,y,p,m)=\text{NaN}$ if $x<0$ and $y$ is finite and not an integer
7612 ///
7613 /// Unlike a [`Float`] base, a [`Rational`] base may lie outside the [`Float`] exponent range or
7614 /// so close to 1 that no [`Float`] can represent its logarithm; both cases are handled exactly,
7615 /// by working with the base as an exact [`Rational`] throughout.
7616 ///
7617 /// Overflow and underflow:
7618 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
7619 /// returned instead.
7620 /// - 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}$
7621 /// is returned instead.
7622 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
7623 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
7624 /// instead.
7625 /// - If $0<f(x,y,p,m)\leq2^{-2^{30}-1}$ and $m$ is `Nearest`, $0.0$ is returned instead.
7626 /// - If $2^{-2^{30}-1}<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
7627 /// instead.
7628 /// - Negative results (from negative $x$ and odd integer $y$) mirror the bullets above, with
7629 /// the rounding directions reflected.
7630 ///
7631 /// If you know you'll be using `Nearest`, consider using [`Float::rational_pow_prec_ref_val`]
7632 /// instead.
7633 ///
7634 /// # Worst-case complexity
7635 /// $T(n) = O(n^{3/2} \log n \log\log n)$
7636 ///
7637 /// $M(n) = O(n \log n)$
7638 ///
7639 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(prec, x.significant_bits(),
7640 /// y.significant_bits())`.
7641 ///
7642 /// # Panics
7643 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the given
7644 /// precision.
7645 ///
7646 /// # Examples
7647 /// ```
7648 /// use malachite_base::rounding_modes::RoundingMode::*;
7649 /// use malachite_float::Float;
7650 /// use malachite_q::Rational;
7651 /// use std::cmp::Ordering::*;
7652 ///
7653 /// let (p, o) = Float::rational_pow_prec_round_ref_val(
7654 /// &Rational::from_unsigneds(3u32, 2u32),
7655 /// Float::from(2.5),
7656 /// 5,
7657 /// Floor,
7658 /// );
7659 /// assert_eq!(p.to_string(), "2.75");
7660 /// assert_eq!(o, Less);
7661 ///
7662 /// let (p, o) = Float::rational_pow_prec_round_ref_val(
7663 /// &Rational::from_unsigneds(3u32, 2u32),
7664 /// Float::from(2.5),
7665 /// 5,
7666 /// Ceiling,
7667 /// );
7668 /// assert_eq!(p.to_string(), "2.88");
7669 /// assert_eq!(o, Greater);
7670 ///
7671 /// let (p, o) = Float::rational_pow_prec_round_ref_val(
7672 /// &Rational::from_unsigneds(3u32, 2u32),
7673 /// Float::from(2.5),
7674 /// 5,
7675 /// Nearest,
7676 /// );
7677 /// assert_eq!(p.to_string(), "2.75");
7678 /// assert_eq!(o, Less);
7679 ///
7680 /// let (p, o) = Float::rational_pow_prec_round_ref_val(
7681 /// &Rational::from_unsigneds(3u32, 2u32),
7682 /// Float::from(2.5),
7683 /// 20,
7684 /// Floor,
7685 /// );
7686 /// assert_eq!(p.to_string(), "2.7556725");
7687 /// assert_eq!(o, Less);
7688 ///
7689 /// let (p, o) = Float::rational_pow_prec_round_ref_val(
7690 /// &Rational::from_unsigneds(3u32, 2u32),
7691 /// Float::from(2.5),
7692 /// 20,
7693 /// Ceiling,
7694 /// );
7695 /// assert_eq!(p.to_string(), "2.7556763");
7696 /// assert_eq!(o, Greater);
7697 ///
7698 /// let (p, o) = Float::rational_pow_prec_round_ref_val(
7699 /// &Rational::from_unsigneds(3u32, 2u32),
7700 /// Float::from(2.5),
7701 /// 20,
7702 /// Nearest,
7703 /// );
7704 /// assert_eq!(p.to_string(), "2.7556763");
7705 /// assert_eq!(o, Greater);
7706 /// ```
7707 #[inline]
7708 pub fn rational_pow_prec_round_ref_val(
7709 x: &Rational,
7710 y: Self,
7711 prec: u64,
7712 rm: RoundingMode,
7713 ) -> (Self, Ordering) {
7714 Self::rational_pow_prec_round_ref_ref(x, &y, prec, rm)
7715 }
7716
7717 #[allow(clippy::needless_pass_by_value)]
7718 /// Raises a [`Rational`] to a [`Float`] power, returning the result as a [`Float`] rounded to
7719 /// the specified precision and to the nearest value. The [`Rational`] and the [`Float`] are
7720 /// both taken by value. An [`Ordering`] is also returned, indicating whether the rounded power
7721 /// is less than, equal to, or greater than the exact power. Although `NaN`s are not comparable
7722 /// to any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
7723 ///
7724 /// If the power is equidistant from two [`Float`]s with the specified precision, the [`Float`]
7725 /// with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of
7726 /// the `Nearest` rounding mode.
7727 ///
7728 /// $$
7729 /// f(x,y,p) = x^y+\varepsilon.
7730 /// $$
7731 /// - If $x^y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
7732 /// - If $x^y$ is finite and nonzero, then $|\varepsilon| \leq 2^{\lfloor\log_2
7733 /// |x^y|\rfloor-p}$.
7734 ///
7735 /// If the output has a precision, it is `prec`.
7736 ///
7737 /// Special cases:
7738 /// - $f(x,\pm0.0,p)=1.0$ for any $x$, even $0$
7739 /// - $f(1,y,p)=1.0$ for any $y$, even `NaN`
7740 /// - $f(x,\text{NaN},p)=\text{NaN}$ otherwise
7741 /// - $f(x,\infty,p)=\infty$ if $|x|>1$, and $0.0$ if $|x|<1$
7742 /// - $f(x,-\infty,p)=0.0$ if $|x|>1$, and $\infty$ if $|x|<1$
7743 /// - $f(\pm1,\pm\infty,p)=1.0$
7744 /// - $f(-1,y,p)=1.0$ if $y$ is an even integer, and $-1.0$ if $y$ is an odd integer
7745 /// - $f(0,y,p)=0.0$ if $y>0$, and $\infty$ if $y<0$; a [`Rational`] zero is unsigned, so the
7746 /// results take positive signs
7747 /// - $f(x,y,p)=\text{NaN}$ if $x<0$ and $y$ is finite and not an integer
7748 ///
7749 /// Unlike a [`Float`] base, a [`Rational`] base may lie outside the [`Float`] exponent range or
7750 /// so close to 1 that no [`Float`] can represent its logarithm; both cases are handled exactly,
7751 /// by working with the base as an exact [`Rational`] throughout.
7752 ///
7753 /// Overflow and underflow:
7754 /// - If $f(x,y,p)\geq 2^{2^{30}-1}$, $\infty$ is returned instead.
7755 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
7756 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
7757 /// - Negative results (from negative $x$ and odd integer $y$) mirror the bullets above.
7758 ///
7759 /// If you want to use a rounding mode other than `Nearest`, consider using
7760 /// [`Float::rational_pow_prec_round`] instead.
7761 ///
7762 /// # Worst-case complexity
7763 /// $T(n) = O(n^{3/2} \log n \log\log n)$
7764 ///
7765 /// $M(n) = O(n \log n)$
7766 ///
7767 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(prec, x.significant_bits(),
7768 /// y.significant_bits())`.
7769 ///
7770 /// # Examples
7771 /// ```
7772 /// use malachite_float::Float;
7773 /// use malachite_q::Rational;
7774 /// use std::cmp::Ordering::*;
7775 ///
7776 /// let (p, o) =
7777 /// Float::rational_pow_prec(Rational::from_unsigneds(3u32, 2u32), Float::from(2.5), 5);
7778 /// assert_eq!(p.to_string(), "2.75");
7779 /// assert_eq!(o, Less);
7780 ///
7781 /// let (p, o) =
7782 /// Float::rational_pow_prec(Rational::from_unsigneds(3u32, 2u32), Float::from(2.5), 20);
7783 /// assert_eq!(p.to_string(), "2.7556763");
7784 /// assert_eq!(o, Greater);
7785 /// ```
7786 #[inline]
7787 pub fn rational_pow_prec(x: Rational, y: Self, prec: u64) -> (Self, Ordering) {
7788 Self::rational_pow_prec_ref_ref(&x, &y, prec)
7789 }
7790
7791 #[allow(clippy::needless_pass_by_value)]
7792 /// Raises a [`Rational`] to a [`Float`] power, returning the result as a [`Float`] rounded to
7793 /// the specified precision and to the nearest value. The [`Rational`] is taken by value and the
7794 /// [`Float`] by reference. An [`Ordering`] is also returned, indicating whether the rounded
7795 /// power is less than, equal to, or greater than the exact power. Although `NaN`s are not
7796 /// comparable to any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
7797 ///
7798 /// If the power is equidistant from two [`Float`]s with the specified precision, the [`Float`]
7799 /// with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of
7800 /// the `Nearest` rounding mode.
7801 ///
7802 /// $$
7803 /// f(x,y,p) = x^y+\varepsilon.
7804 /// $$
7805 /// - If $x^y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
7806 /// - If $x^y$ is finite and nonzero, then $|\varepsilon| \leq 2^{\lfloor\log_2
7807 /// |x^y|\rfloor-p}$.
7808 ///
7809 /// If the output has a precision, it is `prec`.
7810 ///
7811 /// Special cases:
7812 /// - $f(x,\pm0.0,p)=1.0$ for any $x$, even $0$
7813 /// - $f(1,y,p)=1.0$ for any $y$, even `NaN`
7814 /// - $f(x,\text{NaN},p)=\text{NaN}$ otherwise
7815 /// - $f(x,\infty,p)=\infty$ if $|x|>1$, and $0.0$ if $|x|<1$
7816 /// - $f(x,-\infty,p)=0.0$ if $|x|>1$, and $\infty$ if $|x|<1$
7817 /// - $f(\pm1,\pm\infty,p)=1.0$
7818 /// - $f(-1,y,p)=1.0$ if $y$ is an even integer, and $-1.0$ if $y$ is an odd integer
7819 /// - $f(0,y,p)=0.0$ if $y>0$, and $\infty$ if $y<0$; a [`Rational`] zero is unsigned, so the
7820 /// results take positive signs
7821 /// - $f(x,y,p)=\text{NaN}$ if $x<0$ and $y$ is finite and not an integer
7822 ///
7823 /// Unlike a [`Float`] base, a [`Rational`] base may lie outside the [`Float`] exponent range or
7824 /// so close to 1 that no [`Float`] can represent its logarithm; both cases are handled exactly,
7825 /// by working with the base as an exact [`Rational`] throughout.
7826 ///
7827 /// Overflow and underflow:
7828 /// - If $f(x,y,p)\geq 2^{2^{30}-1}$, $\infty$ is returned instead.
7829 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
7830 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
7831 /// - Negative results (from negative $x$ and odd integer $y$) mirror the bullets above.
7832 ///
7833 /// If you want to use a rounding mode other than `Nearest`, consider using
7834 /// [`Float::rational_pow_prec_round_val_ref`] instead.
7835 ///
7836 /// # Worst-case complexity
7837 /// $T(n) = O(n^{3/2} \log n \log\log n)$
7838 ///
7839 /// $M(n) = O(n \log n)$
7840 ///
7841 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(prec, x.significant_bits(),
7842 /// y.significant_bits())`.
7843 ///
7844 /// # Examples
7845 /// ```
7846 /// use malachite_float::Float;
7847 /// use malachite_q::Rational;
7848 /// use std::cmp::Ordering::*;
7849 ///
7850 /// let (p, o) = Float::rational_pow_prec_val_ref(
7851 /// Rational::from_unsigneds(3u32, 2u32),
7852 /// &Float::from(2.5),
7853 /// 5,
7854 /// );
7855 /// assert_eq!(p.to_string(), "2.75");
7856 /// assert_eq!(o, Less);
7857 ///
7858 /// let (p, o) = Float::rational_pow_prec_val_ref(
7859 /// Rational::from_unsigneds(3u32, 2u32),
7860 /// &Float::from(2.5),
7861 /// 20,
7862 /// );
7863 /// assert_eq!(p.to_string(), "2.7556763");
7864 /// assert_eq!(o, Greater);
7865 /// ```
7866 #[inline]
7867 pub fn rational_pow_prec_val_ref(x: Rational, y: &Self, prec: u64) -> (Self, Ordering) {
7868 Self::rational_pow_prec_ref_ref(&x, y, prec)
7869 }
7870
7871 #[allow(clippy::needless_pass_by_value)]
7872 /// Raises a [`Rational`] to a [`Float`] power, returning the result as a [`Float`] rounded to
7873 /// the specified precision and to the nearest value. The [`Rational`] is taken by reference and
7874 /// the [`Float`] by value. An [`Ordering`] is also returned, indicating whether the rounded
7875 /// power is less than, equal to, or greater than the exact power. Although `NaN`s are not
7876 /// comparable to any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
7877 ///
7878 /// If the power is equidistant from two [`Float`]s with the specified precision, the [`Float`]
7879 /// with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of
7880 /// the `Nearest` rounding mode.
7881 ///
7882 /// $$
7883 /// f(x,y,p) = x^y+\varepsilon.
7884 /// $$
7885 /// - If $x^y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
7886 /// - If $x^y$ is finite and nonzero, then $|\varepsilon| \leq 2^{\lfloor\log_2
7887 /// |x^y|\rfloor-p}$.
7888 ///
7889 /// If the output has a precision, it is `prec`.
7890 ///
7891 /// Special cases:
7892 /// - $f(x,\pm0.0,p)=1.0$ for any $x$, even $0$
7893 /// - $f(1,y,p)=1.0$ for any $y$, even `NaN`
7894 /// - $f(x,\text{NaN},p)=\text{NaN}$ otherwise
7895 /// - $f(x,\infty,p)=\infty$ if $|x|>1$, and $0.0$ if $|x|<1$
7896 /// - $f(x,-\infty,p)=0.0$ if $|x|>1$, and $\infty$ if $|x|<1$
7897 /// - $f(\pm1,\pm\infty,p)=1.0$
7898 /// - $f(-1,y,p)=1.0$ if $y$ is an even integer, and $-1.0$ if $y$ is an odd integer
7899 /// - $f(0,y,p)=0.0$ if $y>0$, and $\infty$ if $y<0$; a [`Rational`] zero is unsigned, so the
7900 /// results take positive signs
7901 /// - $f(x,y,p)=\text{NaN}$ if $x<0$ and $y$ is finite and not an integer
7902 ///
7903 /// Unlike a [`Float`] base, a [`Rational`] base may lie outside the [`Float`] exponent range or
7904 /// so close to 1 that no [`Float`] can represent its logarithm; both cases are handled exactly,
7905 /// by working with the base as an exact [`Rational`] throughout.
7906 ///
7907 /// Overflow and underflow:
7908 /// - If $f(x,y,p)\geq 2^{2^{30}-1}$, $\infty$ is returned instead.
7909 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
7910 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
7911 /// - Negative results (from negative $x$ and odd integer $y$) mirror the bullets above.
7912 ///
7913 /// If you want to use a rounding mode other than `Nearest`, consider using
7914 /// [`Float::rational_pow_prec_round_ref_val`] instead.
7915 ///
7916 /// # Worst-case complexity
7917 /// $T(n) = O(n^{3/2} \log n \log\log n)$
7918 ///
7919 /// $M(n) = O(n \log n)$
7920 ///
7921 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(prec, x.significant_bits(),
7922 /// y.significant_bits())`.
7923 ///
7924 /// # Examples
7925 /// ```
7926 /// use malachite_float::Float;
7927 /// use malachite_q::Rational;
7928 /// use std::cmp::Ordering::*;
7929 ///
7930 /// let (p, o) = Float::rational_pow_prec_ref_val(
7931 /// &Rational::from_unsigneds(3u32, 2u32),
7932 /// Float::from(2.5),
7933 /// 5,
7934 /// );
7935 /// assert_eq!(p.to_string(), "2.75");
7936 /// assert_eq!(o, Less);
7937 ///
7938 /// let (p, o) = Float::rational_pow_prec_ref_val(
7939 /// &Rational::from_unsigneds(3u32, 2u32),
7940 /// Float::from(2.5),
7941 /// 20,
7942 /// );
7943 /// assert_eq!(p.to_string(), "2.7556763");
7944 /// assert_eq!(o, Greater);
7945 /// ```
7946 #[inline]
7947 pub fn rational_pow_prec_ref_val(x: &Rational, y: Self, prec: u64) -> (Self, Ordering) {
7948 Self::rational_pow_prec_ref_ref(x, &y, prec)
7949 }
7950
7951 /// Raises a [`Rational`] to a [`Float`] power, returning the result as a [`Float`] rounded to
7952 /// the specified precision and to the nearest value. The [`Rational`] and the [`Float`] are
7953 /// both taken by reference. An [`Ordering`] is also returned, indicating whether the rounded
7954 /// power is less than, equal to, or greater than the exact power. Although `NaN`s are not
7955 /// comparable to any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
7956 ///
7957 /// If the power is equidistant from two [`Float`]s with the specified precision, the [`Float`]
7958 /// with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of
7959 /// the `Nearest` rounding mode.
7960 ///
7961 /// $$
7962 /// f(x,y,p) = x^y+\varepsilon.
7963 /// $$
7964 /// - If $x^y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
7965 /// - If $x^y$ is finite and nonzero, then $|\varepsilon| \leq 2^{\lfloor\log_2
7966 /// |x^y|\rfloor-p}$.
7967 ///
7968 /// If the output has a precision, it is `prec`.
7969 ///
7970 /// Special cases:
7971 /// - $f(x,\pm0.0,p)=1.0$ for any $x$, even $0$
7972 /// - $f(1,y,p)=1.0$ for any $y$, even `NaN`
7973 /// - $f(x,\text{NaN},p)=\text{NaN}$ otherwise
7974 /// - $f(x,\infty,p)=\infty$ if $|x|>1$, and $0.0$ if $|x|<1$
7975 /// - $f(x,-\infty,p)=0.0$ if $|x|>1$, and $\infty$ if $|x|<1$
7976 /// - $f(\pm1,\pm\infty,p)=1.0$
7977 /// - $f(-1,y,p)=1.0$ if $y$ is an even integer, and $-1.0$ if $y$ is an odd integer
7978 /// - $f(0,y,p)=0.0$ if $y>0$, and $\infty$ if $y<0$; a [`Rational`] zero is unsigned, so the
7979 /// results take positive signs
7980 /// - $f(x,y,p)=\text{NaN}$ if $x<0$ and $y$ is finite and not an integer
7981 ///
7982 /// Unlike a [`Float`] base, a [`Rational`] base may lie outside the [`Float`] exponent range or
7983 /// so close to 1 that no [`Float`] can represent its logarithm; both cases are handled exactly,
7984 /// by working with the base as an exact [`Rational`] throughout.
7985 ///
7986 /// Overflow and underflow:
7987 /// - If $f(x,y,p)\geq 2^{2^{30}-1}$, $\infty$ is returned instead.
7988 /// - If $0<f(x,y,p)\leq2^{-2^{30}-1}$, $0.0$ is returned instead.
7989 /// - If $2^{-2^{30}-1}<f(x,y,p)<2^{-2^{30}}$, $2^{-2^{30}}$ is returned instead.
7990 /// - Negative results (from negative $x$ and odd integer $y$) mirror the bullets above.
7991 ///
7992 /// If you want to use a rounding mode other than `Nearest`, consider using
7993 /// [`Float::rational_pow_prec_round_ref_ref`] instead.
7994 ///
7995 /// # Worst-case complexity
7996 /// $T(n) = O(n^{3/2} \log n \log\log n)$
7997 ///
7998 /// $M(n) = O(n \log n)$
7999 ///
8000 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(prec, x.significant_bits(),
8001 /// y.significant_bits())`.
8002 ///
8003 /// # Examples
8004 /// ```
8005 /// use malachite_float::Float;
8006 /// use malachite_q::Rational;
8007 /// use std::cmp::Ordering::*;
8008 ///
8009 /// let (p, o) = Float::rational_pow_prec_ref_ref(
8010 /// &Rational::from_unsigneds(3u32, 2u32),
8011 /// &Float::from(2.5),
8012 /// 5,
8013 /// );
8014 /// assert_eq!(p.to_string(), "2.75");
8015 /// assert_eq!(o, Less);
8016 ///
8017 /// let (p, o) = Float::rational_pow_prec_ref_ref(
8018 /// &Rational::from_unsigneds(3u32, 2u32),
8019 /// &Float::from(2.5),
8020 /// 20,
8021 /// );
8022 /// assert_eq!(p.to_string(), "2.7556763");
8023 /// assert_eq!(o, Greater);
8024 /// ```
8025 #[inline]
8026 pub fn rational_pow_prec_ref_ref(x: &Rational, y: &Self, prec: u64) -> (Self, Ordering) {
8027 Self::rational_pow_prec_round_ref_ref(x, y, prec, Nearest)
8028 }
8029}
8030
8031impl Float {
8032 /// Raises a [`Float`] to the power of a [`Rational`], rounding the result to the specified
8033 /// precision and with the specified rounding mode. Both the [`Float`] and the [`Rational`] are
8034 /// taken by value. An [`Ordering`] is also returned, indicating whether the rounded power is
8035 /// less than, equal to, or greater than the exact power. Although `NaN`s are not comparable to
8036 /// any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
8037 ///
8038 /// See [`RoundingMode`] for a description of the possible rounding modes.
8039 ///
8040 /// $$
8041 /// f(x,y,p,m) = x^y+\varepsilon.
8042 /// $$
8043 /// - If $x^y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
8044 /// - If $x^y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
8045 /// 2^{\lfloor\log_2 |x^y|\rfloor-p+1}$.
8046 /// - If $x^y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
8047 /// 2^{\lfloor\log_2 |x^y|\rfloor-p}$.
8048 ///
8049 /// If the output has a precision, it is `prec`.
8050 ///
8051 /// Special cases:
8052 /// - $f(x,0,p,m)=1.0$ for any $x$, even `NaN`
8053 /// - $f(\text{NaN},y,p,m)=\text{NaN}$ if $y \neq 0$
8054 /// - $f(x,y,p,m)=\text{NaN}$ if $x<0$ and $y$ is not an integer
8055 /// - $f(1.0,y,p,m)=1.0$
8056 /// - $f(-1.0,y,p,m)=1.0$ if $y$ is an even integer, and $-1.0$ if $y$ is an odd integer
8057 /// - $f(\infty,y,p,m)=\infty$ if $y>0$, and $0.0$ if $y<0$
8058 /// - $f(-\infty,y,p,m)=-\infty$ if $y$ is a positive odd integer, $\infty$ if $y$ is positive
8059 /// and not an odd integer, $-0.0$ if $y$ is a negative odd integer, and $0.0$ if $y$ is
8060 /// negative and not an odd integer
8061 /// - $f(0.0,y,p,m)=0.0$ if $y>0$, and $\infty$ if $y<0$
8062 /// - $f(-0.0,y,p,m)=-0.0$ if $y$ is a positive odd integer, $0.0$ if $y$ is positive and not an
8063 /// odd integer, $-\infty$ if $y$ is a negative odd integer, and $\infty$ if $y$ is negative
8064 /// and not an odd integer
8065 ///
8066 /// Unlike the exponent of a [`Float`], the exact [`Rational`] exponent selects a definite
8067 /// branch of the power, so results that are exactly representable (such as roots of perfect
8068 /// powers) are detected and rounded exactly.
8069 ///
8070 /// Overflow and underflow:
8071 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
8072 /// returned instead.
8073 /// - 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}$
8074 /// is returned instead.
8075 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
8076 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
8077 /// instead.
8078 /// - If $0<f(x,y,p,m)\leq2^{-2^{30}-1}$ and $m$ is `Nearest`, $0.0$ is returned instead.
8079 /// - If $2^{-2^{30}-1}<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
8080 /// instead.
8081 /// - Negative results (from negative $x$ and odd integer $y$) mirror the bullets above, with
8082 /// the rounding directions reflected.
8083 ///
8084 /// # Worst-case complexity
8085 /// $T(n) = O(n^{3/2} \log n \log\log n)$
8086 ///
8087 /// $M(n) = O(n \log n)$
8088 ///
8089 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(prec, self.significant_bits(),
8090 /// other.significant_bits())`.
8091 ///
8092 /// # Panics
8093 /// Panics if `prec` is zero, or if `rm` is `Exact` but the result cannot be represented exactly
8094 /// with the given precision.
8095 ///
8096 /// # Examples
8097 /// ```
8098 /// use malachite_base::num::basic::traits::Two;
8099 /// use malachite_base::rounding_modes::RoundingMode::*;
8100 /// use malachite_float::Float;
8101 /// use malachite_q::Rational;
8102 /// use std::cmp::Ordering::*;
8103 ///
8104 /// let (p, o) = Float::TWO.pow_rational_prec_round(Rational::from_signeds(3, 2), 20, Floor);
8105 /// assert_eq!(p.to_string(), "2.8284264");
8106 /// assert_eq!(o, Less);
8107 ///
8108 /// let (p, o) = Float::TWO.pow_rational_prec_round(Rational::from_signeds(3, 2), 20, Ceiling);
8109 /// assert_eq!(p.to_string(), "2.8284302");
8110 /// assert_eq!(o, Greater);
8111 ///
8112 /// let (p, o) =
8113 /// Float::from(8).pow_rational_prec_round(Rational::from_signeds(1, 3), 20, Floor);
8114 /// assert_eq!(p.to_string(), "2.0000000");
8115 /// assert_eq!(o, Equal);
8116 /// ```
8117 #[allow(clippy::needless_pass_by_value)]
8118 #[inline]
8119 pub fn pow_rational_prec_round(
8120 self,
8121 other: Rational,
8122 prec: u64,
8123 rm: RoundingMode,
8124 ) -> (Self, Ordering) {
8125 float_rational_pow(&self, &other, prec, rm)
8126 }
8127
8128 /// Raises a [`Float`] to the power of a [`Rational`], rounding the result to the specified
8129 /// precision and with the specified rounding mode. The [`Float`] is taken by value and the
8130 /// [`Rational`] by reference. An [`Ordering`] is also returned, indicating whether the rounded
8131 /// power is less than, equal to, or greater than the exact power. Although `NaN`s are not
8132 /// comparable to any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
8133 ///
8134 /// See [`RoundingMode`] for a description of the possible rounding modes.
8135 ///
8136 /// See the [`Float::pow_rational_prec_round`] documentation for information on special cases,
8137 /// overflow, and underflow.
8138 #[inline]
8139 pub fn pow_rational_prec_round_val_ref(
8140 self,
8141 other: &Rational,
8142 prec: u64,
8143 rm: RoundingMode,
8144 ) -> (Self, Ordering) {
8145 float_rational_pow(&self, other, prec, rm)
8146 }
8147
8148 /// Raises a [`Float`] to the power of a [`Rational`], rounding the result to the specified
8149 /// precision and with the specified rounding mode. The [`Float`] is taken by reference and the
8150 /// [`Rational`] by value. An [`Ordering`] is also returned, indicating whether the rounded
8151 /// power is less than, equal to, or greater than the exact power. Although `NaN`s are not
8152 /// comparable to any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
8153 ///
8154 /// See [`RoundingMode`] for a description of the possible rounding modes.
8155 ///
8156 /// See the [`Float::pow_rational_prec_round`] documentation for information on special cases,
8157 /// overflow, and underflow.
8158 #[allow(clippy::needless_pass_by_value)]
8159 #[inline]
8160 pub fn pow_rational_prec_round_ref_val(
8161 &self,
8162 other: Rational,
8163 prec: u64,
8164 rm: RoundingMode,
8165 ) -> (Self, Ordering) {
8166 float_rational_pow(self, &other, prec, rm)
8167 }
8168
8169 /// Raises a [`Float`] to the power of a [`Rational`], rounding the result to the specified
8170 /// precision and with the specified rounding mode. Both the [`Float`] and the [`Rational`] are
8171 /// taken by reference. An [`Ordering`] is also returned, indicating whether the rounded power
8172 /// is less than, equal to, or greater than the exact power. Although `NaN`s are not comparable
8173 /// to any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
8174 ///
8175 /// See [`RoundingMode`] for a description of the possible rounding modes.
8176 ///
8177 /// See the [`Float::pow_rational_prec_round`] documentation for information on special cases,
8178 /// overflow, and underflow.
8179 #[inline]
8180 pub fn pow_rational_prec_round_ref_ref(
8181 &self,
8182 other: &Rational,
8183 prec: u64,
8184 rm: RoundingMode,
8185 ) -> (Self, Ordering) {
8186 float_rational_pow(self, other, prec, rm)
8187 }
8188
8189 /// Raises a [`Float`] to the power of a [`Rational`], rounding the result to the specified
8190 /// precision and to the nearest value. Both the [`Float`] and the [`Rational`] are taken by
8191 /// value. An [`Ordering`] is also returned, indicating whether the rounded power is less than,
8192 /// equal to, or greater than the exact power. Although `NaN`s are not comparable to any
8193 /// [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
8194 ///
8195 /// If the power is equidistant from two [`Float`]s with the specified precision, the [`Float`]
8196 /// with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of
8197 /// the `Nearest` rounding mode.
8198 ///
8199 /// $$
8200 /// f(x,y,p,m) = x^y+\varepsilon.
8201 /// $$
8202 /// - If $x^y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
8203 /// - If $x^y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
8204 /// 2^{\lfloor\log_2 |x^y|\rfloor-p+1}$.
8205 /// - If $x^y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
8206 /// 2^{\lfloor\log_2 |x^y|\rfloor-p}$.
8207 ///
8208 /// If the output has a precision, it is `prec`.
8209 ///
8210 /// Special cases:
8211 /// - $f(x,0,p,m)=1.0$ for any $x$, even `NaN`
8212 /// - $f(\text{NaN},y,p,m)=\text{NaN}$ if $y \neq 0$
8213 /// - $f(x,y,p,m)=\text{NaN}$ if $x<0$ and $y$ is not an integer
8214 /// - $f(1.0,y,p,m)=1.0$
8215 /// - $f(-1.0,y,p,m)=1.0$ if $y$ is an even integer, and $-1.0$ if $y$ is an odd integer
8216 /// - $f(\infty,y,p,m)=\infty$ if $y>0$, and $0.0$ if $y<0$
8217 /// - $f(-\infty,y,p,m)=-\infty$ if $y$ is a positive odd integer, $\infty$ if $y$ is positive
8218 /// and not an odd integer, $-0.0$ if $y$ is a negative odd integer, and $0.0$ if $y$ is
8219 /// negative and not an odd integer
8220 /// - $f(0.0,y,p,m)=0.0$ if $y>0$, and $\infty$ if $y<0$
8221 /// - $f(-0.0,y,p,m)=-0.0$ if $y$ is a positive odd integer, $0.0$ if $y$ is positive and not an
8222 /// odd integer, $-\infty$ if $y$ is a negative odd integer, and $\infty$ if $y$ is negative
8223 /// and not an odd integer
8224 ///
8225 /// Unlike the exponent of a [`Float`], the exact [`Rational`] exponent selects a definite
8226 /// branch of the power, so results that are exactly representable (such as roots of perfect
8227 /// powers) are detected and rounded exactly.
8228 ///
8229 /// Overflow and underflow:
8230 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
8231 /// returned instead.
8232 /// - 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}$
8233 /// is returned instead.
8234 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
8235 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
8236 /// instead.
8237 /// - If $0<f(x,y,p,m)\leq2^{-2^{30}-1}$ and $m$ is `Nearest`, $0.0$ is returned instead.
8238 /// - If $2^{-2^{30}-1}<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
8239 /// instead.
8240 /// - Negative results (from negative $x$ and odd integer $y$) mirror the bullets above, with
8241 /// the rounding directions reflected.
8242 ///
8243 /// # Worst-case complexity
8244 /// $T(n) = O(n^{3/2} \log n \log\log n)$
8245 ///
8246 /// $M(n) = O(n \log n)$
8247 ///
8248 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(prec, self.significant_bits(),
8249 /// other.significant_bits())`.
8250 ///
8251 /// # Panics
8252 /// Panics if `prec` is zero.
8253 ///
8254 /// # Examples
8255 /// ```
8256 /// use malachite_base::num::basic::traits::Two;
8257 /// use malachite_float::Float;
8258 /// use malachite_q::Rational;
8259 /// use std::cmp::Ordering::*;
8260 ///
8261 /// let (p, o) = Float::TWO.pow_rational_prec(Rational::from_signeds(3, 2), 20);
8262 /// assert_eq!(p.to_string(), "2.8284264");
8263 /// assert_eq!(o, Less);
8264 ///
8265 /// let (p, o) = Float::from(27).pow_rational_prec(Rational::from_signeds(2, 3), 20);
8266 /// assert_eq!(p.to_string(), "9.0000000");
8267 /// assert_eq!(o, Equal);
8268 /// ```
8269 #[inline]
8270 pub fn pow_rational_prec(self, other: Rational, prec: u64) -> (Self, Ordering) {
8271 self.pow_rational_prec_round(other, prec, Nearest)
8272 }
8273
8274 /// Raises a [`Float`] to the power of a [`Rational`], rounding the result to the specified
8275 /// precision and to the nearest value. The [`Float`] is taken by value and the [`Rational`] by
8276 /// reference. An [`Ordering`] is also returned, indicating whether the rounded power is less
8277 /// than, equal to, or greater than the exact power. Although `NaN`s are not comparable to any
8278 /// [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
8279 ///
8280 /// If the power is equidistant from two [`Float`]s with the specified precision, the [`Float`]
8281 /// with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of
8282 /// the `Nearest` rounding mode.
8283 ///
8284 /// See the [`Float::pow_rational_prec_round`] documentation for information on special cases,
8285 /// overflow, and underflow.
8286 #[inline]
8287 pub fn pow_rational_prec_val_ref(self, other: &Rational, prec: u64) -> (Self, Ordering) {
8288 self.pow_rational_prec_round_val_ref(other, prec, Nearest)
8289 }
8290
8291 /// Raises a [`Float`] to the power of a [`Rational`], rounding the result to the specified
8292 /// precision and to the nearest value. The [`Float`] is taken by reference and the [`Rational`]
8293 /// by value. An [`Ordering`] is also returned, indicating whether the rounded power is less
8294 /// than, equal to, or greater than the exact power. Although `NaN`s are not comparable to any
8295 /// [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
8296 ///
8297 /// If the power is equidistant from two [`Float`]s with the specified precision, the [`Float`]
8298 /// with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of
8299 /// the `Nearest` rounding mode.
8300 ///
8301 /// See the [`Float::pow_rational_prec_round`] documentation for information on special cases,
8302 /// overflow, and underflow.
8303 #[inline]
8304 pub fn pow_rational_prec_ref_val(&self, other: Rational, prec: u64) -> (Self, Ordering) {
8305 self.pow_rational_prec_round_ref_val(other, prec, Nearest)
8306 }
8307
8308 /// Raises a [`Float`] to the power of a [`Rational`], rounding the result to the specified
8309 /// precision and to the nearest value. Both the [`Float`] and the [`Rational`] are taken by
8310 /// reference. An [`Ordering`] is also returned, indicating whether the rounded power is less
8311 /// than, equal to, or greater than the exact power. Although `NaN`s are not comparable to any
8312 /// [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
8313 ///
8314 /// If the power is equidistant from two [`Float`]s with the specified precision, the [`Float`]
8315 /// with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of
8316 /// the `Nearest` rounding mode.
8317 ///
8318 /// See the [`Float::pow_rational_prec_round`] documentation for information on special cases,
8319 /// overflow, and underflow.
8320 #[inline]
8321 pub fn pow_rational_prec_ref_ref(&self, other: &Rational, prec: u64) -> (Self, Ordering) {
8322 self.pow_rational_prec_round_ref_ref(other, prec, Nearest)
8323 }
8324
8325 /// Raises a [`Float`] to the power of a [`Rational`], rounding the result to the precision of
8326 /// the base and with the specified rounding mode. Both the [`Float`] and the [`Rational`] are
8327 /// taken by value. An [`Ordering`] is also returned, indicating whether the rounded power is
8328 /// less than, equal to, or greater than the exact power. Although `NaN`s are not comparable to
8329 /// any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
8330 ///
8331 /// The output precision is the precision of `self`. See [`RoundingMode`] for a description of
8332 /// the possible rounding modes.
8333 ///
8334 /// $$
8335 /// f(x,y,p,m) = x^y+\varepsilon.
8336 /// $$
8337 /// - If $x^y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
8338 /// - If $x^y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
8339 /// 2^{\lfloor\log_2 |x^y|\rfloor-p+1}$.
8340 /// - If $x^y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
8341 /// 2^{\lfloor\log_2 |x^y|\rfloor-p}$.
8342 ///
8343 /// If the output has a precision, it is `prec`.
8344 ///
8345 /// Special cases:
8346 /// - $f(x,0,p,m)=1.0$ for any $x$, even `NaN`
8347 /// - $f(\text{NaN},y,p,m)=\text{NaN}$ if $y \neq 0$
8348 /// - $f(x,y,p,m)=\text{NaN}$ if $x<0$ and $y$ is not an integer
8349 /// - $f(1.0,y,p,m)=1.0$
8350 /// - $f(-1.0,y,p,m)=1.0$ if $y$ is an even integer, and $-1.0$ if $y$ is an odd integer
8351 /// - $f(\infty,y,p,m)=\infty$ if $y>0$, and $0.0$ if $y<0$
8352 /// - $f(-\infty,y,p,m)=-\infty$ if $y$ is a positive odd integer, $\infty$ if $y$ is positive
8353 /// and not an odd integer, $-0.0$ if $y$ is a negative odd integer, and $0.0$ if $y$ is
8354 /// negative and not an odd integer
8355 /// - $f(0.0,y,p,m)=0.0$ if $y>0$, and $\infty$ if $y<0$
8356 /// - $f(-0.0,y,p,m)=-0.0$ if $y$ is a positive odd integer, $0.0$ if $y$ is positive and not an
8357 /// odd integer, $-\infty$ if $y$ is a negative odd integer, and $\infty$ if $y$ is negative
8358 /// and not an odd integer
8359 ///
8360 /// Unlike the exponent of a [`Float`], the exact [`Rational`] exponent selects a definite
8361 /// branch of the power, so results that are exactly representable (such as roots of perfect
8362 /// powers) are detected and rounded exactly.
8363 ///
8364 /// Overflow and underflow:
8365 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
8366 /// returned instead.
8367 /// - 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}$
8368 /// is returned instead.
8369 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
8370 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
8371 /// instead.
8372 /// - If $0<f(x,y,p,m)\leq2^{-2^{30}-1}$ and $m$ is `Nearest`, $0.0$ is returned instead.
8373 /// - If $2^{-2^{30}-1}<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
8374 /// instead.
8375 /// - Negative results (from negative $x$ and odd integer $y$) mirror the bullets above, with
8376 /// the rounding directions reflected.
8377 ///
8378 /// # Worst-case complexity
8379 /// $T(n) = O(n^{3/2} \log n \log\log n)$
8380 ///
8381 /// $M(n) = O(n \log n)$
8382 ///
8383 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(prec, self.significant_bits(),
8384 /// other.significant_bits())`.
8385 ///
8386 /// # Panics
8387 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the base's
8388 /// precision.
8389 ///
8390 /// # Examples
8391 /// ```
8392 /// use malachite_base::rounding_modes::RoundingMode::*;
8393 /// use malachite_float::Float;
8394 /// use malachite_q::Rational;
8395 /// use std::cmp::Ordering::*;
8396 ///
8397 /// // The output precision is the precision of the base, here 3 bits.
8398 /// let (p, o) = Float::from(5).pow_rational_round(Rational::from_signeds(3, 2), Floor);
8399 /// assert_eq!(p.to_string(), "10.0");
8400 /// assert_eq!(o, Less);
8401 ///
8402 /// let (p, o) = Float::from(5).pow_rational_round(Rational::from_signeds(3, 2), Ceiling);
8403 /// assert_eq!(p.to_string(), "12.0");
8404 /// assert_eq!(o, Greater);
8405 /// ```
8406 pub fn pow_rational_round(self, other: Rational, rm: RoundingMode) -> (Self, Ordering) {
8407 let prec = self.significant_bits();
8408 self.pow_rational_prec_round(other, prec, rm)
8409 }
8410
8411 /// Raises a [`Float`] to the power of a [`Rational`], rounding the result to the precision of
8412 /// the base and with the specified rounding mode. The [`Float`] is taken by value and the
8413 /// [`Rational`] by reference. An [`Ordering`] is also returned, indicating whether the rounded
8414 /// power is less than, equal to, or greater than the exact power. Although `NaN`s are not
8415 /// comparable to any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
8416 ///
8417 /// The output precision is the precision of `self`. See [`RoundingMode`] for a description of
8418 /// the possible rounding modes.
8419 ///
8420 /// See the [`Float::pow_rational_prec_round`] documentation for information on special cases,
8421 /// overflow, and underflow.
8422 pub fn pow_rational_round_val_ref(
8423 self,
8424 other: &Rational,
8425 rm: RoundingMode,
8426 ) -> (Self, Ordering) {
8427 let prec = self.significant_bits();
8428 self.pow_rational_prec_round_val_ref(other, prec, rm)
8429 }
8430
8431 /// Raises a [`Float`] to the power of a [`Rational`], rounding the result to the precision of
8432 /// the base and with the specified rounding mode. The [`Float`] is taken by reference and the
8433 /// [`Rational`] by value. An [`Ordering`] is also returned, indicating whether the rounded
8434 /// power is less than, equal to, or greater than the exact power. Although `NaN`s are not
8435 /// comparable to any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
8436 ///
8437 /// The output precision is the precision of `self`. See [`RoundingMode`] for a description of
8438 /// the possible rounding modes.
8439 ///
8440 /// See the [`Float::pow_rational_prec_round`] documentation for information on special cases,
8441 /// overflow, and underflow.
8442 pub fn pow_rational_round_ref_val(
8443 &self,
8444 other: Rational,
8445 rm: RoundingMode,
8446 ) -> (Self, Ordering) {
8447 let prec = self.significant_bits();
8448 self.pow_rational_prec_round_ref_val(other, prec, rm)
8449 }
8450
8451 /// Raises a [`Float`] to the power of a [`Rational`], rounding the result to the precision of
8452 /// the base and with the specified rounding mode. Both the [`Float`] and the [`Rational`] are
8453 /// taken by reference. An [`Ordering`] is also returned, indicating whether the rounded power
8454 /// is less than, equal to, or greater than the exact power. Although `NaN`s are not comparable
8455 /// to any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
8456 ///
8457 /// The output precision is the precision of `self`. See [`RoundingMode`] for a description of
8458 /// the possible rounding modes.
8459 ///
8460 /// See the [`Float::pow_rational_prec_round`] documentation for information on special cases,
8461 /// overflow, and underflow.
8462 pub fn pow_rational_round_ref_ref(
8463 &self,
8464 other: &Rational,
8465 rm: RoundingMode,
8466 ) -> (Self, Ordering) {
8467 let prec = self.significant_bits();
8468 self.pow_rational_prec_round_ref_ref(other, prec, rm)
8469 }
8470
8471 /// Raises a [`Float`] to the power of a [`Rational`] in place, taking the [`Rational`] by
8472 /// value.
8473 ///
8474 /// See the [`Float::pow_rational_prec_round`] documentation for information on special cases,
8475 /// overflow, and underflow.
8476 ///
8477 /// # Worst-case complexity
8478 /// $T(n) = O(n^{3/2} \log n \log\log n)$
8479 ///
8480 /// $M(n) = O(n \log n)$
8481 ///
8482 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(prec, self.significant_bits(),
8483 /// other.significant_bits())`.
8484 ///
8485 /// # Panics
8486 /// Panics if `prec` is zero, or if `rm` is `Exact` but the result cannot be represented exactly
8487 /// with the given precision.
8488 #[allow(clippy::needless_pass_by_value)]
8489 pub fn pow_rational_prec_round_assign(
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 /// reference.
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)$
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, or if `rm` is `Exact` but the result cannot be represented exactly
8516 /// with the given precision.
8517 pub fn pow_rational_prec_round_assign_ref(
8518 &mut self,
8519 other: &Rational,
8520 prec: u64,
8521 rm: RoundingMode,
8522 ) -> Ordering {
8523 let (result, o) = float_rational_pow(self, other, prec, rm);
8524 *self = result;
8525 o
8526 }
8527
8528 /// Raises a [`Float`] to the power of a [`Rational`] in place, taking the [`Rational`] by
8529 /// value.
8530 ///
8531 /// See the [`Float::pow_rational_prec_round`] documentation for information on special cases,
8532 /// overflow, and underflow.
8533 ///
8534 /// # Worst-case complexity
8535 /// $T(n) = O(n^{3/2} \log n \log\log n)$
8536 ///
8537 /// $M(n) = O(n \log n)$
8538 ///
8539 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(prec, self.significant_bits(),
8540 /// other.significant_bits())`.
8541 ///
8542 /// # Panics
8543 /// Panics if `prec` is zero.
8544 #[inline]
8545 pub fn pow_rational_prec_assign(&mut self, other: Rational, prec: u64) -> Ordering {
8546 self.pow_rational_prec_round_assign(other, prec, Nearest)
8547 }
8548
8549 /// Raises a [`Float`] to the power of a [`Rational`] in place, taking the [`Rational`] by
8550 /// reference.
8551 ///
8552 /// See the [`Float::pow_rational_prec_round`] documentation for information on special cases,
8553 /// overflow, and underflow.
8554 ///
8555 /// # Worst-case complexity
8556 /// $T(n) = O(n^{3/2} \log n \log\log n)$
8557 ///
8558 /// $M(n) = O(n \log n)$
8559 ///
8560 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(prec, self.significant_bits(),
8561 /// other.significant_bits())`.
8562 ///
8563 /// # Panics
8564 /// Panics if `prec` is zero.
8565 #[inline]
8566 pub fn pow_rational_prec_assign_ref(&mut self, other: &Rational, prec: u64) -> Ordering {
8567 self.pow_rational_prec_round_assign_ref(other, prec, Nearest)
8568 }
8569
8570 /// Raises a [`Float`] to the power of a [`Rational`] in place, taking the [`Rational`] by
8571 /// value.
8572 ///
8573 /// See the [`Float::pow_rational_prec_round`] documentation for information on special cases,
8574 /// overflow, and underflow.
8575 ///
8576 /// # Worst-case complexity
8577 /// $T(n) = O(n^{3/2} \log n \log\log n)$
8578 ///
8579 /// $M(n) = O(n \log n)$
8580 ///
8581 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(prec, self.significant_bits(),
8582 /// other.significant_bits())`.
8583 ///
8584 /// # Panics
8585 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the base's
8586 /// precision.
8587 pub fn pow_rational_round_assign(&mut self, other: Rational, rm: RoundingMode) -> Ordering {
8588 let prec = self.significant_bits();
8589 self.pow_rational_prec_round_assign(other, prec, rm)
8590 }
8591
8592 /// Raises a [`Float`] to the power of a [`Rational`] in place, taking the [`Rational`] by
8593 /// reference.
8594 ///
8595 /// See the [`Float::pow_rational_prec_round`] documentation for information on special cases,
8596 /// overflow, and underflow.
8597 ///
8598 /// # Worst-case complexity
8599 /// $T(n) = O(n^{3/2} \log n \log\log n)$
8600 ///
8601 /// $M(n) = O(n \log n)$
8602 ///
8603 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(prec, self.significant_bits(),
8604 /// other.significant_bits())`.
8605 ///
8606 /// # Panics
8607 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the base's
8608 /// precision.
8609 pub fn pow_rational_round_assign_ref(
8610 &mut self,
8611 other: &Rational,
8612 rm: RoundingMode,
8613 ) -> Ordering {
8614 let prec = self.significant_bits();
8615 self.pow_rational_prec_round_assign_ref(other, prec, rm)
8616 }
8617}
8618
8619impl Pow<Rational> for Float {
8620 type Output = Self;
8621
8622 /// Raises a [`Float`] to the power of a [`Rational`], rounding the result to the nearest value
8623 /// at the precision of the base. Both the [`Float`] and the [`Rational`] are taken by value.
8624 ///
8625 /// See the [`Float::pow_rational_prec_round`] documentation for information on special cases,
8626 /// overflow, and underflow.
8627 #[inline]
8628 fn pow(self, other: Rational) -> Self {
8629 let prec = self.significant_bits();
8630 self.pow_rational_prec_round(other, prec, Nearest).0
8631 }
8632}
8633
8634impl Pow<&Rational> for Float {
8635 type Output = Self;
8636
8637 /// Raises a [`Float`] to the power of a [`Rational`], rounding the result to the nearest value
8638 /// at the precision of the base. The [`Float`] is taken by value and the [`Rational`] by
8639 /// reference.
8640 ///
8641 /// See the [`Float::pow_rational_prec_round`] documentation for information on special cases,
8642 /// overflow, and underflow.
8643 #[inline]
8644 fn pow(self, other: &Rational) -> Self {
8645 let prec = self.significant_bits();
8646 self.pow_rational_prec_round_val_ref(other, prec, Nearest).0
8647 }
8648}
8649
8650impl Pow<Rational> for &Float {
8651 type Output = Float;
8652
8653 /// Raises a [`Float`] to the power of a [`Rational`], rounding the result to the nearest value
8654 /// at the precision of the base. The [`Float`] is taken by reference and the [`Rational`] by
8655 /// value.
8656 ///
8657 /// See the [`Float::pow_rational_prec_round`] documentation for information on special cases,
8658 /// overflow, and underflow.
8659 #[inline]
8660 fn pow(self, other: Rational) -> Float {
8661 let prec = self.significant_bits();
8662 self.pow_rational_prec_round_ref_val(other, prec, Nearest).0
8663 }
8664}
8665
8666impl Pow<&Rational> for &Float {
8667 type Output = Float;
8668
8669 /// Raises a [`Float`] to the power of a [`Rational`], rounding the result to the nearest value
8670 /// at the precision of the base. Both the [`Float`] and the [`Rational`] are taken by
8671 /// reference.
8672 ///
8673 /// See the [`Float::pow_rational_prec_round`] documentation for information on special cases,
8674 /// overflow, and underflow.
8675 #[inline]
8676 fn pow(self, other: &Rational) -> Float {
8677 let prec = self.significant_bits();
8678 self.pow_rational_prec_round_ref_ref(other, prec, Nearest).0
8679 }
8680}
8681
8682impl PowAssign<Rational> for Float {
8683 /// Raises a [`Float`] to the power of a [`Rational`] in place, taking the [`Rational`] by
8684 /// value, and rounding the result to the nearest value at the precision of the base.
8685 ///
8686 /// See the [`Float::pow_rational_prec_round`] documentation for information on special cases,
8687 /// overflow, and underflow.
8688 #[inline]
8689 fn pow_assign(&mut self, other: Rational) {
8690 let prec = self.significant_bits();
8691 self.pow_rational_prec_assign(other, prec);
8692 }
8693}
8694
8695impl PowAssign<&Rational> for Float {
8696 /// Raises a [`Float`] to the power of a [`Rational`] in place, taking the [`Rational`] by
8697 /// reference, and rounding the result to the nearest value at the precision of the base.
8698 ///
8699 /// See the [`Float::pow_rational_prec_round`] documentation for information on special cases,
8700 /// overflow, and underflow.
8701 #[inline]
8702 fn pow_assign(&mut self, other: &Rational) {
8703 let prec = self.significant_bits();
8704 self.pow_rational_prec_assign_ref(other, prec);
8705 }
8706}
8707
8708impl Float {
8709 /// Raises a [`Float`] to a [`Float`] power using the IEEE 754 `powr` function, rounding the
8710 /// result to the specified precision and with the specified rounding mode. Both [`Float`]s are
8711 /// taken by value. An [`Ordering`] is also returned, indicating whether the rounded power is
8712 /// less than, equal to, or greater than the exact power. Although `NaN`s are not comparable to
8713 /// any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
8714 ///
8715 /// See [`RoundingMode`] for a description of the possible rounding modes.
8716 ///
8717 /// $$
8718 /// f(x,y) = x^y+\varepsilon.
8719 /// $$
8720 /// - If $x^y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
8721 /// - If $x^y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
8722 /// 2^{\lfloor\log_2 |x^y|\rfloor-p+1}$.
8723 /// - If $x^y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
8724 /// 2^{\lfloor\log_2 |x^y|\rfloor-p}$.
8725 ///
8726 /// If the output has a precision, it is `prec`.
8727 ///
8728 /// `powr(x, y)` is $e^{y\ln x}$; unlike [`pow`](Float::pow_prec_round), its base is restricted
8729 /// to $x\geq 0$ and it never produces a negative result.
8730 ///
8731 /// Special cases:
8732 /// - $f(x,y)=\text{NaN}$ if $x$ is `NaN`, if $x<0$, if $x$ is $\pm0$ or $\infty$ and $y=0$, or
8733 /// if $x=1$ and $y$ is infinite
8734 /// - $f(x,0)=1.0$ if $x$ is finite and positive
8735 /// - $f(1.0,y)=1.0$ if $y$ is finite
8736 /// - $f(\infty,y)=\infty$ if $y>0$, and $0.0$ if $y<0$
8737 /// - $f(\pm0.0,y)=0.0$ if $y>0$, and $\infty$ if $y<0$
8738 /// - $f(x,\infty)=\infty$ if $x>1$, and $0.0$ if $0<x<1$
8739 /// - $f(x,-\infty)=0.0$ if $x>1$, and $\infty$ if $0<x<1$
8740 ///
8741 /// Overflow and underflow:
8742 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
8743 /// returned instead.
8744 /// - 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}$
8745 /// is returned instead.
8746 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
8747 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
8748 /// instead.
8749 /// - If $0<f(x,y,p,m)\leq2^{-2^{30}-1}$ and $m$ is `Nearest`, $0.0$ is returned instead.
8750 /// - If $2^{-2^{30}-1}<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
8751 /// instead.
8752 ///
8753 /// # Worst-case complexity
8754 /// $T(n, m) = O(n^{3/2} \log n \log\log n + m)$
8755 ///
8756 /// $M(n, m) = O(n \log n + m)$
8757 ///
8758 /// where $T$ is time, $M$ is additional memory, $n$ is `prec`, and $m$ is
8759 /// `max(self.significant_bits(), other.significant_bits())`.
8760 ///
8761 /// # Panics
8762 /// Panics if `prec` is zero, or if `rm` is `Exact` but the result cannot be represented exactly
8763 /// with the given precision.
8764 /// # Examples
8765 /// ```
8766 /// use malachite_base::rounding_modes::RoundingMode::*;
8767 /// use malachite_float::Float;
8768 /// use std::cmp::Ordering::*;
8769 ///
8770 /// let (p, o) = Float::from(3).powr_prec_round(Float::from(2.5), 20, Floor);
8771 /// assert_eq!(p.to_string(), "15.588455");
8772 /// assert_eq!(o, Less);
8773 ///
8774 /// let (p, o) = Float::from(3).powr_prec_round(Float::from(2.5), 20, Ceiling);
8775 /// assert_eq!(p.to_string(), "15.588470");
8776 /// assert_eq!(o, Greater);
8777 ///
8778 /// // A negative base gives NaN (unlike `pow`).
8779 /// let (p, o) = Float::from(-2).powr_prec_round(Float::from(3), 10, Nearest);
8780 /// assert_eq!(p.to_string(), "NaN");
8781 /// assert_eq!(o, Equal);
8782 /// ```
8783 #[allow(clippy::needless_pass_by_value)]
8784 #[inline]
8785 pub fn powr_prec_round(self, other: Self, prec: u64, rm: RoundingMode) -> (Self, Ordering) {
8786 self.powr_prec_round_ref_ref(&other, prec, rm)
8787 }
8788
8789 /// Raises a [`Float`] to a [`Float`] power using the IEEE 754 `powr` function, rounding the
8790 /// result to the specified precision and with the specified rounding mode. The first [`Float`]
8791 /// is taken by value and the second by reference. An [`Ordering`] is also returned, indicating
8792 /// whether the rounded power is less than, equal to, or greater than the exact power. Although
8793 /// `NaN`s are not comparable to any [`Float`], whenever this function returns a `NaN` it also
8794 /// returns `Equal`.
8795 ///
8796 /// See [`RoundingMode`] for a description of the possible rounding modes.
8797 ///
8798 /// See the [`Float::powr_prec_round`] documentation for information on special cases, overflow,
8799 /// and underflow.
8800 #[inline]
8801 pub fn powr_prec_round_val_ref(
8802 self,
8803 other: &Self,
8804 prec: u64,
8805 rm: RoundingMode,
8806 ) -> (Self, Ordering) {
8807 self.powr_prec_round_ref_ref(other, prec, rm)
8808 }
8809
8810 /// Raises a [`Float`] to a [`Float`] power using the IEEE 754 `powr` function, rounding the
8811 /// result to the specified precision and with the specified rounding mode. The first [`Float`]
8812 /// is taken by reference and the second by value. An [`Ordering`] is also returned, indicating
8813 /// whether the rounded power is less than, equal to, or greater than the exact power. Although
8814 /// `NaN`s are not comparable to any [`Float`], whenever this function returns a `NaN` it also
8815 /// returns `Equal`.
8816 ///
8817 /// See [`RoundingMode`] for a description of the possible rounding modes.
8818 ///
8819 /// See the [`Float::powr_prec_round`] documentation for information on special cases, overflow,
8820 /// and underflow.
8821 #[allow(clippy::needless_pass_by_value)]
8822 #[inline]
8823 pub fn powr_prec_round_ref_val(
8824 &self,
8825 other: Self,
8826 prec: u64,
8827 rm: RoundingMode,
8828 ) -> (Self, Ordering) {
8829 self.powr_prec_round_ref_ref(&other, prec, rm)
8830 }
8831
8832 /// Raises a [`Float`] to a [`Float`] power using the IEEE 754 `powr` function, rounding the
8833 /// result to the specified precision and with the specified rounding mode. Both [`Float`]s are
8834 /// taken by reference. An [`Ordering`] is also returned, indicating whether the rounded power
8835 /// is less than, equal to, or greater than the exact power. Although `NaN`s are not comparable
8836 /// to any [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
8837 ///
8838 /// See [`RoundingMode`] for a description of the possible rounding modes.
8839 ///
8840 /// $$
8841 /// f(x,y) = x^y+\varepsilon.
8842 /// $$
8843 /// - If $x^y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
8844 /// - If $x^y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
8845 /// 2^{\lfloor\log_2 |x^y|\rfloor-p+1}$.
8846 /// - If $x^y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
8847 /// 2^{\lfloor\log_2 |x^y|\rfloor-p}$.
8848 ///
8849 /// If the output has a precision, it is `prec`.
8850 ///
8851 /// `powr(x, y)` is $e^{y\ln x}$; unlike [`pow`](Float::pow_prec_round), its base is restricted
8852 /// to $x\geq 0$ and it never produces a negative result.
8853 ///
8854 /// Special cases:
8855 /// - $f(x,y)=\text{NaN}$ if $x$ is `NaN`, if $x<0$, if $x$ is $\pm0$ or $\infty$ and $y=0$, or
8856 /// if $x=1$ and $y$ is infinite
8857 /// - $f(x,0)=1.0$ if $x$ is finite and positive
8858 /// - $f(1.0,y)=1.0$ if $y$ is finite
8859 /// - $f(\infty,y)=\infty$ if $y>0$, and $0.0$ if $y<0$
8860 /// - $f(\pm0.0,y)=0.0$ if $y>0$, and $\infty$ if $y<0$
8861 /// - $f(x,\infty)=\infty$ if $x>1$, and $0.0$ if $0<x<1$
8862 /// - $f(x,-\infty)=0.0$ if $x>1$, and $\infty$ if $0<x<1$
8863 ///
8864 /// Overflow and underflow:
8865 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
8866 /// returned instead.
8867 /// - 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}$
8868 /// is returned instead.
8869 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
8870 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
8871 /// instead.
8872 /// - If $0<f(x,y,p,m)\leq2^{-2^{30}-1}$ and $m$ is `Nearest`, $0.0$ is returned instead.
8873 /// - If $2^{-2^{30}-1}<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
8874 /// instead.
8875 ///
8876 /// # Worst-case complexity
8877 /// $T(n, m) = O(n^{3/2} \log n \log\log n + m)$
8878 ///
8879 /// $M(n, m) = O(n \log n + m)$
8880 ///
8881 /// where $T$ is time, $M$ is additional memory, $n$ is `prec`, and $m$ is
8882 /// `max(self.significant_bits(), other.significant_bits())`.
8883 ///
8884 /// # Panics
8885 /// Panics if `prec` is zero, or if `rm` is `Exact` but the result cannot be represented exactly
8886 /// with the given precision.
8887 /// # Examples
8888 /// ```
8889 /// use malachite_base::rounding_modes::RoundingMode::*;
8890 /// use malachite_float::Float;
8891 /// use std::cmp::Ordering::*;
8892 ///
8893 /// let (p, o) = Float::from(3).powr_prec_round(Float::from(2.5), 20, Floor);
8894 /// assert_eq!(p.to_string(), "15.588455");
8895 /// assert_eq!(o, Less);
8896 ///
8897 /// let (p, o) = Float::from(3).powr_prec_round(Float::from(2.5), 20, Ceiling);
8898 /// assert_eq!(p.to_string(), "15.588470");
8899 /// assert_eq!(o, Greater);
8900 ///
8901 /// // A negative base gives NaN (unlike `pow`).
8902 /// let (p, o) = Float::from(-2).powr_prec_round(Float::from(3), 10, Nearest);
8903 /// assert_eq!(p.to_string(), "NaN");
8904 /// assert_eq!(o, Equal);
8905 /// ```
8906 pub fn powr_prec_round_ref_ref(
8907 &self,
8908 other: &Self,
8909 prec: u64,
8910 rm: RoundingMode,
8911 ) -> (Self, Ordering) {
8912 assert_ne!(prec, 0);
8913 let x = self;
8914 let y = other;
8915 // powr(x, y) = exp(y * ln(x)). This is `mpfr_powr` from `powr.c`, MPFR 4.3.0.
8916 match (x, y) {
8917 // A NaN or negative base (finite negative or -Inf) is NaN (pow allows a negative base
8918 // with an integer exponent); and a singular +0, -0, or +Inf base with a zero exponent
8919 // is NaN (pow gives 1).
8920 (Self(NaN | Finite { sign: false, .. } | Infinity { sign: false }), _)
8921 | (Self(Zero { .. } | Infinity { sign: true }), float_either_zero!()) => {
8922 (Self::NAN, Equal)
8923 }
8924 // powr treats -0 like +0: a finite nonzero exponent gives +0 (y > 0) or +Inf (y < 0),
8925 // always positive (pow gives a signed result for odd-integer y).
8926 (float_negative_zero!(), Self(Finite { sign, .. })) => {
8927 if *sign {
8928 (Self::ZERO, Equal)
8929 } else {
8930 (Self::INFINITY, Equal)
8931 }
8932 }
8933 // A base of exactly 1 with an infinite exponent is NaN (pow gives 1).
8934 (_, float_either_infinity!()) if *x == 1u32 => (Self::NAN, Equal),
8935 // Everything else defers to pow.
8936 _ => self.pow_prec_round_ref_ref(y, prec, rm),
8937 }
8938 }
8939
8940 /// Raises a [`Float`] to a [`Float`] power using the IEEE 754 `powr` function, rounding the
8941 /// result to the specified precision and to the nearest value. Both [`Float`]s are taken by
8942 /// value. An [`Ordering`] is also returned, indicating whether the rounded power is less than,
8943 /// equal to, or greater than the exact power. Although `NaN`s are not comparable to any
8944 /// [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
8945 ///
8946 /// If the power is equidistant from two [`Float`]s with the specified precision, the [`Float`]
8947 /// with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of
8948 /// the `Nearest` rounding mode.
8949 ///
8950 /// $$
8951 /// f(x,y) = x^y+\varepsilon.
8952 /// $$
8953 /// - If $x^y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
8954 /// - If $x^y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
8955 /// 2^{\lfloor\log_2 |x^y|\rfloor-p+1}$.
8956 /// - If $x^y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
8957 /// 2^{\lfloor\log_2 |x^y|\rfloor-p}$.
8958 ///
8959 /// If the output has a precision, it is `prec`.
8960 ///
8961 /// `powr(x, y)` is $e^{y\ln x}$; unlike [`pow`](Float::pow_prec_round), its base is restricted
8962 /// to $x\geq 0$ and it never produces a negative result.
8963 ///
8964 /// Special cases:
8965 /// - $f(x,y)=\text{NaN}$ if $x$ is `NaN`, if $x<0$, if $x$ is $\pm0$ or $\infty$ and $y=0$, or
8966 /// if $x=1$ and $y$ is infinite
8967 /// - $f(x,0)=1.0$ if $x$ is finite and positive
8968 /// - $f(1.0,y)=1.0$ if $y$ is finite
8969 /// - $f(\infty,y)=\infty$ if $y>0$, and $0.0$ if $y<0$
8970 /// - $f(\pm0.0,y)=0.0$ if $y>0$, and $\infty$ if $y<0$
8971 /// - $f(x,\infty)=\infty$ if $x>1$, and $0.0$ if $0<x<1$
8972 /// - $f(x,-\infty)=0.0$ if $x>1$, and $\infty$ if $0<x<1$
8973 ///
8974 /// Overflow and underflow:
8975 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
8976 /// returned instead.
8977 /// - 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}$
8978 /// is returned instead.
8979 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
8980 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
8981 /// instead.
8982 /// - If $0<f(x,y,p,m)\leq2^{-2^{30}-1}$ and $m$ is `Nearest`, $0.0$ is returned instead.
8983 /// - If $2^{-2^{30}-1}<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
8984 /// instead.
8985 ///
8986 /// # Worst-case complexity
8987 /// $T(n, m) = O(n^{3/2} \log n \log\log n + m)$
8988 ///
8989 /// $M(n, m) = O(n \log n + m)$
8990 ///
8991 /// where $T$ is time, $M$ is additional memory, $n$ is `prec`, and $m$ is
8992 /// `max(self.significant_bits(), other.significant_bits())`.
8993 ///
8994 /// # Panics
8995 /// Panics if `prec` is zero.
8996 /// # Examples
8997 /// ```
8998 /// use malachite_float::Float;
8999 /// use std::cmp::Ordering::*;
9000 ///
9001 /// let (p, o) = Float::from(9).powr_prec(Float::from(0.5), 10);
9002 /// assert_eq!(p.to_string(), "3.0000");
9003 /// assert_eq!(o, Equal);
9004 /// ```
9005 #[allow(clippy::needless_pass_by_value)]
9006 #[inline]
9007 pub fn powr_prec(self, other: Self, prec: u64) -> (Self, Ordering) {
9008 self.powr_prec_round_ref_ref(&other, prec, Nearest)
9009 }
9010
9011 /// Raises a [`Float`] to a [`Float`] power using the IEEE 754 `powr` function, rounding the
9012 /// result to the specified precision and to the nearest value. The first [`Float`] is taken by
9013 /// value and the second by reference. An [`Ordering`] is also returned, indicating whether the
9014 /// rounded power is less than, equal to, or greater than the exact power. Although `NaN`s are
9015 /// not comparable to any [`Float`], whenever this function returns a `NaN` it also returns
9016 /// `Equal`.
9017 ///
9018 /// If the power is equidistant from two [`Float`]s with the specified precision, the [`Float`]
9019 /// with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of
9020 /// the `Nearest` rounding mode.
9021 ///
9022 /// See the [`Float::powr_prec_round`] documentation for information on special cases, overflow,
9023 /// and underflow.
9024 #[inline]
9025 pub fn powr_prec_val_ref(self, other: &Self, prec: u64) -> (Self, Ordering) {
9026 self.powr_prec_round_ref_ref(other, prec, Nearest)
9027 }
9028
9029 /// Raises a [`Float`] to a [`Float`] power using the IEEE 754 `powr` function, rounding the
9030 /// result to the specified precision and to the nearest value. The first [`Float`] is taken by
9031 /// reference and the second by value. An [`Ordering`] is also returned, indicating whether the
9032 /// rounded power is less than, equal to, or greater than the exact power. Although `NaN`s are
9033 /// not comparable to any [`Float`], whenever this function returns a `NaN` it also returns
9034 /// `Equal`.
9035 ///
9036 /// If the power is equidistant from two [`Float`]s with the specified precision, the [`Float`]
9037 /// with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of
9038 /// the `Nearest` rounding mode.
9039 ///
9040 /// See the [`Float::powr_prec_round`] documentation for information on special cases, overflow,
9041 /// and underflow.
9042 #[allow(clippy::needless_pass_by_value)]
9043 #[inline]
9044 pub fn powr_prec_ref_val(&self, other: Self, prec: u64) -> (Self, Ordering) {
9045 self.powr_prec_round_ref_ref(&other, prec, Nearest)
9046 }
9047
9048 /// Raises a [`Float`] to a [`Float`] power using the IEEE 754 `powr` function, rounding the
9049 /// result to the specified precision and to the nearest value. Both [`Float`]s are taken by
9050 /// reference. An [`Ordering`] is also returned, indicating whether the rounded power is less
9051 /// than, equal to, or greater than the exact power. Although `NaN`s are not comparable to any
9052 /// [`Float`], whenever this function returns a `NaN` it also returns `Equal`.
9053 ///
9054 /// If the power is equidistant from two [`Float`]s with the specified precision, the [`Float`]
9055 /// with fewer 1s in its binary expansion is chosen. See [`RoundingMode`] for a description of
9056 /// the `Nearest` rounding mode.
9057 ///
9058 /// See the [`Float::powr_prec_round`] documentation for information on special cases, overflow,
9059 /// and underflow.
9060 #[inline]
9061 pub fn powr_prec_ref_ref(&self, other: &Self, prec: u64) -> (Self, Ordering) {
9062 self.powr_prec_round_ref_ref(other, prec, Nearest)
9063 }
9064
9065 /// Raises a [`Float`] to a [`Float`] power using the IEEE 754 `powr` function, rounding the
9066 /// result to the maximum of the precisions of the inputs and with the specified rounding mode.
9067 /// Both [`Float`]s are taken by value. An [`Ordering`] is also returned, indicating whether the
9068 /// rounded power is less than, equal to, or greater than the exact power. Although `NaN`s are
9069 /// not comparable to any [`Float`], whenever this function returns a `NaN` it also returns
9070 /// `Equal`.
9071 ///
9072 /// See [`RoundingMode`] for a description of the possible rounding modes.
9073 ///
9074 /// $$
9075 /// f(x,y) = x^y+\varepsilon.
9076 /// $$
9077 /// - If $x^y$ is infinite, zero, or `NaN`, $\varepsilon$ may be ignored or assumed to be 0.
9078 /// - If $x^y$ is finite and nonzero, and $m$ is not `Nearest`, then $|\varepsilon| <
9079 /// 2^{\lfloor\log_2 |x^y|\rfloor-p+1}$.
9080 /// - If $x^y$ is finite and nonzero, and $m$ is `Nearest`, then $|\varepsilon| \leq
9081 /// 2^{\lfloor\log_2 |x^y|\rfloor-p}$.
9082 ///
9083 /// If the output has a precision, it is `prec`.
9084 ///
9085 /// `powr(x, y)` is $e^{y\ln x}$; unlike [`pow`](Float::pow_prec_round), its base is restricted
9086 /// to $x\geq 0$ and it never produces a negative result.
9087 ///
9088 /// Special cases:
9089 /// - $f(x,y)=\text{NaN}$ if $x$ is `NaN`, if $x<0$, if $x$ is $\pm0$ or $\infty$ and $y=0$, or
9090 /// if $x=1$ and $y$ is infinite
9091 /// - $f(x,0)=1.0$ if $x$ is finite and positive
9092 /// - $f(1.0,y)=1.0$ if $y$ is finite
9093 /// - $f(\infty,y)=\infty$ if $y>0$, and $0.0$ if $y<0$
9094 /// - $f(\pm0.0,y)=0.0$ if $y>0$, and $\infty$ if $y<0$
9095 /// - $f(x,\infty)=\infty$ if $x>1$, and $0.0$ if $0<x<1$
9096 /// - $f(x,-\infty)=0.0$ if $x>1$, and $\infty$ if $0<x<1$
9097 ///
9098 /// Overflow and underflow:
9099 /// - If $f(x,y,p,m)\geq 2^{2^{30}-1}$ and $m$ is `Ceiling`, `Up`, or `Nearest`, $\infty$ is
9100 /// returned instead.
9101 /// - 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}$
9102 /// is returned instead.
9103 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Floor` or `Down`, $0.0$ is returned instead.
9104 /// - If $0<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Ceiling` or `Up`, $2^{-2^{30}}$ is returned
9105 /// instead.
9106 /// - If $0<f(x,y,p,m)\leq2^{-2^{30}-1}$ and $m$ is `Nearest`, $0.0$ is returned instead.
9107 /// - If $2^{-2^{30}-1}<f(x,y,p,m)<2^{-2^{30}}$ and $m$ is `Nearest`, $2^{-2^{30}}$ is returned
9108 /// instead.
9109 ///
9110 /// # Worst-case complexity
9111 /// $T(n) = O(n^{3/2} \log n \log\log n)$
9112 ///
9113 /// $M(n) = O(n \log n)$
9114 ///
9115 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
9116 /// other.significant_bits())`.
9117 ///
9118 /// # Panics
9119 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the output
9120 /// precision.
9121 /// # Examples
9122 /// ```
9123 /// use malachite_base::rounding_modes::RoundingMode::*;
9124 /// use malachite_float::Float;
9125 /// use std::cmp::Ordering::*;
9126 ///
9127 /// let (p, o) = Float::from(3).powr_round(Float::from(2.5), Floor);
9128 /// assert_eq!(p.to_string(), "14.0");
9129 /// assert_eq!(o, Less);
9130 /// ```
9131 #[allow(clippy::needless_pass_by_value)]
9132 pub fn powr_round(self, other: Self, rm: RoundingMode) -> (Self, Ordering) {
9133 let prec = self.significant_bits().max(other.significant_bits());
9134 self.powr_prec_round_ref_ref(&other, prec, rm)
9135 }
9136
9137 /// Raises a [`Float`] to a [`Float`] power using the IEEE 754 `powr` function, rounding the
9138 /// result to the maximum of the precisions of the inputs and with the specified rounding mode.
9139 /// The first [`Float`] is taken by value and the second by reference. An [`Ordering`] is also
9140 /// returned, indicating whether the rounded power is less than, equal to, or greater than the
9141 /// exact power. Although `NaN`s are not comparable to any [`Float`], whenever this function
9142 /// returns a `NaN` it also returns `Equal`.
9143 ///
9144 /// See [`RoundingMode`] for a description of the possible rounding modes.
9145 ///
9146 /// See the [`Float::powr_prec_round`] documentation for information on special cases, overflow,
9147 /// and underflow.
9148 pub fn powr_round_val_ref(self, other: &Self, rm: RoundingMode) -> (Self, Ordering) {
9149 let prec = self.significant_bits().max(other.significant_bits());
9150 self.powr_prec_round_ref_ref(other, prec, rm)
9151 }
9152
9153 /// Raises a [`Float`] to a [`Float`] power using the IEEE 754 `powr` function, rounding the
9154 /// result to the maximum of the precisions of the inputs and with the specified rounding mode.
9155 /// The first [`Float`] is taken by reference and the second by value. An [`Ordering`] is also
9156 /// returned, indicating whether the rounded power is less than, equal to, or greater than the
9157 /// exact power. Although `NaN`s are not comparable to any [`Float`], whenever this function
9158 /// returns a `NaN` it also returns `Equal`.
9159 ///
9160 /// See [`RoundingMode`] for a description of the possible rounding modes.
9161 ///
9162 /// See the [`Float::powr_prec_round`] documentation for information on special cases, overflow,
9163 /// and underflow.
9164 #[allow(clippy::needless_pass_by_value)]
9165 pub fn powr_round_ref_val(&self, other: Self, rm: RoundingMode) -> (Self, Ordering) {
9166 let prec = self.significant_bits().max(other.significant_bits());
9167 self.powr_prec_round_ref_ref(&other, prec, rm)
9168 }
9169
9170 /// Raises a [`Float`] to a [`Float`] power using the IEEE 754 `powr` function, rounding the
9171 /// result to the maximum of the precisions of the inputs and with the specified rounding mode.
9172 /// Both [`Float`]s are taken by reference. An [`Ordering`] is also returned, indicating whether
9173 /// the rounded power is less than, equal to, or greater than the exact power. Although `NaN`s
9174 /// are not comparable to any [`Float`], whenever this function returns a `NaN` it also returns
9175 /// `Equal`.
9176 ///
9177 /// See [`RoundingMode`] for a description of the possible rounding modes.
9178 ///
9179 /// See the [`Float::powr_prec_round`] documentation for information on special cases, overflow,
9180 /// and underflow.
9181 pub fn powr_round_ref_ref(&self, other: &Self, rm: RoundingMode) -> (Self, Ordering) {
9182 let prec = self.significant_bits().max(other.significant_bits());
9183 self.powr_prec_round_ref_ref(other, prec, rm)
9184 }
9185
9186 /// Raises a [`Float`] to a [`Float`] power in place using the IEEE 754 `powr` function, taking
9187 /// the exponent by value.
9188 ///
9189 /// See the [`Float::powr_prec_round`] documentation for information on special cases, overflow,
9190 /// and underflow.
9191 ///
9192 /// # Worst-case complexity
9193 /// $T(n, m) = O(n^{3/2} \log n \log\log n + m)$
9194 ///
9195 /// $M(n, m) = O(n \log n + m)$
9196 ///
9197 /// where $T$ is time, $M$ is additional memory, $n$ is `prec`, and $m$ is
9198 /// `max(self.significant_bits(), other.significant_bits())`.
9199 ///
9200 /// # Panics
9201 /// Panics if `prec` is zero, or if `rm` is `Exact` but the result cannot be represented exactly
9202 /// with the given precision.
9203 #[allow(clippy::needless_pass_by_value)]
9204 pub fn powr_prec_round_assign(&mut self, other: Self, prec: u64, rm: RoundingMode) -> Ordering {
9205 let (result, o) = self.powr_prec_round_ref_ref(&other, prec, rm);
9206 *self = result;
9207 o
9208 }
9209
9210 /// Raises a [`Float`] to a [`Float`] power in place using the IEEE 754 `powr` function, taking
9211 /// the exponent by reference.
9212 ///
9213 /// See the [`Float::powr_prec_round`] documentation for information on special cases, overflow,
9214 /// and underflow.
9215 ///
9216 /// # Worst-case complexity
9217 /// $T(n, m) = O(n^{3/2} \log n \log\log n + m)$
9218 ///
9219 /// $M(n, m) = O(n \log n + m)$
9220 ///
9221 /// where $T$ is time, $M$ is additional memory, $n$ is `prec`, and $m$ is
9222 /// `max(self.significant_bits(), other.significant_bits())`.
9223 ///
9224 /// # Panics
9225 /// Panics if `prec` is zero, or if `rm` is `Exact` but the result cannot be represented exactly
9226 /// with the given precision.
9227 pub fn powr_prec_round_assign_ref(
9228 &mut self,
9229 other: &Self,
9230 prec: u64,
9231 rm: RoundingMode,
9232 ) -> Ordering {
9233 let (result, o) = self.powr_prec_round_ref_ref(other, prec, rm);
9234 *self = result;
9235 o
9236 }
9237
9238 /// Raises a [`Float`] to a [`Float`] power in place using the IEEE 754 `powr` function, taking
9239 /// the exponent by value.
9240 ///
9241 /// See the [`Float::powr_prec_round`] documentation for information on special cases, overflow,
9242 /// and underflow.
9243 ///
9244 /// # Worst-case complexity
9245 /// $T(n, m) = O(n^{3/2} \log n \log\log n + m)$
9246 ///
9247 /// $M(n, m) = O(n \log n + m)$
9248 ///
9249 /// where $T$ is time, $M$ is additional memory, $n$ is `prec`, and $m$ is
9250 /// `max(self.significant_bits(), other.significant_bits())`.
9251 ///
9252 /// # Panics
9253 /// Panics if `prec` is zero.
9254 #[allow(clippy::needless_pass_by_value)]
9255 #[inline]
9256 pub fn powr_prec_assign(&mut self, other: Self, prec: u64) -> Ordering {
9257 self.powr_prec_round_assign(other, prec, Nearest)
9258 }
9259
9260 /// Raises a [`Float`] to a [`Float`] power in place using the IEEE 754 `powr` function, taking
9261 /// the exponent by reference.
9262 ///
9263 /// See the [`Float::powr_prec_round`] documentation for information on special cases, overflow,
9264 /// and underflow.
9265 ///
9266 /// # Worst-case complexity
9267 /// $T(n, m) = O(n^{3/2} \log n \log\log n + m)$
9268 ///
9269 /// $M(n, m) = O(n \log n + m)$
9270 ///
9271 /// where $T$ is time, $M$ is additional memory, $n$ is `prec`, and $m$ is
9272 /// `max(self.significant_bits(), other.significant_bits())`.
9273 ///
9274 /// # Panics
9275 /// Panics if `prec` is zero.
9276 #[inline]
9277 pub fn powr_prec_assign_ref(&mut self, other: &Self, prec: u64) -> Ordering {
9278 self.powr_prec_round_assign_ref(other, prec, Nearest)
9279 }
9280
9281 /// Raises a [`Float`] to a [`Float`] power in place using the IEEE 754 `powr` function, taking
9282 /// the exponent by value.
9283 ///
9284 /// See the [`Float::powr_prec_round`] documentation for information on special cases, overflow,
9285 /// and underflow.
9286 ///
9287 /// # Worst-case complexity
9288 /// $T(n) = O(n^{3/2} \log n \log\log n)$
9289 ///
9290 /// $M(n) = O(n \log n)$
9291 ///
9292 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
9293 /// other.significant_bits())`.
9294 ///
9295 /// # Panics
9296 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the output
9297 /// precision.
9298 #[allow(clippy::needless_pass_by_value)]
9299 pub fn powr_round_assign(&mut self, other: Self, rm: RoundingMode) -> Ordering {
9300 let prec = self.significant_bits().max(other.significant_bits());
9301 self.powr_prec_round_assign(other, prec, rm)
9302 }
9303
9304 /// Raises a [`Float`] to a [`Float`] power in place using the IEEE 754 `powr` function, taking
9305 /// the exponent by reference.
9306 ///
9307 /// See the [`Float::powr_prec_round`] documentation for information on special cases, overflow,
9308 /// and underflow.
9309 ///
9310 /// # Worst-case complexity
9311 /// $T(n) = O(n^{3/2} \log n \log\log n)$
9312 ///
9313 /// $M(n) = O(n \log n)$
9314 ///
9315 /// where $T$ is time, $M$ is additional memory, and $n$ is `max(self.significant_bits(),
9316 /// other.significant_bits())`.
9317 ///
9318 /// # Panics
9319 /// Panics if `rm` is `Exact` but the result cannot be represented exactly with the output
9320 /// precision.
9321 pub fn powr_round_assign_ref(&mut self, other: &Self, rm: RoundingMode) -> Ordering {
9322 let prec = self.significant_bits().max(other.significant_bits());
9323 self.powr_prec_round_assign_ref(other, prec, rm)
9324 }
9325}