Skip to main content

malachite_base/num/arithmetic/
traits.rs

1// Copyright © 2026 Mikhail Hogrefe
2//
3// This file is part of Malachite.
4//
5// Malachite is free software: you can redistribute it and/or modify it under the terms of the GNU
6// Lesser General Public License (LGPL) as published by the Free Software Foundation; either version
7// 3 of the License, or (at your option) any later version. See <https://www.gnu.org/licenses/>.
8
9use crate::num::basic::traits::Two;
10use crate::rounding_modes::RoundingMode;
11use core::cmp::Ordering;
12
13/// Takes the absolute value of a number. Assumes that the number has a representable absolute
14/// value.
15pub trait Abs {
16    type Output;
17
18    fn abs(self) -> Self::Output;
19}
20
21/// Replaces a number with its absolute value. Assumes that the number has a representable absolute
22/// value.
23pub trait AbsAssign {
24    fn abs_assign(&mut self);
25}
26
27/// Takes the absolute value of a number and converts to the unsigned equivalent.
28pub trait UnsignedAbs {
29    type Output;
30
31    fn unsigned_abs(self) -> Self::Output;
32}
33
34/// Subtracts two numbers and takes the absolute value of the difference.
35pub trait AbsDiff<RHS = Self> {
36    type Output;
37
38    fn abs_diff(self, other: RHS) -> Self::Output;
39}
40
41/// Replaces a number with the absolute value of its difference with another number.
42pub trait AbsDiffAssign<RHS = Self> {
43    fn abs_diff_assign(&mut self, other: RHS);
44}
45
46/// Adds a number and the product of two other numbers.
47///
48/// Depending on the implementing type, the fused operation may compute the same value as the
49/// unfused `self + y * z` more efficiently; or, for types with rounding, it may compute a *more
50/// accurate* value -- the product enters the addition exactly, with a single rounding at the end --
51/// but *less* efficiently, since the exact product must be computed in full. See each
52/// implementation's documentation for which contract it provides.
53pub trait AddMul<Y = Self, Z = Self> {
54    type Output;
55
56    fn add_mul(self, y: Y, z: Z) -> Self::Output;
57}
58
59/// Adds a number and the product of two other numbers, in place.
60///
61/// Depending on the implementing type, the fused operation may compute the same value as the
62/// unfused `*self + y * z` more efficiently; or, for types with rounding, it may compute a *more
63/// accurate* value -- the product enters the addition exactly, with a single rounding at the end --
64/// but *less* efficiently, since the exact product must be computed in full. See each
65/// implementation's documentation for which contract it provides.
66pub trait AddMulAssign<Y = Self, Z = Self> {
67    fn add_mul_assign(&mut self, y: Y, z: Z);
68}
69
70/// Adds the products of two pairs of numbers.
71pub trait MulAddMul<Y = Self, Z = Self, W = Self> {
72    type Output;
73
74    fn mul_add_mul(self, y: Y, z: Z, w: W) -> Self::Output;
75}
76
77/// Adds the products of two pairs of numbers, in place.
78pub trait MulAddMulAssign<Y = Self, Z = Self, W = Self> {
79    fn mul_add_mul_assign(&mut self, y: Y, z: Z, w: W);
80}
81
82/// Multiplies two numbers and right-shifts the product (divides it by a power of 2), rounding the
83/// result according to a specified rounding mode. An [`Ordering`] is also returned, indicating
84/// whether the returned value is less than, equal to, or greater than the exact value.
85///
86/// The product is computed exactly, as if at unlimited width; only the final shifted result must be
87/// representable.
88pub trait MulShrRound<RHS = Self, B = u64> {
89    type Output;
90
91    fn mul_shr_round(self, other: RHS, bits: B, rm: RoundingMode) -> (Self::Output, Ordering);
92}
93
94/// Multiplies two numbers and right-shifts the product (divides it by a power of 2) in place,
95/// rounding the result according to a specified rounding mode. An [`Ordering`] is returned,
96/// indicating whether the assigned value is less than, equal to, or greater than the exact value.
97///
98/// The product is computed exactly, as if at unlimited width; only the final shifted result must be
99/// representable.
100pub trait MulShrRoundAssign<RHS = Self, B = u64> {
101    fn mul_shr_round_assign(&mut self, other: RHS, bits: B, rm: RoundingMode) -> Ordering;
102}
103
104/// Subtracts the product of one pair of numbers from the product of another.
105pub trait MulSubMul<Y = Self, Z = Self, W = Self> {
106    type Output;
107
108    fn mul_sub_mul(self, y: Y, z: Z, w: W) -> Self::Output;
109}
110
111/// Subtracts the product of one pair of numbers from the product of another, in place.
112pub trait MulSubMulAssign<Y = Self, Z = Self, W = Self> {
113    fn mul_sub_mul_assign(&mut self, y: Y, z: Z, w: W);
114}
115
116/// Calculates the AGM (arithmetic-geometric mean) of two numbers.
117pub trait Agm<RHS = Self> {
118    type Output;
119
120    fn agm(self, other: RHS) -> Self::Output;
121}
122
123/// Replaces a number with the AGM (arithmetic-geometric mean) of it and another number.
124pub trait AgmAssign<RHS = Self> {
125    fn agm_assign(&mut self, other: RHS);
126}
127
128/// Calculates the hypotenuse of two numbers, $\sqrt{x^2+y^2}$.
129pub trait Hypot<RHS = Self> {
130    type Output;
131
132    fn hypot(self, other: RHS) -> Self::Output;
133}
134
135/// Replaces a number with the hypotenuse of it and another number.
136pub trait HypotAssign<RHS = Self> {
137    fn hypot_assign(&mut self, other: RHS);
138}
139
140/// Calculates the compound function $(1+x)^n$ of a number $x$.
141pub trait Compound<N> {
142    type Output;
143
144    fn compound(self, n: N) -> Self::Output;
145}
146
147/// Replaces a number $x$ with the compound function $(1+x)^n$.
148pub trait CompoundAssign<N> {
149    fn compound_assign(&mut self, n: N);
150}
151
152/// Left-shifts a number (multiplies it by a power of 2), returning `None` if the result is not
153/// representable.
154pub trait ArithmeticCheckedShl<RHS> {
155    type Output;
156
157    fn arithmetic_checked_shl(self, other: RHS) -> Option<Self::Output>;
158}
159
160/// Right-shifts a number (divides it by a power of 2), returning `None` if the result is not
161/// representable.
162pub trait ArithmeticCheckedShr<RHS> {
163    type Output;
164
165    fn arithmetic_checked_shr(self, other: RHS) -> Option<Self::Output>;
166}
167
168/// Computes the average (arithmetic mean) of two numbers, rounding to the nearest integer. Two-way
169/// ties are broken by rounding to the even integer.
170///
171/// The average is computed without overflow: the result is always exact or within a half of the
172/// exact value, so it always fits in the same type as the inputs.
173pub trait Average<RHS = Self> {
174    type Output;
175
176    fn average(self, other: RHS) -> Self::Output;
177}
178
179/// Computes the average (arithmetic mean) of two numbers, rounding to the nearest integer and
180/// replacing the first number with it. Two-way ties are broken by rounding to the even integer.
181///
182/// The average is computed without overflow: the result is always exact or within a half of the
183/// exact value, so it always fits in the same type as the inputs.
184pub trait AverageAssign<RHS = Self> {
185    fn average_assign(&mut self, other: RHS);
186}
187
188/// Computes the average (arithmetic mean) of two numbers and rounds according to a specified
189/// rounding mode. An [`Ordering`] is also returned, indicating whether the returned value is less
190/// than, equal to, or greater than the exact value.
191///
192/// The average is computed without overflow: the result is always exact or within a half of the
193/// exact value, so it always fits in the same type as the inputs.
194pub trait AverageRound<RHS = Self> {
195    type Output;
196
197    fn average_round(self, other: RHS, rm: RoundingMode) -> (Self::Output, Ordering);
198}
199
200/// Computes the average (arithmetic mean) of two numbers, rounding according to a specified
201/// rounding mode and replacing the first number with it. An [`Ordering`] is returned, indicating
202/// whether the assigned value is less than, equal to, or greater than the exact value.
203///
204/// The average is computed without overflow: the result is always exact or within a half of the
205/// exact value, so it always fits in the same type as the inputs.
206pub trait AverageRoundAssign<RHS = Self> {
207    fn average_round_assign(&mut self, other: RHS, rm: RoundingMode) -> Ordering;
208}
209
210pub trait BinomialCoefficient<T = Self> {
211    fn binomial_coefficient(n: T, k: T) -> Self;
212}
213
214pub trait CheckedBinomialCoefficient<T = Self>: Sized {
215    fn checked_binomial_coefficient(n: T, k: T) -> Option<Self>;
216}
217
218/// Takes the ceiling of a number.
219pub trait Ceiling {
220    type Output;
221
222    fn ceiling(self) -> Self::Output;
223}
224
225/// Replaces a number with its ceiling.
226pub trait CeilingAssign {
227    fn ceiling_assign(&mut self);
228}
229
230/// Takes the absolute valie of a number, returning `None` if the result is not representable.
231pub trait CheckedAbs {
232    type Output;
233
234    fn checked_abs(self) -> Option<Self::Output>;
235}
236
237/// Adds two numbers, returning `None` if the result is not representable.
238pub trait CheckedAdd<RHS = Self> {
239    type Output;
240
241    fn checked_add(self, other: RHS) -> Option<Self::Output>;
242}
243
244/// Adds a number and the product of two other numbers, returning `None` if the result is not
245/// representable.
246pub trait CheckedAddMul<Y = Self, Z = Self> {
247    type Output;
248
249    fn checked_add_mul(self, y: Y, z: Z) -> Option<Self::Output>;
250}
251
252/// Adds the products of two pairs of numbers, returning `None` if the result is not representable.
253pub trait CheckedMulAddMul<Y = Self, Z = Self, W = Self> {
254    type Output;
255
256    fn checked_mul_add_mul(self, y: Y, z: Z, w: W) -> Option<Self::Output>;
257}
258
259/// Subtracts the product of one pair of numbers from the product of another, returning `None` if
260/// the result is not representable.
261pub trait CheckedMulSubMul<Y = Self, Z = Self, W = Self> {
262    type Output;
263
264    fn checked_mul_sub_mul(self, y: Y, z: Z, w: W) -> Option<Self::Output>;
265}
266
267/// Divides two numbers, returning `None` if the result is not representable.
268pub trait CheckedDiv<RHS = Self> {
269    type Output;
270
271    fn checked_div(self, other: RHS) -> Option<Self::Output>;
272}
273
274/// Multiplies two numbers, returning `None` if the result is not representable.
275pub trait CheckedMul<RHS = Self> {
276    type Output;
277
278    fn checked_mul(self, other: RHS) -> Option<Self::Output>;
279}
280
281/// Negates a number, returning `None` if the result is not representable.
282pub trait CheckedNeg {
283    type Output;
284
285    fn checked_neg(self) -> Option<Self::Output>;
286}
287
288/// Finds the smallest integer power of 2 greater than or equal to a number, returning `None` if the
289/// result is not representable.
290pub trait CheckedNextPowerOf2 {
291    type Output;
292
293    fn checked_next_power_of_2(self) -> Option<Self::Output>;
294}
295
296/// Raises a number to a power, returning `None` if the result is not representable.
297pub trait CheckedPow<RHS> {
298    type Output;
299
300    fn checked_pow(self, exp: RHS) -> Option<Self::Output>;
301}
302
303/// Squares a number, returning `None` if the result is not representable.
304pub trait CheckedSquare {
305    type Output;
306
307    fn checked_square(self) -> Option<Self::Output>;
308}
309
310/// Subtracts two numbers, returning `None` if the result is not representable.
311pub trait CheckedSub<RHS = Self> {
312    type Output;
313
314    fn checked_sub(self, other: RHS) -> Option<Self::Output>;
315}
316
317/// Subtracts a number by the product of two other numbers, returning `None` if the result is not
318/// representable.
319pub trait CheckedSubMul<Y = Self, Z = Self> {
320    type Output;
321
322    fn checked_sub_mul(self, y: Y, z: Z) -> Option<Self::Output>;
323}
324
325/// Determines whether two numbers are coprime.
326pub trait CoprimeWith<RHS = Self> {
327    fn coprime_with(self, other: RHS) -> bool;
328}
329
330/// Combines two congruences by the Chinese remainder theorem, returning `None` if the moduli are
331/// not coprime. The residues must be already reduced modulo their moduli.
332pub trait Crt<M1 = Self, R2 = Self, M2 = Self> {
333    type Output;
334
335    fn crt(self, m1: M1, r2: R2, m2: M2) -> Option<Self::Output>;
336}
337
338/// Combines two congruences by the Chinese remainder theorem, returning the representative of
339/// smallest absolute value, or `None` if the moduli are not coprime. The first residue may be
340/// negative.
341pub trait BalancedCrt<M1 = Self, R2 = Self, M2 = Self> {
342    type Output;
343
344    fn balanced_crt(self, m1: M1, r2: R2, m2: M2) -> Option<Self::Output>;
345}
346
347/// Divides two numbers, assuming the first exactly divides the second.
348///
349/// If it doesn't, the `div_exact` function may panic or return a meaningless result.
350pub trait DivExact<RHS = Self> {
351    type Output;
352
353    fn div_exact(self, other: RHS) -> Self::Output;
354}
355
356/// Divides a number by another number in place, assuming the first exactly divides the second.
357///
358/// If it doesn't, this function may panic or assign a meaningless number to the first number.
359pub trait DivExactAssign<RHS = Self> {
360    fn div_exact_assign(&mut self, other: RHS);
361}
362
363/// Divides two numbers, returning the quotient and remainder. The quotient is rounded towards
364/// negative infinity, and the remainder has the same sign as the divisor (second input).
365///
366/// The quotient and remainder satisfy $x = qy + r$ and $0 \leq |r| < |y|$.
367pub trait DivMod<RHS = Self> {
368    type DivOutput;
369    type ModOutput;
370
371    fn div_mod(self, other: RHS) -> (Self::DivOutput, Self::ModOutput);
372}
373
374/// Divides two numbers, returning the quotient and remainder. The quotient is rounded towards
375/// negative infinity, and the remainder has the same sign as the divisor (second input).
376///
377/// The quotient and remainder satisfy $x = qy + r$ and $0 \leq |r| < |y|$.
378///
379/// If multiple divisions by the same divisor are necessary, it can be quicker to precompute some
380/// piece of data based on the divisor and reuse it in the division calls. This trait provides a
381/// function for precomputing the data and a function for using it during division.
382pub trait DivModPrecomputed<RHS = Self> {
383    type DivOutput;
384    type ModOutput;
385    type Data;
386
387    /// Precomputes some data to use for division.
388    fn precompute_div_mod_data(other: &RHS) -> Self::Data;
389
390    fn div_mod_precomputed(
391        self,
392        other: RHS,
393        data: &Self::Data,
394    ) -> (Self::DivOutput, Self::ModOutput);
395}
396
397/// Divides a number by another number in place, returning the remainder. The quotient is rounded
398/// towards negative infinity, and the remainder has the same sign as the divisor (second input).
399///
400/// The quotient and remainder satisfy $x = qy + r$ and $0 \leq |r| < |y|$.
401///
402/// If multiple divisions by the same divisor are necessary, it can be quicker to precompute some
403/// piece of data based on the divisor and reuse it in the division calls. This trait provides a
404/// function for using precomputed data during division. For precomputing the data, use the
405/// [`precompute_div_mod_data`](DivModPrecomputed::precompute_div_mod_data) function in
406/// [`DivModPrecomputed`].
407pub trait DivAssignModPrecomputed<RHS = Self>: DivModPrecomputed<RHS> {
408    fn div_assign_mod_precomputed(&mut self, other: RHS, data: &Self::Data) -> Self::ModOutput;
409}
410
411/// Divides two numbers, returning just the quotient. The quotient is rounded towards the quotient
412/// that makes the remainder nonnegative.
413///
414/// If the remainder were computed, the quotient and remainder would satisfy $x = qy + r$ and $0
415/// \leq r < |y|$.
416pub trait DivEuclidean<RHS = Self> {
417    type Output;
418
419    fn div_euclidean(self, other: RHS) -> Self::Output;
420}
421
422/// Divides a number by another number in place, keeping just the quotient. The quotient is rounded
423/// towards the quotient that makes the remainder nonnegative.
424///
425/// If the remainder were computed, the quotient and remainder would satisfy $x = qy + r$ and $0
426/// \leq r < |y|$.
427pub trait DivEuclideanAssign<RHS = Self> {
428    fn div_euclidean_assign(&mut self, other: RHS);
429}
430
431/// Divides two numbers, returning the quotient and remainder. The quotient is rounded towards the
432/// quotient that makes the remainder nonnegative, and the remainder is always nonnegative.
433///
434/// The quotient and remainder satisfy $x = qy + r$ and $0 \leq r < |y|$.
435pub trait DivModEuclidean<RHS = Self> {
436    type DivOutput;
437    type ModOutput;
438
439    fn div_mod_euclidean(self, other: RHS) -> (Self::DivOutput, Self::ModOutput);
440}
441
442/// Divides a number by another number in place, returning the remainder. The quotient is rounded
443/// towards negative infinity, and the remainder has the same sign as the divisor (second input).
444///
445/// The quotient and remainder satisfy $x = qy + r$ and $0 \leq |r| < |y|$.
446pub trait DivAssignMod<RHS = Self> {
447    type ModOutput;
448
449    fn div_assign_mod(&mut self, other: RHS) -> Self::ModOutput;
450}
451
452/// Divides a number by another number in place, returning the remainder. The quotient is rounded
453/// towards the quotient that makes the remainder nonnegative, and the remainder is always
454/// nonnegative.
455///
456/// The quotient and remainder satisfy $x = qy + r$ and $0 \leq r < |y|$.
457pub trait DivAssignModEuclidean<RHS = Self> {
458    type ModOutput;
459
460    fn div_assign_mod_euclidean(&mut self, other: RHS) -> Self::ModOutput;
461}
462
463/// Divides two numbers, returning the quotient and remainder. The quotient is rounded towards zero,
464/// and the remainder has the same sign as the dividend (first input).
465///
466/// The quotient and remainder satisfy $x = qy + r$ and $0 \leq |r| < |y|$.
467pub trait DivRem<RHS = Self> {
468    type DivOutput;
469    type RemOutput;
470
471    fn div_rem(self, other: RHS) -> (Self::DivOutput, Self::RemOutput);
472}
473
474/// Divides a number by another number in place, returning the remainder. The quotient is rounded
475/// towards zero, and the remainder has the same sign as the dividend (first input).
476///
477/// The quotient and remainder satisfy $x = qy + r$ and $0 \leq |r| < |y|$.
478pub trait DivAssignRem<RHS = Self> {
479    type RemOutput;
480
481    fn div_assign_rem(&mut self, other: RHS) -> Self::RemOutput;
482}
483
484/// Divides a number by another number, returning the ceiling of the quotient and the remainder of
485/// the negative of the first number divided by the second.
486///
487/// The quotient and remainder satisfy $x = qy - r$ and $0 \leq r < y$.
488pub trait CeilingDivNegMod<RHS = Self> {
489    type DivOutput;
490    type ModOutput;
491
492    fn ceiling_div_neg_mod(self, other: RHS) -> (Self::DivOutput, Self::ModOutput);
493}
494
495/// Divides a number by another number in place, taking the ceiling of the quotient and returning
496/// the remainder of the negative of the first number divided by the second.
497///
498/// The quotient and remainder satisfy $x = qy - r$ and $0 \leq r < y$.
499pub trait CeilingDivAssignNegMod<RHS = Self> {
500    type ModOutput;
501
502    fn ceiling_div_assign_neg_mod(&mut self, other: RHS) -> Self::ModOutput;
503}
504
505/// Divides a number by another number, returning the quotient and remainder. The quotient is
506/// rounded towards positive infinity and the remainder has the opposite sign as the divisor (second
507/// input).
508///
509/// The quotient and remainder satisfy $x = qy + r$ and $0 \leq |r| < |y|$.
510pub trait CeilingDivMod<RHS = Self> {
511    type DivOutput;
512    type ModOutput;
513
514    fn ceiling_div_mod(self, other: RHS) -> (Self::DivOutput, Self::ModOutput);
515}
516
517/// Divides a number by another number in place, taking the quotient and returning the remainder.
518/// The quotient is rounded towards positive infinity and the remainder has the opposite sign of the
519/// divisor (second input).
520///
521/// The quotient and remainder satisfy $x = qy + r$ and $0 \leq |r| < |y|$.
522pub trait CeilingDivAssignMod<RHS = Self> {
523    type ModOutput;
524
525    fn ceiling_div_assign_mod(&mut self, other: RHS) -> Self::ModOutput;
526}
527
528/// Divides a number by another number and rounds according to a specified rounding mode. An
529/// [`Ordering`] is also returned, indicating whether the returned value is less than, equal to, or
530/// greater than the exact value.
531pub trait DivRound<RHS = Self> {
532    type Output;
533
534    fn div_round(self, other: RHS, rm: RoundingMode) -> (Self::Output, Ordering);
535}
536
537/// Divides a number by another number in place and rounds according to a specified rounding mode.
538/// An [`Ordering`] is returned, indicating whether the assigned value is less than, equal to, or
539/// greater than the exact value.
540pub trait DivRoundAssign<RHS = Self> {
541    fn div_round_assign(&mut self, other: RHS, rm: RoundingMode) -> Ordering;
542}
543
544/// Determines whether a number is divisible by $2^k$.
545pub trait DivisibleByPowerOf2 {
546    fn divisible_by_power_of_2(self, pow: u64) -> bool;
547}
548
549/// Determines whether a number is divisible by another number.
550pub trait DivisibleBy<RHS = Self> {
551    fn divisible_by(self, other: RHS) -> bool;
552}
553
554/// Determines whether a number is equivalent to another number modulo $2^k$.
555pub trait EqModPowerOf2<RHS = Self> {
556    fn eq_mod_power_of_2(self, other: RHS, pow: u64) -> bool;
557}
558
559/// Determines whether a number is equivalent to another number modulo $m$.
560pub trait EqMod<RHS = Self, M = Self> {
561    fn eq_mod(self, other: RHS, m: M) -> bool;
562}
563
564/// Computes the GCD (greatest common divisor) of two numbers $a$ and $b$, and also the coefficients
565/// $x$ and $y$ in Bézout's identity $ax+by=\gcd(a,b)$.
566///
567/// The are infinitely many $x$, $y$ that satisfy the identity, so the full specification is more
568/// detailed:
569///
570/// - $f(0, 0) = (0, 0, 0)$.
571/// - $f(a, ak) = (a, 1, 0)$ if $a > 0$ and $k \neq 1$.
572/// - $f(a, ak) = (-a, -1, 0)$ if $a < 0$ and $k \neq 1$.
573/// - $f(bk, b) = (b, 0, 1)$ if $b > 0$.
574/// - $f(bk, b) = (-b, 0, -1)$ if $b < 0$.
575/// - $f(a, b) = (g, x, y)$ if $a \neq 0$ and $b \neq 0$ and $\gcd(a, b) \neq \min(|a|, |b|)$, where
576///   $g = \gcd(a, b) \geq 0$, $ax + by = g$, $x \leq \lfloor b/g \rfloor$, and $y \leq \lfloor a/g
577///   \rfloor$.
578pub trait ExtendedGcd<RHS = Self> {
579    type Gcd;
580    type Cofactor;
581
582    fn extended_gcd(self, other: RHS) -> (Self::Gcd, Self::Cofactor, Self::Cofactor);
583}
584
585/// Computes the $n$th Bell number: the number of ways to partition a set of $n$ elements.
586pub trait BellNumber {
587    fn bell_number(n: u64) -> Self;
588}
589
590/// Computes the $n$th Bell number, returning `None` if the result is too large to be represented.
591pub trait CheckedBellNumber: Sized {
592    fn checked_bell_number(n: u64) -> Option<Self>;
593}
594
595/// Computes the factorial of a `u64`.
596pub trait Factorial {
597    fn factorial(n: u64) -> Self;
598}
599
600/// Computes the factorial of a `u64`, returning `None` if the result is too large to be
601/// represented.
602pub trait CheckedFactorial: Sized {
603    fn checked_factorial(n: u64) -> Option<Self>;
604}
605
606/// Computes the double factorial of a `u64`. The double factorial of a non-negative integer is the
607/// product of all the positive integers that are less than or equal to it and have the same parity
608/// as it.
609pub trait DoubleFactorial {
610    fn double_factorial(n: u64) -> Self;
611}
612
613/// Computes the double factorial of a `u64`, returning `None` if the result is too large to be
614/// represented. The double factorial of a non-negative integer is the product of all the positive
615/// integers that are less than or equal to it and have the same parity as it.
616pub trait CheckedDoubleFactorial: Sized {
617    fn checked_double_factorial(n: u64) -> Option<Self>;
618}
619
620/// Computes the $m$-multifactorial of a `u64`. The $m$-multifactorial of a non-negative integer $n$
621/// is the product of all integers $k$ such that $0<k\leq n$ and $k\equiv n \pmod m$.
622pub trait Multifactorial {
623    fn multifactorial(n: u64, m: u64) -> Self;
624}
625
626/// Computes the $m$-multifactorial of a `u64`, returning `None` if the result is too large to be
627/// represented. The $m$-multifactorial of a non-negative integer $n$ is the product of all integers
628/// $k$ such that $0<k\leq n$ and $k\equiv n \pmod m$.
629pub trait CheckedMultifactorial: Sized {
630    fn checked_multifactorial(n: u64, m: u64) -> Option<Self>;
631}
632
633/// Computes the subfactorial of a `u64`. The subfactorial of a non-negative integer $n$ counts the
634/// number of derangements of $n$ elements, which are the permutations in which no element is fixed.
635pub trait Subfactorial {
636    fn subfactorial(n: u64) -> Self;
637}
638
639/// Computes the subfactorial of a `u64`, returning `None` if the result is too large to be
640/// represented. The subfactorial of a non-negative integer $n$ counts the number of derangements of
641/// $n$ elements, which are the permutations in which no element is fixed.
642pub trait CheckedSubfactorial: Sized {
643    fn checked_subfactorial(n: u64) -> Option<Self>;
644}
645
646/// Computes the rising factorial of a number: the product of the `n` consecutive numbers starting
647/// at `self`, or 1 when `n` is 0.
648pub trait RisingFactorial {
649    type Output;
650
651    fn rising_factorial(self, n: u64) -> Self::Output;
652}
653
654/// Computes the rising factorial of a number, returning `None` if the result cannot be represented.
655pub trait CheckedRisingFactorial: Sized {
656    fn checked_rising_factorial(self, n: u64) -> Option<Self>;
657}
658
659/// Computes the $n$th Fibonacci number, either alone or paired with its predecessor:
660/// `fibonacci_pair(n)` returns $(F(n), F(n-1))$.
661pub trait Fibonacci: Sized {
662    fn fibonacci(n: u64) -> Self;
663
664    fn fibonacci_pair(n: u64) -> (Self, Self);
665}
666
667/// Computes the $n$th Fibonacci number, either alone or paired with its predecessor, returning
668/// `None` if the result is too large to be represented.
669pub trait CheckedFibonacci: Sized {
670    fn checked_fibonacci(n: u64) -> Option<Self>;
671
672    fn checked_fibonacci_pair(n: u64) -> Option<(Self, Self)>;
673}
674
675/// Takes the floor of a number.
676pub trait Floor {
677    type Output;
678
679    fn floor(self) -> Self::Output;
680}
681
682/// Replaces a number with its floor.
683pub trait FloorAssign {
684    fn floor_assign(&mut self);
685}
686
687/// Calculates the GCD (greatest common divisor) of two numbers.
688pub trait Gcd<RHS = Self> {
689    type Output;
690
691    fn gcd(self, other: RHS) -> Self::Output;
692}
693
694/// Replaces a number with the GCD (greatest common divisor) of it and another number.
695pub trait GcdAssign<RHS = Self> {
696    fn gcd_assign(&mut self, other: RHS);
697}
698
699/// Determines whether a number is an integer power of 2.
700pub trait IsPowerOf2 {
701    fn is_power_of_2(&self) -> bool;
702}
703
704/// Calculates the LCM (least common multiple) of two numbers.
705pub trait Lcm<RHS = Self> {
706    type Output;
707
708    fn lcm(self, other: RHS) -> Self::Output;
709}
710
711/// Replaces a number with the LCM (least common multiple) of it and another number.
712pub trait LcmAssign<RHS = Self> {
713    fn lcm_assign(&mut self, other: RHS);
714}
715
716/// Computes $e^x$, the exponential of a number.
717pub trait Exp {
718    type Output;
719
720    fn exp(self) -> Self::Output;
721}
722
723/// Replaces a number with its exponential, $e^x$.
724pub trait ExpAssign {
725    fn exp_assign(&mut self);
726}
727
728/// Computes $e^x-1$, the exponential of a number, minus one.
729pub trait ExpXMinus1 {
730    type Output;
731
732    fn exp_x_minus_1(self) -> Self::Output;
733}
734
735/// Replaces a number $x$ with $e^x-1$.
736pub trait ExpXMinus1Assign {
737    fn exp_x_minus_1_assign(&mut self);
738}
739
740/// Computes $2^x-1$, two raised to the power of a number, minus one.
741pub trait PowerOf2XMinus1 {
742    type Output;
743
744    fn power_of_2_x_minus_1(self) -> Self::Output;
745}
746
747/// Replaces a number $x$ with $2^x-1$.
748pub trait PowerOf2XMinus1Assign {
749    fn power_of_2_x_minus_1_assign(&mut self);
750}
751
752/// Computes $10^x-1$, ten raised to the power of a number, minus one.
753pub trait PowerOf10XMinus1 {
754    type Output;
755
756    fn power_of_10_x_minus_1(self) -> Self::Output;
757}
758
759/// Replaces a number $x$ with $10^x-1$.
760pub trait PowerOf10XMinus1Assign {
761    fn power_of_10_x_minus_1_assign(&mut self);
762}
763
764/// Takes the natural logarithm of a number.
765pub trait Ln {
766    type Output;
767
768    fn ln(self) -> Self::Output;
769}
770
771/// Replaces a number with its natural logarithm.
772pub trait LnAssign {
773    fn ln_assign(&mut self);
774}
775
776/// Computes $\ln(1+x)$.
777pub trait Ln1PlusX {
778    type Output;
779
780    fn ln_1_plus_x(self) -> Self::Output;
781}
782
783/// Replaces a number $x$ by $\ln(1+x)$.
784pub trait Ln1PlusXAssign {
785    fn ln_1_plus_x_assign(&mut self);
786}
787
788/// Calculates the LCM (least common multiple) of two numbers, returning `None` if the result is not
789/// representable.
790pub trait CheckedLcm<RHS = Self> {
791    type Output;
792
793    fn checked_lcm(self, other: RHS) -> Option<Self::Output>;
794}
795
796/// Calculates the Legendre symbol of two numbers. Typically the implementations will be identical
797/// to those of [`JacobiSymbol`].
798pub trait LegendreSymbol<RHS = Self> {
799    fn legendre_symbol(self, other: RHS) -> i8;
800}
801
802/// Calculates the Jacobi symbol of two numbers.
803pub trait JacobiSymbol<RHS = Self> {
804    fn jacobi_symbol(self, other: RHS) -> i8;
805}
806
807/// Calculates the Kronecker symbol of two numbers.
808pub trait KroneckerSymbol<RHS = Self> {
809    fn kronecker_symbol(self, other: RHS) -> i8;
810}
811
812/// Calculates the base-$b$ logarithm of a number, or returns `None` if the number is not a perfect
813/// power of $b$.
814pub trait CheckedLogBase<B = Self> {
815    type Output;
816
817    fn checked_log_base(self, base: B) -> Option<Self::Output>;
818}
819
820/// Calculates the floor of the base-$b$ logarithm of a number.
821pub trait FloorLogBase<B = Self> {
822    type Output;
823
824    fn floor_log_base(self, base: B) -> Self::Output;
825}
826
827/// Calculates the ceiling of the base-$b$ logarithm of a number.
828pub trait CeilingLogBase<B = Self> {
829    type Output;
830
831    fn ceiling_log_base(self, base: B) -> Self::Output;
832}
833
834/// Calculates the base-2 logarithm of a number, or returns `None` if the number is not a perfect
835/// power of 2.
836pub trait CheckedLogBase2 {
837    type Output;
838
839    fn checked_log_base_2(self) -> Option<Self::Output>;
840}
841
842/// Calculates the base-2 logarithm of a number.
843pub trait LogBase2 {
844    type Output;
845
846    fn log_base_2(self) -> Self::Output;
847}
848
849/// Replaces a number with its base-2 logarithm.
850pub trait LogBase2Assign {
851    fn log_base_2_assign(&mut self);
852}
853
854/// Calculates the base-10 logarithm of a number, rounding the (generally irrational) result.
855pub trait LogBase10 {
856    type Output;
857
858    fn log_base_10(self) -> Self::Output;
859}
860
861/// Replaces a number with its base-10 logarithm, rounding the (generally irrational) result.
862pub trait LogBase10Assign {
863    fn log_base_10_assign(&mut self);
864}
865
866/// Computes $\log_2(1+x)$.
867pub trait LogBase2Of1PlusX {
868    type Output;
869
870    fn log_base_2_1_plus_x(self) -> Self::Output;
871}
872
873/// Replaces a number $x$ by $\log_2(1+x)$.
874pub trait LogBase2Of1PlusXAssign {
875    fn log_base_2_1_plus_x_assign(&mut self);
876}
877
878/// Computes $\log_{2^k}(1+x)$.
879pub trait LogBasePowerOf2Of1PlusX<POW> {
880    type Output;
881
882    fn log_base_power_of_2_1_plus_x(self, pow: POW) -> Self::Output;
883}
884
885/// Replaces a number $x$ by $\log_{2^k}(1+x)$.
886pub trait LogBasePowerOf2Of1PlusXAssign<POW> {
887    fn log_base_power_of_2_1_plus_x_assign(&mut self, pow: POW);
888}
889
890/// Computes $\log_b(1+x)$ for an integer base $b$.
891pub trait LogBaseOf1PlusX<B = Self> {
892    type Output;
893
894    fn log_base_1_plus_x(self, base: B) -> Self::Output;
895}
896
897/// Replaces a number $x$ by $\log_b(1+x)$ for an integer base $b$.
898pub trait LogBaseOf1PlusXAssign<B = Self> {
899    fn log_base_1_plus_x_assign(&mut self, base: B);
900}
901
902/// Computes $\log_{10}(1+x)$.
903pub trait LogBase10Of1PlusX {
904    type Output;
905
906    fn log_base_10_1_plus_x(self) -> Self::Output;
907}
908
909/// Replaces a number $x$ by $\log_{10}(1+x)$.
910pub trait LogBase10Of1PlusXAssign {
911    fn log_base_10_1_plus_x_assign(&mut self);
912}
913
914/// Calculates the floor of the base-2 logarithm of a number.
915pub trait FloorLogBase2 {
916    type Output;
917
918    fn floor_log_base_2(self) -> Self::Output;
919}
920
921/// Calculates the ceiling of the base-2 logarithm of a number.
922pub trait CeilingLogBase2 {
923    type Output;
924
925    fn ceiling_log_base_2(self) -> Self::Output;
926}
927
928/// Calculates the base-$2^k$ logarithm of a number, or returns `None` if the number is not a
929/// perfect power of $2^k$.
930pub trait CheckedLogBasePowerOf2<POW> {
931    type Output;
932
933    fn checked_log_base_power_of_2(self, pow: POW) -> Option<Self::Output>;
934}
935
936/// Calculates the floor of the base-$2^k$ logarithm of a number.
937pub trait FloorLogBasePowerOf2<POW> {
938    type Output;
939
940    fn floor_log_base_power_of_2(self, pow: POW) -> Self::Output;
941}
942
943/// Calculates the ceiling of the base-$2^k$ logarithm of a number.
944pub trait CeilingLogBasePowerOf2<POW> {
945    type Output;
946
947    fn ceiling_log_base_power_of_2(self, pow: POW) -> Self::Output;
948}
949
950/// Calculates the base-$2^k$ logarithm of a number.
951pub trait LogBasePowerOf2<POW> {
952    type Output;
953
954    fn log_base_power_of_2(self, pow: POW) -> Self::Output;
955}
956
957/// Replaces a number with its base-$2^k$ logarithm.
958pub trait LogBasePowerOf2Assign<POW> {
959    fn log_base_power_of_2_assign(&mut self, pow: POW);
960}
961
962/// Calculates the base-$b$ logarithm of a number, rounding the (generally irrational) result.
963pub trait LogBase<B = Self> {
964    type Output;
965
966    fn log_base(self, base: B) -> Self::Output;
967}
968
969/// Replaces a number with its base-$b$ logarithm, rounding the (generally irrational) result.
970pub trait LogBaseAssign<B = Self> {
971    fn log_base_assign(&mut self, base: B);
972}
973
974/// Computes the $n$th Lucas number, either alone or paired with its predecessor:
975/// `lucas_number_pair(n)` returns $(L(n), L(n-1))$.
976pub trait LucasNumber: Sized {
977    fn lucas_number(n: u64) -> Self;
978
979    fn lucas_number_pair(n: u64) -> (Self, Self);
980}
981
982/// Computes the $n$th Lucas number, either alone or paired with its predecessor, returning `None`
983/// if the result is too large to be represented.
984pub trait CheckedLucasNumber: Sized {
985    fn checked_lucas_number(n: u64) -> Option<Self>;
986
987    fn checked_lucas_number_pair(n: u64) -> Option<(Self, Self)>;
988}
989
990/// Adds two numbers modulo a third number $m$. The inputs must be already reduced modulo $m$.
991pub trait ModAdd<RHS = Self, M = Self> {
992    type Output;
993
994    fn mod_add(self, other: RHS, m: M) -> Self::Output;
995}
996
997/// Adds two numbers modulo a third number $m$, in place. The inputs must be already reduced modulo
998/// $m$.
999pub trait ModAddAssign<RHS = Self, M = Self> {
1000    fn mod_add_assign(&mut self, other: RHS, m: M);
1001}
1002
1003/// Divides a number by another number modulo a third number $m$, returning `None` if no quotient
1004/// exists. The inputs must be already reduced modulo $m$.
1005///
1006/// If the divisor is not invertible modulo $m$, a quotient may exist without being unique; in that
1007/// case one of the quotients is returned.
1008pub trait ModDiv<RHS = Self, M = Self> {
1009    type Output;
1010
1011    fn mod_div(self, other: RHS, m: M) -> Option<Self::Output>;
1012}
1013
1014/// Finds all quotients of a number and another number modulo a third number $m$, returning `None`
1015/// if no quotient exists. The inputs must be already reduced modulo $m$.
1016///
1017/// The quotients form an arithmetic progression: `Some((start, stride, length))` means that the
1018/// quotients are exactly the numbers $\text{start} + \text{stride} \cdot i$ for $0 \leq i <
1019/// \text{length}$, where `start` is the smallest quotient.
1020pub trait ModDivList<RHS = Self, M = Self> {
1021    type Output;
1022
1023    #[allow(clippy::type_complexity)]
1024    fn mod_div_list(self, other: RHS, m: M) -> Option<(Self::Output, Self::Output, Self::Output)>;
1025}
1026
1027/// Finds the multiplicative inverse of a number modulo another number $m$. The input must be
1028/// already reduced modulo $m$.
1029pub trait ModInverse<M = Self> {
1030    type Output;
1031
1032    fn mod_inverse(self, m: M) -> Option<Self::Output>;
1033}
1034
1035/// Checks whether a number is reduced modulo another number $m$.
1036pub trait ModIsReduced<M = Self> {
1037    fn mod_is_reduced(&self, m: &M) -> bool;
1038}
1039
1040/// Multiplies two numbers modulo a third number $m$. The inputs must be already reduced modulo $m$.
1041pub trait ModMul<RHS = Self, M = Self> {
1042    type Output;
1043
1044    fn mod_mul(self, other: RHS, m: M) -> Self::Output;
1045}
1046
1047/// Multiplies two numbers modulo a third number $m$, in place. The inputs must be already reduced
1048/// modulo $m$.
1049pub trait ModMulAssign<RHS = Self, M = Self> {
1050    fn mod_mul_assign(&mut self, other: RHS, m: M);
1051}
1052
1053/// Multiplies two numbers modulo a third number $m$. The inputs must be already reduced modulo $m$.
1054///
1055/// If multiple modular multiplications with the same modulus are necessary, it can be quicker to
1056/// precompute some piece of data and reuse it in the multiplication calls. This trait provides a
1057/// function for precomputing the data and a function for using it during multiplication.
1058pub trait ModMulPrecomputed<RHS = Self, M = Self> {
1059    type Output;
1060    type Data;
1061
1062    /// Precomputes some data to use for modular multiplication.
1063    fn precompute_mod_mul_data(m: &M) -> Self::Data;
1064
1065    fn mod_mul_precomputed(self, other: RHS, m: M, data: &Self::Data) -> Self::Output;
1066}
1067
1068/// Multiplies two numbers modulo a third number $m$, in place.The inputs must be already reduced
1069/// modulo $m$.
1070///
1071/// If multiple modular multiplications with the same modulus are necessary, it can be quicker to
1072/// precompute some piece of data and reuse it in the multiplication calls. This trait provides a
1073/// function for using precomputed data during multiplication. For precomputing the data, use the
1074/// [`precompute_mod_mul_data`](ModMulPrecomputed::precompute_mod_mul_data) function in
1075/// [`ModMulPrecomputed`].
1076pub trait ModMulPrecomputedAssign<RHS = Self, M = Self>: ModMulPrecomputed<RHS, M> {
1077    fn mod_mul_precomputed_assign(&mut self, other: RHS, m: M, data: &Self::Data);
1078}
1079
1080/// Negates a number modulo another number $m$. The input must be already reduced modulo $m$.
1081pub trait ModNeg<M = Self> {
1082    type Output;
1083
1084    fn mod_neg(self, m: M) -> Self::Output;
1085}
1086
1087/// Negates a number modulo another number $m$, in place. The input must be already reduced modulo
1088/// $m$.
1089pub trait ModNegAssign<M = Self> {
1090    fn mod_neg_assign(&mut self, m: M);
1091}
1092
1093/// Divides a number by another number, returning just the remainder. The remainder has the same
1094/// sign as the divisor (second number).
1095///
1096/// If the quotient were computed, the quotient and remainder would satisfy $x = qy + r$ and $0 \leq
1097/// |r| < |y|$.
1098pub trait Mod<RHS = Self> {
1099    type Output;
1100
1101    fn mod_op(self, other: RHS) -> Self::Output;
1102}
1103
1104/// Divides a number by another number, replacing the first number by the remainder. The remainder
1105/// has the same sign as the divisor (second number).
1106///
1107/// If the quotient were computed, the quotient and remainder would satisfy $x = qy + r$ and $0 \leq
1108/// |r| < |y|$.
1109pub trait ModAssign<RHS = Self> {
1110    fn mod_assign(&mut self, other: RHS);
1111}
1112
1113/// Divides a number by another number, returning the balanced remainder: the representative of the
1114/// first number modulo the second that is closest to zero.
1115///
1116/// The remainder $r$ satisfies $-|y|/2 < r \leq |y|/2$, so a remainder of exactly $|y|/2$ is
1117/// positive. It is congruent to $x$ modulo $y$, and those two properties determine it uniquely.
1118pub trait BalancedMod<RHS = Self> {
1119    type Output;
1120
1121    fn balanced_mod(self, other: RHS) -> Self::Output;
1122}
1123
1124/// Divides a number by another number, replacing the first number by the balanced remainder: the
1125/// representative of the first number modulo the second that is closest to zero.
1126///
1127/// The remainder $r$ satisfies $-|y|/2 < r \leq |y|/2$, so a remainder of exactly $|y|/2$ is
1128/// positive.
1129pub trait BalancedModAssign<RHS = Self> {
1130    fn balanced_mod_assign(&mut self, other: RHS);
1131}
1132
1133/// Divides a number by another number, returning just the remainder. The remainder is always
1134/// nonnegative.
1135///
1136/// If the quotient were computed, the quotient and remainder would satisfy $x = qy + r$ and $0 \leq
1137/// r < |y|$.
1138pub trait ModEuclidean<RHS = Self> {
1139    type Output;
1140
1141    fn mod_euclidean(self, other: RHS) -> Self::Output;
1142}
1143
1144/// Divides a number by another number, replacing the first number by the remainder. The remainder
1145/// is always nonnegative.
1146///
1147/// If the quotient were computed, the quotient and remainder would satisfy $x = qy + r$ and $0 \leq
1148/// r < |y|$.
1149pub trait ModEuclideanAssign<RHS = Self> {
1150    fn mod_euclidean_assign(&mut self, other: RHS);
1151}
1152
1153/// Divides the negative of a number by another number, returning the remainder.
1154///
1155/// If the quotient were computed, the quotient and remainder would satisfy $x = qy - r$ and $0 \leq
1156/// r < y$.
1157pub trait NegMod<RHS = Self> {
1158    type Output;
1159
1160    fn neg_mod(self, other: RHS) -> Self::Output;
1161}
1162
1163/// Divides the negative of a number by another number, replacing the first number by the remainder.
1164///
1165/// If the quotient were computed, the quotient and remainder would satisfy $x = qy - r$ and $0 \leq
1166/// r < y$.
1167pub trait NegModAssign<RHS = Self> {
1168    fn neg_mod_assign(&mut self, other: RHS);
1169}
1170
1171/// Divides a number by another number, returning just the remainder. The remainder has the opposite
1172/// sign as the divisor (second number).
1173///
1174/// If the quotient were computed, the quotient and remainder would satisfy $x = qy + r$ and $0 \leq
1175/// |r| < |y|$.
1176pub trait CeilingMod<RHS = Self> {
1177    type Output;
1178
1179    fn ceiling_mod(self, other: RHS) -> Self::Output;
1180}
1181
1182/// Divides a number by another number, replacing the first number by the remainder. The remainder
1183/// has the same sign as the divisor (second number).
1184///
1185/// If the quotient were computed, the quotient and remainder would satisfy $x = qy + r$ and $0 \leq
1186/// |r| < |y|$.
1187pub trait CeilingModAssign<RHS = Self> {
1188    fn ceiling_mod_assign(&mut self, other: RHS);
1189}
1190
1191/// Raises a number to a power modulo another number $m$. The base must be already reduced modulo
1192/// $m$.
1193pub trait ModPow<RHS = Self, M = Self> {
1194    type Output;
1195
1196    fn mod_pow(self, exp: RHS, m: M) -> Self::Output;
1197}
1198
1199/// Raises a number to a power modulo another number $m$, in place. The base must be already reduced
1200/// modulo $m$.
1201pub trait ModPowAssign<RHS = Self, M = Self> {
1202    fn mod_pow_assign(&mut self, exp: RHS, m: M);
1203}
1204
1205/// Raises a number to a power modulo another number $m$. The base must be already reduced modulo
1206/// $m$.
1207///
1208/// If multiple modular exponentiations with the same modulus are necessary, it can be quicker to
1209/// precompute some piece of data and reuse it in the exponentiation calls. This trait provides a
1210/// function for precomputing the data and a function for using it during exponentiation.
1211pub trait ModPowPrecomputed<RHS = Self, M = Self>
1212where
1213    Self: Sized,
1214{
1215    type Output;
1216    type Data;
1217
1218    /// Precomputes some data to use for modular exponentiation.
1219    fn precompute_mod_pow_data(m: &M) -> Self::Data;
1220
1221    fn mod_pow_precomputed(self, exp: RHS, m: M, data: &Self::Data) -> Self::Output;
1222}
1223
1224/// Raises a number to a power modulo another number $m$, in place. The base must be already reduced
1225/// modulo $m$.
1226///
1227/// If multiple modular exponentiations with the same modulus are necessary, it can be quicker to
1228/// precompute some piece of data and reuse it in the exponentiation calls. This trait provides a
1229/// function for using precomputed data during exponentiation. For precomputing the data, use the
1230/// [`precompute_mod_pow_data`](ModPowPrecomputed::precompute_mod_pow_data) function in
1231/// [`ModPowPrecomputed`].
1232pub trait ModPowPrecomputedAssign<RHS: Two = Self, M = Self>: ModPowPrecomputed<RHS, M> {
1233    fn mod_pow_precomputed_assign(&mut self, exp: RHS, m: M, data: &Self::Data);
1234}
1235
1236/// Adds two numbers modulo $2^k$. The inputs must be already reduced modulo $2^k$.
1237pub trait ModPowerOf2Add<RHS = Self> {
1238    type Output;
1239
1240    fn mod_power_of_2_add(self, other: RHS, pow: u64) -> Self::Output;
1241}
1242
1243/// Adds two numbers modulo $2^k$, in place. The inputs must be already reduced modulo $2^k$.
1244pub trait ModPowerOf2AddAssign<RHS = Self> {
1245    fn mod_power_of_2_add_assign(&mut self, other: RHS, pow: u64);
1246}
1247
1248/// Finds the multiplicative inverse of a number modulo $2^k$. The input must be already reduced
1249/// modulo $2^k$.
1250pub trait ModPowerOf2Inverse {
1251    type Output;
1252
1253    fn mod_power_of_2_inverse(self, pow: u64) -> Option<Self::Output>;
1254}
1255
1256/// Checks whether a number is reduced modulo $2^k$.
1257pub trait ModPowerOf2IsReduced {
1258    fn mod_power_of_2_is_reduced(&self, pow: u64) -> bool;
1259}
1260
1261/// Multiplies two numbers modulo $2^k$. The inputs must be already reduced modulo $2^k$.
1262pub trait ModPowerOf2Mul<RHS = Self> {
1263    type Output;
1264
1265    fn mod_power_of_2_mul(self, other: RHS, pow: u64) -> Self::Output;
1266}
1267
1268/// Multiplies two numbers modulo $2^k$, in place. The inputs must be already reduced modulo $2^k$.
1269pub trait ModPowerOf2MulAssign<RHS = Self> {
1270    fn mod_power_of_2_mul_assign(&mut self, other: RHS, pow: u64);
1271}
1272
1273/// Negates a number modulo $2^k$. The input must be already reduced modulo $2^k$.
1274pub trait ModPowerOf2Neg {
1275    type Output;
1276
1277    fn mod_power_of_2_neg(self, pow: u64) -> Self::Output;
1278}
1279
1280/// Negates a number modulo $2^k$ in place. The input must be already reduced modulo $2^k$.
1281pub trait ModPowerOf2NegAssign {
1282    fn mod_power_of_2_neg_assign(&mut self, pow: u64);
1283}
1284
1285/// Raises a number to a power modulo $2^k$. The base must be already reduced modulo $2^k$.
1286pub trait ModPowerOf2Pow<RHS = Self> {
1287    type Output;
1288
1289    fn mod_power_of_2_pow(self, exp: RHS, pow: u64) -> Self::Output;
1290}
1291
1292/// Raises a number to a power modulo $2^k$, in place. The base must be already reduced modulo
1293/// $2^k$.
1294pub trait ModPowerOf2PowAssign<RHS = Self> {
1295    fn mod_power_of_2_pow_assign(&mut self, exp: RHS, pow: u64);
1296}
1297
1298/// Left-shifts a number (multiplies it by a power of 2) modulo $2^k$. The number must be already
1299/// reduced modulo $2^k$.
1300pub trait ModPowerOf2Shl<RHS> {
1301    type Output;
1302
1303    fn mod_power_of_2_shl(self, other: RHS, pow: u64) -> Self::Output;
1304}
1305
1306/// Left-shifts a number (multiplies it by a power of 2) modulo $2^k$, in place. The number must be
1307/// already reduced modulo $2^k$.
1308pub trait ModPowerOf2ShlAssign<RHS> {
1309    fn mod_power_of_2_shl_assign(&mut self, other: RHS, pow: u64);
1310}
1311
1312/// Right-shifts a number (divides it by a power of 2) modulo $2^k$. The number must be already
1313/// reduced modulo $2^k$.
1314pub trait ModPowerOf2Shr<RHS> {
1315    type Output;
1316
1317    fn mod_power_of_2_shr(self, other: RHS, pow: u64) -> Self::Output;
1318}
1319
1320/// Right-shifts a number (divides it by a power of 2) modulo $2^k$, in place. The number must be
1321/// already reduced modulo $2^k$.
1322pub trait ModPowerOf2ShrAssign<RHS> {
1323    fn mod_power_of_2_shr_assign(&mut self, other: RHS, pow: u64);
1324}
1325
1326/// Squares a number modulo $2^k$. The input must be already reduced modulo $2^k$.
1327pub trait ModPowerOf2Square {
1328    type Output;
1329
1330    fn mod_power_of_2_square(self, pow: u64) -> Self::Output;
1331}
1332
1333/// Squares a number modulo $2^k$ in place. The input must be already reduced modulo $2^k$.
1334pub trait ModPowerOf2SquareAssign {
1335    fn mod_power_of_2_square_assign(&mut self, pow: u64);
1336}
1337
1338/// Subtracts two numbers modulo $2^k$. The inputs must be already reduced modulo $2^k$.
1339pub trait ModPowerOf2Sub<RHS = Self> {
1340    type Output;
1341
1342    fn mod_power_of_2_sub(self, other: RHS, pow: u64) -> Self::Output;
1343}
1344
1345/// Subtracts two numbers modulo $2^k$, in place. The inputs must be already reduced modulo $2^k$.
1346pub trait ModPowerOf2SubAssign<RHS = Self> {
1347    fn mod_power_of_2_sub_assign(&mut self, other: RHS, pow: u64);
1348}
1349
1350/// Divides a number by $2^k$, returning just the remainder. The remainder is non-negative.
1351///
1352/// If the quotient were computed, the quotient and remainder would satisfy $x = q2^k + r$ and $0
1353/// \leq r < 2^k$.
1354pub trait ModPowerOf2 {
1355    type Output;
1356
1357    fn mod_power_of_2(self, other: u64) -> Self::Output;
1358}
1359
1360/// Divides a number by $2^k$, replacing the number by the remainder. The remainder is non-negative.
1361///
1362/// If the quotient were computed, the quotient and remainder would satisfy $x = q2^k + r$ and $0
1363/// \leq r < 2^k$.
1364pub trait ModPowerOf2Assign {
1365    fn mod_power_of_2_assign(&mut self, other: u64);
1366}
1367
1368/// Divides a number by $2^k$, returning just the remainder. The remainder has the same sign as the
1369/// number.
1370///
1371/// If the quotient were computed, the quotient and remainder would satisfy $x = q2^k + r$ and $0
1372/// \leq |r| < 2^k$.
1373pub trait RemPowerOf2 {
1374    type Output;
1375
1376    fn rem_power_of_2(self, other: u64) -> Self::Output;
1377}
1378
1379/// Divides a number by $2^k$, replacing the number by the remainder. The remainder has the same
1380/// sign as the number.
1381///
1382/// If the quotient were computed, the quotient and remainder would satisfy $x = q2^k + r$ and $0
1383/// \leq |r| < 2^k$.
1384pub trait RemPowerOf2Assign {
1385    fn rem_power_of_2_assign(&mut self, other: u64);
1386}
1387
1388/// Divides the negative of a number by $2^k$, returning the remainder.
1389///
1390/// If the quotient were computed, the quotient and remainder would satisfy $x = q2^k - r$ and $0
1391/// \leq r < 2^k$.
1392pub trait NegModPowerOf2 {
1393    type Output;
1394
1395    fn neg_mod_power_of_2(self, other: u64) -> Self::Output;
1396}
1397
1398/// Divides the negative of a number by $2^k$, replacing the number by the remainder.
1399///
1400/// If the quotient were computed, the quotient and remainder would satisfy $x = q2^k - r$ and $0
1401/// \leq r < 2^k$.
1402pub trait NegModPowerOf2Assign {
1403    fn neg_mod_power_of_2_assign(&mut self, other: u64);
1404}
1405
1406/// Divides a number by $2^k$, returning just the remainder. The remainder is non-positive.
1407///
1408/// If the quotient were computed, the quotient and remainder would satisfy $x = q2^k + r$ and $0
1409/// \leq -r < 2^k$.
1410pub trait CeilingModPowerOf2 {
1411    type Output;
1412
1413    fn ceiling_mod_power_of_2(self, other: u64) -> Self::Output;
1414}
1415
1416/// Divides a number by $2^k$, replacing the number by the remainder. The remainder is non-positive.
1417///
1418/// If the quotient were computed, the quotient and remainder would satisfy $x = q2^k + r$ and $0
1419/// \leq -r < 2^k$.
1420pub trait CeilingModPowerOf2Assign {
1421    fn ceiling_mod_power_of_2_assign(&mut self, other: u64);
1422}
1423
1424/// Left-shifts a number (multiplies it by a power of 2) modulo another number $m$. The number must
1425/// be already reduced modulo $m$.
1426pub trait ModShl<RHS, M = Self> {
1427    type Output;
1428
1429    fn mod_shl(self, other: RHS, m: M) -> Self::Output;
1430}
1431
1432/// Left-shifts a number (multiplies it by a power of 2) modulo another number $m$, in place. The
1433/// number must be already reduced modulo $m$.
1434pub trait ModShlAssign<RHS, M = Self> {
1435    fn mod_shl_assign(&mut self, other: RHS, m: M);
1436}
1437
1438/// Left-shifts a number (divides it by a power of 2) modulo another number $m$. The number must be
1439/// already reduced modulo $m$.
1440pub trait ModShr<RHS, M = Self> {
1441    type Output;
1442
1443    fn mod_shr(self, other: RHS, m: M) -> Self::Output;
1444}
1445
1446/// Left-shifts a number (divides it by a power of 2) modulo another number $m$, in place. The
1447/// number must be already reduced modulo $m$.
1448pub trait ModShrAssign<RHS, M = Self> {
1449    fn mod_shr_assign(&mut self, other: RHS, m: M);
1450}
1451
1452/// Computes a square root of a number modulo another number $m$, returning `None` if no root is
1453/// found. The input must be already reduced modulo $m$.
1454///
1455/// The modulus should be an odd prime: for such moduli a root is found whenever one exists. The
1456/// behavior for other moduli is deterministic and never hangs, but a root may be missed, and a
1457/// returned value may fail to be a root.
1458pub trait ModSqrt<M = Self> {
1459    type Output;
1460
1461    fn mod_sqrt(self, m: M) -> Option<Self::Output>;
1462}
1463
1464/// Squares a number modulo another number $m$. The input must be already reduced modulo $m$.
1465pub trait ModSquare<M = Self> {
1466    type Output;
1467
1468    fn mod_square(self, m: M) -> Self::Output;
1469}
1470
1471/// Squares a number modulo another number $m$, in place. The input must be already reduced modulo
1472/// $m$.
1473pub trait ModSquareAssign<M = Self> {
1474    fn mod_square_assign(&mut self, m: M);
1475}
1476
1477/// Squares a number modulo another number $m$. The input must be already reduced modulo $m$.
1478///
1479/// If multiple modular squarings with the same modulus are necessary, it can be quicker to
1480/// precompute some piece of data using
1481/// [`precompute_mod_pow_data`](ModPowPrecomputed::precompute_mod_pow_data) function in
1482/// [`ModMulPrecomputed`] and reuse it in the squaring calls.
1483pub trait ModSquarePrecomputed<RHS = Self, M = Self>: ModPowPrecomputed<RHS, M>
1484where
1485    Self: Sized,
1486{
1487    fn mod_square_precomputed(self, m: M, data: &Self::Data) -> Self::Output;
1488}
1489
1490/// Squares a number modulo another number $m$, in place. The input must be already reduced modulo
1491/// $m$.
1492///
1493/// If multiple modular squarings with the same modulus are necessary, it can be quicker to
1494/// precompute some piece of data using
1495/// [`precompute_mod_pow_data`](ModPowPrecomputed::precompute_mod_pow_data) function in
1496/// [`ModMulPrecomputed`] and reuse it in the squaring calls.
1497pub trait ModSquarePrecomputedAssign<RHS = Self, M = Self>: ModPowPrecomputed<RHS, M> {
1498    fn mod_square_precomputed_assign(&mut self, m: M, data: &Self::Data);
1499}
1500
1501/// Adds two numbers modulo a third number $m$. The inputs must be already reduced modulo $m$.
1502pub trait ModSub<RHS = Self, M = Self> {
1503    type Output;
1504
1505    fn mod_sub(self, other: RHS, m: M) -> Self::Output;
1506}
1507
1508/// Adds two numbers modulo a third number $m$, in place. The inputs must be already reduced modulo
1509/// $m$.
1510pub trait ModSubAssign<RHS = Self, M = Self> {
1511    fn mod_sub_assign(&mut self, other: RHS, m: M);
1512}
1513
1514/// Replaces a number with its negative. Assumes the result is representable.
1515pub trait NegAssign {
1516    fn neg_assign(&mut self);
1517}
1518
1519/// Returns the smallest power of 2 greater than or equal to a number. Assumes the result is
1520/// representable.
1521pub trait NextPowerOf2 {
1522    type Output;
1523
1524    fn next_power_of_2(self) -> Self::Output;
1525}
1526
1527/// Replaces a number with the smallest power of 2 greater than or equal it. Assumes the result is
1528/// representable.
1529pub trait NextPowerOf2Assign {
1530    fn next_power_of_2_assign(&mut self);
1531}
1532
1533/// Takes the absolute value of a number.
1534///
1535/// Returns a tuple of the result along with a boolean indicating whether an arithmetic overflow
1536/// occurred. If an overflow occurred, then the wrapped number is returned.
1537pub trait OverflowingAbs {
1538    type Output;
1539
1540    fn overflowing_abs(self) -> (Self::Output, bool);
1541}
1542
1543/// Replaces a number with its absolute value.
1544///
1545/// Returns a boolean indicating whether an arithmetic overflow occurred. If an overflow occurred,
1546/// then the wrapped number is assigned.
1547pub trait OverflowingAbsAssign {
1548    fn overflowing_abs_assign(&mut self) -> bool;
1549}
1550
1551/// Adds two numbers.
1552///
1553/// Returns a tuple of the sum along with a boolean indicating whether an arithmetic overflow
1554/// occurred. If an overflow occurred, then the wrapped number is returned.
1555pub trait OverflowingAdd<RHS = Self> {
1556    type Output;
1557
1558    fn overflowing_add(self, other: RHS) -> (Self::Output, bool);
1559}
1560
1561/// Adds a number to another number in place.
1562///
1563/// Returns a boolean indicating whether an arithmetic overflow occurred. If an overflow occurred,
1564/// then the wrapped number is assigned.
1565pub trait OverflowingAddAssign<RHS = Self> {
1566    fn overflowing_add_assign(&mut self, other: RHS) -> bool;
1567}
1568
1569/// Adds a number and the product of two other numbers.
1570///
1571/// Returns a tuple of the result along with a boolean indicating whether an arithmetic overflow
1572/// occurred. If an overflow occurred, then the wrapped number is returned.
1573pub trait OverflowingAddMul<Y = Self, Z = Self> {
1574    type Output;
1575
1576    fn overflowing_add_mul(self, y: Y, z: Z) -> (Self::Output, bool);
1577}
1578
1579/// Adds a number and the product of two other numbers, in place.
1580///
1581/// Returns a tuple of the result along with a boolean indicating whether an arithmetic overflow
1582/// occurred. If an overflow occurred, then the wrapped number is returned.
1583pub trait OverflowingAddMulAssign<Y = Self, Z = Self> {
1584    fn overflowing_add_mul_assign(&mut self, y: Y, z: Z) -> bool;
1585}
1586
1587/// Adds the products of two pairs of numbers.
1588///
1589/// Returns a tuple of the result along with a boolean indicating whether an arithmetic overflow
1590/// occurred. If an overflow occurred, then the wrapped result is returned.
1591pub trait OverflowingMulAddMul<Y = Self, Z = Self, W = Self> {
1592    type Output;
1593
1594    fn overflowing_mul_add_mul(self, y: Y, z: Z, w: W) -> (Self::Output, bool);
1595}
1596
1597/// Adds the products of two pairs of numbers, in place.
1598///
1599/// Returns a boolean indicating whether an arithmetic overflow occurred. If an overflow occurred,
1600/// then the wrapped result is assigned.
1601pub trait OverflowingMulAddMulAssign<Y = Self, Z = Self, W = Self> {
1602    fn overflowing_mul_add_mul_assign(&mut self, y: Y, z: Z, w: W) -> bool;
1603}
1604
1605/// Subtracts the product of one pair of numbers from the product of another.
1606///
1607/// Returns a tuple of the result along with a boolean indicating whether an arithmetic overflow
1608/// occurred. If an overflow occurred, then the wrapped result is returned.
1609pub trait OverflowingMulSubMul<Y = Self, Z = Self, W = Self> {
1610    type Output;
1611
1612    fn overflowing_mul_sub_mul(self, y: Y, z: Z, w: W) -> (Self::Output, bool);
1613}
1614
1615/// Subtracts the product of one pair of numbers from the product of another, in place.
1616///
1617/// Returns a boolean indicating whether an arithmetic overflow occurred. If an overflow occurred,
1618/// then the wrapped result is assigned.
1619pub trait OverflowingMulSubMulAssign<Y = Self, Z = Self, W = Self> {
1620    fn overflowing_mul_sub_mul_assign(&mut self, y: Y, z: Z, w: W) -> bool;
1621}
1622
1623/// Divides two numbers.
1624///
1625/// Returns a tuple of the sum along with a boolean indicating whether an arithmetic overflow
1626/// occurred. If an overflow occurred, then the wrapped number is returned.
1627pub trait OverflowingDiv<RHS = Self> {
1628    type Output;
1629
1630    fn overflowing_div(self, other: RHS) -> (Self::Output, bool);
1631}
1632
1633/// Divides a number by another number in place.
1634///
1635/// Returns a boolean indicating whether an arithmetic overflow occurred. If an overflow occurred,
1636/// then the wrapped number is assigned.
1637pub trait OverflowingDivAssign<RHS = Self> {
1638    fn overflowing_div_assign(&mut self, other: RHS) -> bool;
1639}
1640
1641/// Multiplies two numbers.
1642///
1643/// Returns a tuple of the sum along with a boolean indicating whether an arithmetic overflow
1644/// occurred. If an overflow occurred, then the wrapped number is returned.
1645pub trait OverflowingMul<RHS = Self> {
1646    type Output;
1647
1648    fn overflowing_mul(self, other: RHS) -> (Self::Output, bool);
1649}
1650
1651/// Multiplies a number by another number in place.
1652///
1653/// Returns a boolean indicating whether an arithmetic overflow occurred. If an overflow occurred,
1654/// then the wrapped number is assigned.
1655pub trait OverflowingMulAssign<RHS = Self> {
1656    fn overflowing_mul_assign(&mut self, other: RHS) -> bool;
1657}
1658
1659/// Negates a number.
1660///
1661/// Returns a tuple of the sum along with a boolean indicating whether an arithmetic overflow
1662/// occurred. If an overflow occurred, then the wrapped number is returned.
1663pub trait OverflowingNeg {
1664    type Output;
1665
1666    fn overflowing_neg(self) -> (Self::Output, bool);
1667}
1668
1669/// Negates a number in place.
1670///
1671/// Returns a boolean indicating whether an arithmetic overflow occurred. If an overflow occurred,
1672/// then the wrapped number is assigned.
1673pub trait OverflowingNegAssign {
1674    fn overflowing_neg_assign(&mut self) -> bool;
1675}
1676
1677/// Raises a number to a power.
1678///
1679/// Returns a tuple of the sum along with a boolean indicating whether an arithmetic overflow
1680/// occurred. If an overflow occurred, then the wrapped number is returned.
1681pub trait OverflowingPow<RHS> {
1682    type Output;
1683
1684    fn overflowing_pow(self, exp: RHS) -> (Self::Output, bool);
1685}
1686
1687/// Raises a number to a power in place.
1688///
1689/// Returns a boolean indicating whether an arithmetic overflow occurred. If an overflow occurred,
1690/// then the wrapped number is assigned.
1691pub trait OverflowingPowAssign<RHS = Self> {
1692    fn overflowing_pow_assign(&mut self, exp: RHS) -> bool;
1693}
1694
1695/// Squares a number.
1696///
1697/// Returns a tuple of the sum along with a boolean indicating whether an arithmetic overflow
1698/// occurred. If an overflow occurred, then the wrapped number is returned.
1699pub trait OverflowingSquare {
1700    type Output;
1701
1702    fn overflowing_square(self) -> (Self::Output, bool);
1703}
1704
1705/// Squares a number in place.
1706///
1707/// Returns a boolean indicating whether an arithmetic overflow occurred. If an overflow occurred,
1708/// then the wrapped number is assigned.
1709pub trait OverflowingSquareAssign {
1710    fn overflowing_square_assign(&mut self) -> bool;
1711}
1712
1713/// Subtracts two numbers.
1714///
1715/// Returns a tuple of the sum along with a boolean indicating whether an arithmetic overflow
1716/// occurred. If an overflow occurred, then the wrapped number is returned.
1717pub trait OverflowingSub<RHS = Self> {
1718    type Output;
1719
1720    fn overflowing_sub(self, other: RHS) -> (Self::Output, bool);
1721}
1722
1723/// Subtracts a number by another number in place.
1724///
1725/// Returns a boolean indicating whether an arithmetic overflow occurred. If an overflow occurred,
1726/// then the wrapped number is assigned.
1727pub trait OverflowingSubAssign<RHS = Self> {
1728    fn overflowing_sub_assign(&mut self, other: RHS) -> bool;
1729}
1730
1731/// Subtracts a number by the product of two other numbers.
1732///
1733/// Returns a tuple of the result along with a boolean indicating whether an arithmetic overflow
1734/// occurred. If an overflow occurred, then the wrapped number is returned.
1735pub trait OverflowingSubMul<Y = Self, Z = Self> {
1736    type Output;
1737
1738    fn overflowing_sub_mul(self, y: Y, z: Z) -> (Self::Output, bool);
1739}
1740
1741/// Subtracts a number by the product of two other numbers, in place.
1742///
1743/// Returns a tuple of the result along with a boolean indicating whether an arithmetic overflow
1744/// occurred. If an overflow occurred, then the wrapped number is returned.
1745pub trait OverflowingSubMulAssign<Y = Self, Z = Self> {
1746    fn overflowing_sub_mul_assign(&mut self, y: Y, z: Z) -> bool;
1747}
1748
1749/// Determines whether a number is even or odd.
1750pub trait Parity {
1751    /// Determines whether a number is even.
1752    fn even(self) -> bool;
1753
1754    /// Determines whether a number is odd.
1755    fn odd(self) -> bool;
1756}
1757
1758/// Raises a number to a power. Assumes the result is representable.
1759pub trait Pow<RHS> {
1760    type Output;
1761
1762    fn pow(self, exp: RHS) -> Self::Output;
1763}
1764
1765/// Raises a number to a power in place. Assumes the result is representable.
1766pub trait PowAssign<RHS = Self> {
1767    fn pow_assign(&mut self, exp: RHS);
1768}
1769
1770/// Raises 2 to a power.
1771pub trait PowerOf2<POW> {
1772    fn power_of_2(pow: POW) -> Self;
1773}
1774
1775/// Replaces a number with 2 raised to the power of that number.
1776pub trait PowerOf2Assign {
1777    fn power_of_2_assign(&mut self);
1778}
1779
1780/// Raises 10 to a power.
1781pub trait PowerOf10<POW> {
1782    fn power_of_10(pow: POW) -> Self;
1783}
1784
1785/// Replaces a number with 10 raised to the power of that number.
1786pub trait PowerOf10Assign {
1787    fn power_of_10_assign(&mut self);
1788}
1789
1790pub trait Primorial {
1791    fn primorial(n: u64) -> Self;
1792
1793    fn product_of_first_n_primes(n: u64) -> Self;
1794}
1795
1796pub trait CheckedPrimorial: Sized {
1797    fn checked_primorial(n: u64) -> Option<Self>;
1798
1799    fn checked_product_of_first_n_primes(n: u64) -> Option<Self>;
1800}
1801
1802/// Finds the reciprocal (multiplicative inverse) of a number.
1803pub trait Reciprocal {
1804    type Output;
1805
1806    fn reciprocal(self) -> Self::Output;
1807}
1808
1809/// Replaces a number with its reciprocal (multiplicative inverse).
1810pub trait ReciprocalAssign {
1811    fn reciprocal_assign(&mut self);
1812}
1813
1814/// Takes the reciprocal of the square root of a number.
1815pub trait ReciprocalSqrt {
1816    type Output;
1817
1818    fn reciprocal_sqrt(self) -> Self::Output;
1819}
1820
1821/// Replaces a number with the reciprocal of its square root.
1822pub trait ReciprocalSqrtAssign {
1823    fn reciprocal_sqrt_assign(&mut self);
1824}
1825
1826/// Finds the floor of the $n$th root of a number.
1827pub trait FloorRoot<POW> {
1828    type Output;
1829
1830    fn floor_root(self, pow: POW) -> Self::Output;
1831}
1832
1833/// Replaces a number with the floor of its $n$th root.
1834pub trait FloorRootAssign<POW> {
1835    fn floor_root_assign(&mut self, pow: POW);
1836}
1837
1838/// Finds the ceiling of the $n$th root of a number.
1839pub trait CeilingRoot<POW> {
1840    type Output;
1841
1842    fn ceiling_root(self, pow: POW) -> Self::Output;
1843}
1844
1845/// Replaces a number with the ceiling of its $n$th root.
1846pub trait CeilingRootAssign<POW> {
1847    fn ceiling_root_assign(&mut self, pow: POW);
1848}
1849
1850/// Finds the $n$th root of a number, returning `None` if it is not a perfect $n$th power.
1851pub trait CheckedRoot<POW> {
1852    type Output;
1853
1854    fn checked_root(self, pow: POW) -> Option<Self::Output>;
1855}
1856
1857/// Finds the floor of the $n$th root of a number, returning both the root and the remainder.
1858pub trait RootRem<POW> {
1859    type RootOutput;
1860    type RemOutput;
1861
1862    fn root_rem(self, exp: POW) -> (Self::RootOutput, Self::RemOutput);
1863}
1864
1865/// Replaces a number with the floor of its $n$th root, returning the remainder.
1866pub trait RootAssignRem<POW> {
1867    type RemOutput;
1868
1869    fn root_assign_rem(&mut self, exp: POW) -> Self::RemOutput;
1870}
1871
1872/// Takes the $n$th root of a number.
1873pub trait Root<POW> {
1874    type Output;
1875
1876    fn root(self, pow: POW) -> Self::Output;
1877}
1878
1879/// Replaces a number with its $n$th root.
1880pub trait RootAssign<POW> {
1881    fn root_assign(&mut self, pow: POW);
1882}
1883
1884/// Takes the cube root of a number.
1885pub trait Cbrt {
1886    type Output;
1887
1888    fn cbrt(self) -> Self::Output;
1889}
1890
1891/// Replaces a number with its cube root.
1892pub trait CbrtAssign {
1893    fn cbrt_assign(&mut self);
1894}
1895
1896/// Rotates a number left, inserting the leftmost bits into the right end.
1897pub trait RotateLeft {
1898    type Output;
1899
1900    fn rotate_left(self, n: u64) -> Self::Output;
1901}
1902
1903/// Rotates a number left, inserting the leftmost bits into the right end, in place.
1904pub trait RotateLeftAssign {
1905    fn rotate_left_assign(&mut self, n: u64);
1906}
1907
1908/// Rotates a number right, inserting the leftmost bits into the left end.
1909pub trait RotateRight {
1910    type Output;
1911
1912    fn rotate_right(self, n: u64) -> Self::Output;
1913}
1914
1915/// Rotates a number right, inserting the leftmost bits into the left end, in place.
1916pub trait RotateRightAssign {
1917    fn rotate_right_assign(&mut self, n: u64);
1918}
1919
1920/// Rounds a number to a multiple of another number, according to a specified rounding mode. An
1921/// [`Ordering`] is also returned, indicating whether the returned value is less than, equal to, or
1922/// greater than the original value.
1923pub trait RoundToMultiple<RHS = Self> {
1924    type Output;
1925
1926    fn round_to_multiple(self, other: RHS, rm: RoundingMode) -> (Self::Output, Ordering);
1927}
1928
1929/// Rounds a number to a multiple of another number in place, according to a specified rounding
1930/// mode. [`Ordering`] is returned, indicating whether the returned value is less than, equal to, or
1931/// greater than the original value.
1932pub trait RoundToMultipleAssign<RHS = Self> {
1933    fn round_to_multiple_assign(&mut self, other: RHS, rm: RoundingMode) -> Ordering;
1934}
1935
1936/// Rounds a number to a multiple of $2^k$, according to a specified rounding mode. An [`Ordering`]
1937/// is also returned, indicating whether the returned value is less than, equal to, or greater than
1938/// the original value.
1939pub trait RoundToMultipleOfPowerOf2<RHS> {
1940    type Output;
1941
1942    fn round_to_multiple_of_power_of_2(
1943        self,
1944        pow: RHS,
1945        rm: RoundingMode,
1946    ) -> (Self::Output, Ordering);
1947}
1948
1949/// Rounds a number to a multiple of $2^k$ in place, according to a specified rounding mode. An
1950/// [`Ordering`] is returned, indicating whether the returned value is less than, equal to, or
1951/// greater than the original value.
1952pub trait RoundToMultipleOfPowerOf2Assign<RHS> {
1953    fn round_to_multiple_of_power_of_2_assign(&mut self, pow: RHS, rm: RoundingMode) -> Ordering;
1954}
1955
1956/// Takes the absolute value of a number, saturating at the numeric bounds instead of overflowing.
1957pub trait SaturatingAbs {
1958    type Output;
1959
1960    fn saturating_abs(self) -> Self::Output;
1961}
1962
1963/// Replaces a number with its absolute value, saturating at the numeric bounds instead of
1964/// overflowing.
1965pub trait SaturatingAbsAssign {
1966    fn saturating_abs_assign(&mut self);
1967}
1968
1969/// Adds two numbers, saturating at the numeric bounds instead of overflowing.
1970pub trait SaturatingAdd<RHS = Self> {
1971    type Output;
1972
1973    fn saturating_add(self, other: RHS) -> Self::Output;
1974}
1975
1976/// Add a number to another number in place, saturating at the numeric bounds instead of
1977/// overflowing.
1978pub trait SaturatingAddAssign<RHS = Self> {
1979    fn saturating_add_assign(&mut self, other: RHS);
1980}
1981
1982/// Adds a number and the product of two other numbers, saturating at the numeric bounds instead of
1983/// overflowing.
1984pub trait SaturatingAddMul<Y = Self, Z = Self> {
1985    type Output;
1986
1987    fn saturating_add_mul(self, y: Y, z: Z) -> Self::Output;
1988}
1989
1990/// Adds a number and the product of two other numbers in place, saturating at the numeric bounds
1991/// instead of overflowing.
1992pub trait SaturatingAddMulAssign<Y = Self, Z = Self> {
1993    fn saturating_add_mul_assign(&mut self, y: Y, z: Z);
1994}
1995
1996/// Adds the products of two pairs of numbers, saturating at the numeric bounds instead of
1997/// overflowing.
1998pub trait SaturatingMulAddMul<Y = Self, Z = Self, W = Self> {
1999    type Output;
2000
2001    fn saturating_mul_add_mul(self, y: Y, z: Z, w: W) -> Self::Output;
2002}
2003
2004/// Adds the products of two pairs of numbers, in place, saturating at the numeric bounds instead of
2005/// overflowing.
2006pub trait SaturatingMulAddMulAssign<Y = Self, Z = Self, W = Self> {
2007    fn saturating_mul_add_mul_assign(&mut self, y: Y, z: Z, w: W);
2008}
2009
2010/// Subtracts the product of one pair of numbers from the product of another, saturating at the
2011/// numeric bounds instead of overflowing.
2012pub trait SaturatingMulSubMul<Y = Self, Z = Self, W = Self> {
2013    type Output;
2014
2015    fn saturating_mul_sub_mul(self, y: Y, z: Z, w: W) -> Self::Output;
2016}
2017
2018/// Subtracts the product of one pair of numbers from the product of another, in place, saturating
2019/// at the numeric bounds instead of overflowing.
2020pub trait SaturatingMulSubMulAssign<Y = Self, Z = Self, W = Self> {
2021    fn saturating_mul_sub_mul_assign(&mut self, y: Y, z: Z, w: W);
2022}
2023
2024/// Multiplies two numbers, saturating at the numeric bounds instead of overflowing.
2025pub trait SaturatingMul<RHS = Self> {
2026    type Output;
2027
2028    fn saturating_mul(self, other: RHS) -> Self::Output;
2029}
2030
2031/// Multiplies a number by another number in place, saturating at the numeric bounds instead of
2032/// overflowing.
2033pub trait SaturatingMulAssign<RHS = Self> {
2034    fn saturating_mul_assign(&mut self, other: RHS);
2035}
2036
2037/// Negates a number, saturating at the numeric bounds instead of overflowing.
2038pub trait SaturatingNeg {
2039    type Output;
2040
2041    fn saturating_neg(self) -> Self::Output;
2042}
2043
2044/// Negates a number in place, saturating at the numeric bounds instead of overflowing.
2045pub trait SaturatingNegAssign {
2046    fn saturating_neg_assign(&mut self);
2047}
2048
2049/// Raises a number to a power, saturating at the numeric bounds instead of overflowing.
2050pub trait SaturatingPow<RHS> {
2051    type Output;
2052
2053    fn saturating_pow(self, exp: RHS) -> Self::Output;
2054}
2055
2056/// Raises a number to a power in place, saturating at the numeric bounds instead of overflowing.
2057pub trait SaturatingPowAssign<RHS = Self> {
2058    fn saturating_pow_assign(&mut self, exp: RHS);
2059}
2060
2061/// Squares a number, saturating at the numeric bounds instead of overflowing.
2062pub trait SaturatingSquare {
2063    type Output;
2064
2065    fn saturating_square(self) -> Self::Output;
2066}
2067
2068/// Squares a number in place, saturating at the numeric bounds instead of overflowing.
2069pub trait SaturatingSquareAssign {
2070    fn saturating_square_assign(&mut self);
2071}
2072
2073/// Subtracts two numbers, saturating at the numeric bounds instead of overflowing.
2074pub trait SaturatingSub<RHS = Self> {
2075    type Output;
2076
2077    fn saturating_sub(self, other: RHS) -> Self::Output;
2078}
2079
2080/// Subtracts a number by another number in place, saturating at the numeric bounds instead of
2081/// overflowing.
2082pub trait SaturatingSubAssign<RHS = Self> {
2083    fn saturating_sub_assign(&mut self, other: RHS);
2084}
2085
2086/// Subtracts a number by the product of two other numbers, saturating at the numeric bounds instead
2087/// of overflowing.
2088pub trait SaturatingSubMul<Y = Self, Z = Self> {
2089    type Output;
2090
2091    fn saturating_sub_mul(self, y: Y, z: Z) -> Self::Output;
2092}
2093
2094/// Subtracts a number by the product of two other numbers in place, saturating at the numeric
2095/// bounds instead of overflowing.
2096pub trait SaturatingSubMulAssign<Y = Self, Z = Self> {
2097    fn saturating_sub_mul_assign(&mut self, y: Y, z: Z);
2098}
2099
2100/// Left-shifts a number (multiplies it by a power of 2), rounding the result according to a
2101/// specified rounding mode. An [`Ordering`] is also returned, indicating whether the returned value
2102/// is less than, equal to, or greater than the exact value.
2103///
2104/// Rounding might only be necessary if `other` is negative.
2105pub trait ShlRound<RHS> {
2106    type Output;
2107
2108    fn shl_round(self, other: RHS, rm: RoundingMode) -> (Self::Output, Ordering);
2109}
2110
2111/// Left-shifts a number (multiplies it by a power of 2) in place, rounding the result according to
2112/// a specified rounding mode. An [`Ordering`] is also returned, indicating whether the assigned
2113/// value is less than, equal to, or greater than the exact value.
2114///
2115/// Rounding might only be necessary if `other` is negative.
2116pub trait ShlRoundAssign<RHS> {
2117    fn shl_round_assign(&mut self, other: RHS, rm: RoundingMode) -> Ordering;
2118}
2119
2120/// Right-shifts a number (divides it by a power of 2), rounding the result according to a specified
2121/// rounding mode. An [`Ordering`] is also returned, indicating whether the returned value is less
2122/// than, equal to, or greater than the exact value.
2123///
2124/// Rounding might only be necessary if `other` is positive.
2125pub trait ShrRound<RHS> {
2126    type Output;
2127
2128    fn shr_round(self, other: RHS, rm: RoundingMode) -> (Self::Output, Ordering);
2129}
2130
2131/// Right-shifts a number (divides it by a power of 2) in place, rounding the result according to a
2132/// specified rounding mode. An [`Ordering`] is also returned, indicating whether the assigned value
2133/// is less than, equal to, or greater than the exact value.
2134///
2135/// Rounding might only be necessary if `other` is positive.
2136pub trait ShrRoundAssign<RHS> {
2137    fn shr_round_assign(&mut self, other: RHS, rm: RoundingMode) -> Ordering;
2138}
2139
2140/// Returns `Greater`, `Equal`, or `Less`, depending on whether a number is positive, zero, or
2141/// negative, respectively.
2142pub trait Sign {
2143    fn sign(&self) -> Ordering;
2144}
2145
2146/// Takes the square root of a number.
2147pub trait Sqrt {
2148    type Output;
2149
2150    fn sqrt(self) -> Self::Output;
2151}
2152
2153/// Replaces a number with its square root.
2154pub trait SqrtAssign {
2155    fn sqrt_assign(&mut self);
2156}
2157
2158/// Finds the floor of the square root of a number.
2159pub trait FloorSqrt {
2160    type Output;
2161
2162    fn floor_sqrt(self) -> Self::Output;
2163}
2164
2165/// Replaces a number with the floor of its square root.
2166pub trait FloorSqrtAssign {
2167    fn floor_sqrt_assign(&mut self);
2168}
2169
2170/// Finds the ceiling of the square root of a number.
2171pub trait CeilingSqrt {
2172    type Output;
2173
2174    fn ceiling_sqrt(self) -> Self::Output;
2175}
2176
2177/// Replaces a number with the ceiling of its square root.
2178pub trait CeilingSqrtAssign {
2179    fn ceiling_sqrt_assign(&mut self);
2180}
2181
2182/// Finds the square root of a number, returning `None` if it is not a perfect square.
2183pub trait CheckedSqrt {
2184    type Output;
2185
2186    fn checked_sqrt(self) -> Option<Self::Output>;
2187}
2188
2189/// Finds the floor of the square root of a number, returning both the root and the remainder.
2190pub trait SqrtRem {
2191    type SqrtOutput;
2192    type RemOutput;
2193
2194    fn sqrt_rem(self) -> (Self::SqrtOutput, Self::RemOutput);
2195}
2196
2197/// Replaces a number with the floor of its square root, returning the remainder.
2198pub trait SqrtAssignRem {
2199    type RemOutput;
2200
2201    fn sqrt_assign_rem(&mut self) -> Self::RemOutput;
2202}
2203
2204/// Squares a number.
2205pub trait Square {
2206    type Output;
2207
2208    fn square(self) -> Self::Output;
2209}
2210
2211/// Replaces a number with its square.
2212pub trait SquareAssign {
2213    fn square_assign(&mut self);
2214}
2215
2216/// Subtracts a number by the product of two other numbers.
2217///
2218/// Depending on the implementing type, the fused operation may compute the same value as the
2219/// unfused `self - y * z` more efficiently; or, for types with rounding, it may compute a *more
2220/// accurate* value -- the product enters the subtraction exactly, with a single rounding at the end
2221/// -- but *less* efficiently, since the exact product must be computed in full. See each
2222/// implementation's documentation for which contract it provides.
2223pub trait SubMul<Y = Self, Z = Self> {
2224    type Output;
2225
2226    fn sub_mul(self, y: Y, z: Z) -> Self::Output;
2227}
2228
2229/// Subtracts a number by the product of two other numbers, in place.
2230///
2231/// Depending on the implementing type, the fused operation may compute the same value as the
2232/// unfused `*self - y * z` more efficiently; or, for types with rounding, it may compute a *more
2233/// accurate* value -- the product enters the subtraction exactly, with a single rounding at the end
2234/// -- but *less* efficiently, since the exact product must be computed in full. See each
2235/// implementation's documentation for which contract it provides.
2236pub trait SubMulAssign<Y = Self, Z = Self> {
2237    fn sub_mul_assign(&mut self, y: Y, z: Z);
2238}
2239
2240/// Takes the absolute value of a number, wrapping around at the boundary of the type.
2241pub trait WrappingAbs {
2242    type Output;
2243
2244    fn wrapping_abs(self) -> Self::Output;
2245}
2246
2247/// Replaces a number with its absolute value, wrapping around at the boundary of the type.
2248pub trait WrappingAbsAssign {
2249    fn wrapping_abs_assign(&mut self);
2250}
2251
2252/// Adds two numbers, wrapping around at the boundary of the type.
2253pub trait WrappingAdd<RHS = Self> {
2254    type Output;
2255
2256    fn wrapping_add(self, other: RHS) -> Self::Output;
2257}
2258
2259/// Adds a number to another number in place, wrapping around at the boundary of the type.
2260pub trait WrappingAddAssign<RHS = Self> {
2261    fn wrapping_add_assign(&mut self, other: RHS);
2262}
2263
2264/// Adds a number and the product of two other numbers, wrapping around at the boundary of the type.
2265pub trait WrappingAddMul<Y = Self, Z = Self> {
2266    type Output;
2267
2268    fn wrapping_add_mul(self, y: Y, z: Z) -> Self::Output;
2269}
2270
2271/// Adds a number and the product of two other numbers, in place, wrapping around at the boundary of
2272/// the type.
2273pub trait WrappingAddMulAssign<Y = Self, Z = Self> {
2274    fn wrapping_add_mul_assign(&mut self, y: Y, z: Z);
2275}
2276
2277/// Adds the products of two pairs of numbers, wrapping around at the boundary of the type.
2278pub trait WrappingMulAddMul<Y = Self, Z = Self, W = Self> {
2279    type Output;
2280
2281    fn wrapping_mul_add_mul(self, y: Y, z: Z, w: W) -> Self::Output;
2282}
2283
2284/// Adds the products of two pairs of numbers, in place, wrapping around at the boundary of the
2285/// type.
2286pub trait WrappingMulAddMulAssign<Y = Self, Z = Self, W = Self> {
2287    fn wrapping_mul_add_mul_assign(&mut self, y: Y, z: Z, w: W);
2288}
2289
2290/// Subtracts the product of one pair of numbers from the product of another, wrapping around at the
2291/// boundary of the type.
2292pub trait WrappingMulSubMul<Y = Self, Z = Self, W = Self> {
2293    type Output;
2294
2295    fn wrapping_mul_sub_mul(self, y: Y, z: Z, w: W) -> Self::Output;
2296}
2297
2298/// Subtracts the product of one pair of numbers from the product of another, in place, wrapping
2299/// around at the boundary of the type.
2300pub trait WrappingMulSubMulAssign<Y = Self, Z = Self, W = Self> {
2301    fn wrapping_mul_sub_mul_assign(&mut self, y: Y, z: Z, w: W);
2302}
2303
2304/// Divides a number by another number, wrapping around at the boundary of the type.
2305pub trait WrappingDiv<RHS = Self> {
2306    type Output;
2307
2308    fn wrapping_div(self, other: RHS) -> Self::Output;
2309}
2310
2311/// Divides a number by another number in place, wrapping around at the boundary of the type.
2312pub trait WrappingDivAssign<RHS = Self> {
2313    fn wrapping_div_assign(&mut self, other: RHS);
2314}
2315
2316/// Multiplies two numbers, wrapping around at the boundary of the type.
2317pub trait WrappingMul<RHS = Self> {
2318    type Output;
2319
2320    fn wrapping_mul(self, other: RHS) -> Self::Output;
2321}
2322
2323/// Multiplies a number by another number in place, wrapping around at the boundary of the type.
2324pub trait WrappingMulAssign<RHS = Self> {
2325    fn wrapping_mul_assign(&mut self, other: RHS);
2326}
2327
2328/// Negates a number, wrapping around at the boundary of the type.
2329pub trait WrappingNeg {
2330    type Output;
2331
2332    fn wrapping_neg(self) -> Self::Output;
2333}
2334
2335/// Negates a number in place, wrapping around at the boundary of the type.
2336pub trait WrappingNegAssign {
2337    fn wrapping_neg_assign(&mut self);
2338}
2339
2340/// Raises a number to a power, wrapping around at the boundary of the type.
2341pub trait WrappingPow<RHS> {
2342    type Output;
2343
2344    fn wrapping_pow(self, exp: RHS) -> Self::Output;
2345}
2346
2347/// Raises a number to a power in place, wrapping around at the boundary of the type.
2348pub trait WrappingPowAssign<RHS = Self> {
2349    fn wrapping_pow_assign(&mut self, exp: RHS);
2350}
2351
2352/// Squares a number, wrapping around at the boundary of the type.
2353pub trait WrappingSquare {
2354    type Output;
2355
2356    fn wrapping_square(self) -> Self::Output;
2357}
2358
2359/// Squares a number in place, wrapping around at the boundary of the type.
2360pub trait WrappingSquareAssign {
2361    fn wrapping_square_assign(&mut self);
2362}
2363
2364/// Subtracts two numbers, wrapping around at the boundary of the type.
2365pub trait WrappingSub<RHS = Self> {
2366    type Output;
2367
2368    fn wrapping_sub(self, other: RHS) -> Self::Output;
2369}
2370
2371/// Subtracts a number by another number in place, wrapping around at the boundary of the type.
2372pub trait WrappingSubAssign<RHS = Self> {
2373    fn wrapping_sub_assign(&mut self, other: RHS);
2374}
2375
2376/// Subtracts a number by the product of two other numbers, wrapping around at the boundary of the
2377/// type.
2378pub trait WrappingSubMul<Y = Self, Z = Self> {
2379    type Output;
2380
2381    fn wrapping_sub_mul(self, y: Y, z: Z) -> Self::Output;
2382}
2383
2384/// Subtracts a number by the product of two other numbers, in place, wrapping around at the
2385/// boundary of the type.
2386pub trait WrappingSubMulAssign<Y = Self, Z = Self> {
2387    fn wrapping_sub_mul_assign(&mut self, y: Y, z: Z);
2388}
2389
2390/// Multiplies two numbers, returning the product as a pair of `Self` values.
2391///
2392/// The more significant number always comes first.
2393pub trait XMulYToZZ: Sized {
2394    fn x_mul_y_to_zz(x: Self, y: Self) -> (Self, Self);
2395}
2396
2397/// Adds two numbers, each composed of two `Self` values, returning the sum as a pair of `Self`
2398/// values.
2399///
2400/// The more significant number always comes first. Addition is wrapping, and overflow is not
2401/// indicated.
2402pub trait XXAddYYToZZ: Sized {
2403    fn xx_add_yy_to_zz(x_1: Self, x_0: Self, y_1: Self, y_0: Self) -> (Self, Self);
2404}
2405
2406/// Computes the quotient and remainder of two numbers. The first is composed of two `Self` values,
2407/// and the second of a single one.
2408///
2409/// `x_1` must be less than `y`.
2410pub trait XXDivModYToQR: Sized {
2411    fn xx_div_mod_y_to_qr(x_1: Self, x_0: Self, y: Self) -> (Self, Self);
2412}
2413
2414/// Subtracts two numbers, each composed of two `Self` values, returing the difference as a pair of
2415/// `Self` values.
2416///
2417/// The more significant number always comes first. Subtraction is wrapping, and overflow is not
2418/// indicated.
2419pub trait XXSubYYToZZ: Sized {
2420    fn xx_sub_yy_to_zz(x_1: Self, x_0: Self, y_1: Self, y_0: Self) -> (Self, Self);
2421}
2422
2423/// Adds two numbers, each composed of three `Self` values, returning the sum as a triple of `Self`
2424/// values.
2425///
2426/// The more significant number always comes first. Addition is wrapping, and overflow is not
2427/// indicated.
2428pub trait XXXAddYYYToZZZ: Sized {
2429    fn xxx_add_yyy_to_zzz(
2430        x_2: Self,
2431        x_1: Self,
2432        x_0: Self,
2433        y_2: Self,
2434        y_1: Self,
2435        y_0: Self,
2436    ) -> (Self, Self, Self);
2437}
2438
2439/// Subtracts two numbers, each composed of three `Self` values, returing the difference as a triple
2440/// of `Self` values.
2441///
2442/// The more significant number always comes first. Subtraction is wrapping, and overflow is not
2443/// indicated.
2444pub trait XXXSubYYYToZZZ: Sized {
2445    fn xxx_sub_yyy_to_zzz(
2446        x_2: Self,
2447        x_1: Self,
2448        x_0: Self,
2449        y_2: Self,
2450        y_1: Self,
2451        y_0: Self,
2452    ) -> (Self, Self, Self);
2453}
2454
2455/// Adds two numbers, each composed of four `Self` values, returning the sum as a quadruple of
2456/// `Self` values.
2457///
2458/// The more significant number always comes first. Addition is wrapping, and overflow is not
2459/// indicated.
2460pub trait XXXXAddYYYYToZZZZ: Sized {
2461    #[allow(clippy::too_many_arguments)]
2462    fn xxxx_add_yyyy_to_zzzz(
2463        x_3: Self,
2464        x_2: Self,
2465        x_1: Self,
2466        x_0: Self,
2467        y_3: Self,
2468        y_2: Self,
2469        y_1: Self,
2470        y_0: Self,
2471    ) -> (Self, Self, Self, Self);
2472}