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/// Computes the squared absolute value of a number.
28///
29/// For a real number this is just its square, but for a complex number it is the sum of the squares
30/// of its real and imaginary parts. In both cases it equals $|x|^2$; for Gaussian integers and
31/// Gaussian rationals this quantity is also called the norm.
32pub trait AbsSquared {
33    type Output;
34
35    fn abs_squared(self) -> Self::Output;
36}
37
38/// Replaces a number with its squared absolute value.
39///
40/// For a real number this is just squaring in place. For a complex number the result is the purely
41/// real value $|x|^2$, embedded in the same type.
42pub trait AbsSquaredAssign {
43    fn abs_squared_assign(&mut self);
44}
45
46/// Computes the complex conjugate of a number.
47///
48/// For a complex number the sign of the imaginary part is flipped. For a real number, which is its
49/// own conjugate, this is the identity; the trivial implementations let generic code use
50/// conjugation uniformly, for example when forming Hermitian products.
51pub trait Conjugate {
52    type Output;
53
54    fn conjugate(self) -> Self::Output;
55}
56
57/// Replaces a number with its complex conjugate.
58///
59/// For a complex number the sign of the imaginary part is flipped. For a real number this does
60/// nothing.
61pub trait ConjugateAssign {
62    fn conjugate_assign(&mut self);
63}
64
65/// Multiplies a number by $i$, the imaginary unit.
66///
67/// For a complex number $a + bi$ this is $-b + ai$, a counterclockwise quarter turn. No type in
68/// this crate implements this trait; it exists for complex types downstream, like Gaussian
69/// integers.
70pub trait MulI {
71    type Output;
72
73    fn mul_i(self) -> Self::Output;
74}
75
76/// Replaces a number with its product with $i$, the imaginary unit.
77///
78/// For a complex number $a + bi$ the result is $-b + ai$, a counterclockwise quarter turn. No type
79/// in this crate implements this trait; it exists for complex types downstream, like Gaussian
80/// integers.
81pub trait MulIAssign {
82    fn mul_i_assign(&mut self);
83}
84
85/// Divides a number by $i$, the imaginary unit.
86///
87/// For a complex number $a + bi$ this is $b - ai$, a clockwise quarter turn. No type in this crate
88/// implements this trait; it exists for complex types downstream, like Gaussian integers.
89pub trait DivI {
90    type Output;
91
92    fn div_i(self) -> Self::Output;
93}
94
95/// Replaces a number with its quotient by $i$, the imaginary unit.
96///
97/// For a complex number $a + bi$ the result is $b - ai$, a clockwise quarter turn. No type in this
98/// crate implements this trait; it exists for complex types downstream, like Gaussian integers.
99pub trait DivIAssign {
100    fn div_i_assign(&mut self);
101}
102
103/// Multiplies a number by $i^k$, a power of the imaginary unit.
104///
105/// Only $k$ modulo 4 matters: $i^0 = 1$, $i^1 = i$, $i^2 = -1$, and $i^3 = -i$, so the result is
106/// the number itself, a counterclockwise quarter turn, a half turn, or a clockwise quarter turn.
107/// Since $i^{-k} = i^{3k}$, a negative power is a matter of tripling the exponent. No type in this
108/// crate implements this trait; it exists for complex types downstream, like Gaussian integers.
109pub trait MulIPow {
110    type Output;
111
112    fn mul_i_pow(self, k: u64) -> Self::Output;
113}
114
115/// Replaces a number with its product with $i^k$, a power of the imaginary unit.
116///
117/// Only $k$ modulo 4 matters: $i^0 = 1$, $i^1 = i$, $i^2 = -1$, and $i^3 = -i$, so the result is
118/// the number itself, a counterclockwise quarter turn, a half turn, or a clockwise quarter turn.
119/// Since $i^{-k} = i^{3k}$, a negative power is a matter of tripling the exponent. No type in this
120/// crate implements this trait; it exists for complex types downstream, like Gaussian integers.
121pub trait MulIPowAssign {
122    fn mul_i_pow_assign(&mut self, k: u64);
123}
124
125/// Determines whether a number is a unit of its ring, meaning that it has a multiplicative inverse
126/// in the same ring.
127///
128/// Which elements these are depends on the ring: the only unit of $\mathbb{N}$ is 1, the units of
129/// $\mathbb{Z}$ are $\pm 1$, the Gaussian integers have four, $\pm 1$ and $\pm i$, and in a field
130/// every nonzero element is a unit.
131///
132/// The implementations for the primitive integers follow suit, and the ones for the primitive
133/// floats count every finite nonzero value.
134pub trait IsUnit {
135    fn is_unit(&self) -> bool;
136}
137
138/// Finds the power of $i$ that brings a number into canonical unit form.
139///
140/// A nonzero complex number has four associates under multiplication by the units $\pm 1$ and $\pm
141/// i$; the canonical one is the associate whose argument lies in $(-\pi/4, \pi/4]$, meaning that
142/// its real part is positive and its imaginary part $b$ satisfies $-a < b \leq a$. This function
143/// returns the $k \in \\{0, 1, 2, 3\\}$ such that $x i^k$ is canonical, and 0 for zero.
144///
145/// A real number has only the two associates $\pm x$ to choose between, so the answer there is 0
146/// for a nonnegative number and 2 for a negative one, $i^2$ being $-1$. That is what the primitive
147/// implementations return.
148pub trait CanonicalUnitIPow {
149    fn canonical_unit_i_pow(&self) -> u64;
150}
151
152/// Brings a number into canonical unit form: replaces it with its canonical associate.
153///
154/// The associates of a number are the numbers it becomes when multiplied by a unit of its ring, and
155/// the canonical associate is the one chosen to represent them all. Each implementation documents
156/// its choice. An unsigned number is already canonical, so the unsigned implementations are the
157/// identity; the units of the integers are $\pm 1$, so the signed implementations return the
158/// absolute value; the units of the Gaussian integers are the powers of $i$, and the canonical
159/// associate is the one whose argument lies in $(-\pi/4, \pi/4]$ (see [`CanonicalUnitIPow`]). In a
160/// field every nonzero element is a unit, so the canonical associate of a nonzero rational number,
161/// a nonzero Gaussian rational, or a finite nonzero floating-point number is 1; zero, the
162/// infinities, and NaN are not units, and keep their absolute value. For a polynomial over the
163/// integers it is the associate with a positive leading coefficient, and over the rationals it is
164/// the monic one.
165pub trait CanonicalizeUnit {
166    type Output;
167
168    fn canonicalize_unit(self) -> Self::Output;
169}
170
171/// Replaces a number with its canonical unit form: its canonical associate.
172///
173/// This is the in-place form of [`CanonicalizeUnit`], and the same per-type behaviour applies.
174pub trait CanonicalizeUnitAssign {
175    fn canonicalize_unit_assign(&mut self);
176}
177
178/// Takes the absolute value of a number and converts to the unsigned equivalent.
179pub trait UnsignedAbs {
180    type Output;
181
182    fn unsigned_abs(self) -> Self::Output;
183}
184
185/// Subtracts two numbers and takes the absolute value of the difference.
186pub trait AbsDiff<RHS = Self> {
187    type Output;
188
189    fn abs_diff(self, other: RHS) -> Self::Output;
190}
191
192/// Replaces a number with the absolute value of its difference with another number.
193pub trait AbsDiffAssign<RHS = Self> {
194    fn abs_diff_assign(&mut self, other: RHS);
195}
196
197/// Adds a number and the product of two other numbers.
198///
199/// Depending on the implementing type, the fused operation may compute the same value as the
200/// unfused `self + y * z` more efficiently; or, for types with rounding, it may compute a *more
201/// accurate* value -- the product enters the addition exactly, with a single rounding at the end --
202/// but *less* efficiently, since the exact product must be computed in full. See each
203/// implementation's documentation for which contract it provides.
204pub trait AddMul<Y = Self, Z = Self> {
205    type Output;
206
207    fn add_mul(self, y: Y, z: Z) -> Self::Output;
208}
209
210/// Adds a number and the product of two other numbers, in place.
211///
212/// Depending on the implementing type, the fused operation may compute the same value as the
213/// unfused `*self + y * z` more efficiently; or, for types with rounding, it may compute a *more
214/// accurate* value -- the product enters the addition exactly, with a single rounding at the end --
215/// but *less* efficiently, since the exact product must be computed in full. See each
216/// implementation's documentation for which contract it provides.
217pub trait AddMulAssign<Y = Self, Z = Self> {
218    fn add_mul_assign(&mut self, y: Y, z: Z);
219}
220
221/// Adds the products of two pairs of numbers.
222pub trait MulAddMul<Y = Self, Z = Self, W = Self> {
223    type Output;
224
225    fn mul_add_mul(self, y: Y, z: Z, w: W) -> Self::Output;
226}
227
228/// Adds the products of two pairs of numbers, in place.
229pub trait MulAddMulAssign<Y = Self, Z = Self, W = Self> {
230    fn mul_add_mul_assign(&mut self, y: Y, z: Z, w: W);
231}
232
233/// Multiplies two numbers and right-shifts the product (divides it by a power of 2), rounding the
234/// result according to a specified rounding mode. An [`Ordering`] is also returned, indicating
235/// whether the returned value is less than, equal to, or greater than the exact value.
236///
237/// The product is computed exactly, as if at unlimited width; only the final shifted result must be
238/// representable.
239pub trait MulShrRound<RHS = Self, B = u64> {
240    type Output;
241
242    fn mul_shr_round(self, other: RHS, bits: B, rm: RoundingMode) -> (Self::Output, Ordering);
243}
244
245/// Multiplies two numbers and right-shifts the product (divides it by a power of 2) in place,
246/// rounding the result according to a specified rounding mode. An [`Ordering`] is returned,
247/// indicating whether the assigned value is less than, equal to, or greater than the exact value.
248///
249/// The product is computed exactly, as if at unlimited width; only the final shifted result must be
250/// representable.
251pub trait MulShrRoundAssign<RHS = Self, B = u64> {
252    fn mul_shr_round_assign(&mut self, other: RHS, bits: B, rm: RoundingMode) -> Ordering;
253}
254
255/// Subtracts the product of one pair of numbers from the product of another.
256pub trait MulSubMul<Y = Self, Z = Self, W = Self> {
257    type Output;
258
259    fn mul_sub_mul(self, y: Y, z: Z, w: W) -> Self::Output;
260}
261
262/// Subtracts the product of one pair of numbers from the product of another, in place.
263pub trait MulSubMulAssign<Y = Self, Z = Self, W = Self> {
264    fn mul_sub_mul_assign(&mut self, y: Y, z: Z, w: W);
265}
266
267/// Calculates the AGM (arithmetic-geometric mean) of two numbers.
268pub trait Agm<RHS = Self> {
269    type Output;
270
271    fn agm(self, other: RHS) -> Self::Output;
272}
273
274/// Replaces a number with the AGM (arithmetic-geometric mean) of it and another number.
275pub trait AgmAssign<RHS = Self> {
276    fn agm_assign(&mut self, other: RHS);
277}
278
279/// Calculates the hypotenuse of two numbers, $\sqrt{x^2+y^2}$.
280pub trait Hypot<RHS = Self> {
281    type Output;
282
283    fn hypot(self, other: RHS) -> Self::Output;
284}
285
286/// Replaces a number with the hypotenuse of it and another number.
287pub trait HypotAssign<RHS = Self> {
288    fn hypot_assign(&mut self, other: RHS);
289}
290
291/// Calculates the compound function $(1+x)^n$ of a number $x$.
292pub trait Compound<N> {
293    type Output;
294
295    fn compound(self, n: N) -> Self::Output;
296}
297
298/// Replaces a number $x$ with the compound function $(1+x)^n$.
299pub trait CompoundAssign<N> {
300    fn compound_assign(&mut self, n: N);
301}
302
303/// Left-shifts a number (multiplies it by a power of 2), returning `None` if the result is not
304/// representable.
305pub trait ArithmeticCheckedShl<RHS> {
306    type Output;
307
308    fn arithmetic_checked_shl(self, other: RHS) -> Option<Self::Output>;
309}
310
311/// Right-shifts a number (divides it by a power of 2), returning `None` if the result is not
312/// representable.
313pub trait ArithmeticCheckedShr<RHS> {
314    type Output;
315
316    fn arithmetic_checked_shr(self, other: RHS) -> Option<Self::Output>;
317}
318
319/// Computes the average (arithmetic mean) of two numbers, rounding to the nearest integer. Two-way
320/// ties are broken by rounding to the even integer.
321///
322/// The average is computed without overflow: the result is always exact or within a half of the
323/// exact value, so it always fits in the same type as the inputs.
324pub trait Average<RHS = Self> {
325    type Output;
326
327    fn average(self, other: RHS) -> Self::Output;
328}
329
330/// Computes the average (arithmetic mean) of two numbers, rounding to the nearest integer and
331/// replacing the first number with it. Two-way ties are broken by rounding to the even integer.
332///
333/// The average is computed without overflow: the result is always exact or within a half of the
334/// exact value, so it always fits in the same type as the inputs.
335pub trait AverageAssign<RHS = Self> {
336    fn average_assign(&mut self, other: RHS);
337}
338
339/// Computes the average (arithmetic mean) of two numbers and rounds according to a specified
340/// rounding mode. An [`Ordering`] is also returned, indicating whether the returned value is less
341/// than, equal to, or greater than the exact value.
342///
343/// The average is computed without overflow: the result is always exact or within a half of the
344/// exact value, so it always fits in the same type as the inputs.
345pub trait AverageRound<RHS = Self> {
346    type Output;
347
348    fn average_round(self, other: RHS, rm: RoundingMode) -> (Self::Output, Ordering);
349}
350
351/// Computes the average (arithmetic mean) of two numbers, rounding according to a specified
352/// rounding mode and replacing the first number with it. An [`Ordering`] is returned, indicating
353/// whether the assigned value is less than, equal to, or greater than the exact value.
354///
355/// The average is computed without overflow: the result is always exact or within a half of the
356/// exact value, so it always fits in the same type as the inputs.
357pub trait AverageRoundAssign<RHS = Self> {
358    fn average_round_assign(&mut self, other: RHS, rm: RoundingMode) -> Ordering;
359}
360
361pub trait BinomialCoefficient<T = Self> {
362    fn binomial_coefficient(n: T, k: T) -> Self;
363}
364
365pub trait CheckedBinomialCoefficient<T = Self>: Sized {
366    fn checked_binomial_coefficient(n: T, k: T) -> Option<Self>;
367}
368
369/// Takes the ceiling of a number.
370pub trait Ceiling {
371    type Output;
372
373    fn ceiling(self) -> Self::Output;
374}
375
376/// Replaces a number with its ceiling.
377pub trait CeilingAssign {
378    fn ceiling_assign(&mut self);
379}
380
381/// Takes the absolute valie of a number, returning `None` if the result is not representable.
382pub trait CheckedAbs {
383    type Output;
384
385    fn checked_abs(self) -> Option<Self::Output>;
386}
387
388/// Adds two numbers, returning `None` if the result is not representable.
389pub trait CheckedAdd<RHS = Self> {
390    type Output;
391
392    fn checked_add(self, other: RHS) -> Option<Self::Output>;
393}
394
395/// Adds a number and the product of two other numbers, returning `None` if the result is not
396/// representable.
397pub trait CheckedAddMul<Y = Self, Z = Self> {
398    type Output;
399
400    fn checked_add_mul(self, y: Y, z: Z) -> Option<Self::Output>;
401}
402
403/// Adds the products of two pairs of numbers, returning `None` if the result is not representable.
404pub trait CheckedMulAddMul<Y = Self, Z = Self, W = Self> {
405    type Output;
406
407    fn checked_mul_add_mul(self, y: Y, z: Z, w: W) -> Option<Self::Output>;
408}
409
410/// Subtracts the product of one pair of numbers from the product of another, returning `None` if
411/// the result is not representable.
412pub trait CheckedMulSubMul<Y = Self, Z = Self, W = Self> {
413    type Output;
414
415    fn checked_mul_sub_mul(self, y: Y, z: Z, w: W) -> Option<Self::Output>;
416}
417
418/// Divides two numbers, returning `None` if the result is not representable.
419pub trait CheckedDiv<RHS = Self> {
420    type Output;
421
422    fn checked_div(self, other: RHS) -> Option<Self::Output>;
423}
424
425/// Multiplies two numbers, returning `None` if the result is not representable.
426pub trait CheckedMul<RHS = Self> {
427    type Output;
428
429    fn checked_mul(self, other: RHS) -> Option<Self::Output>;
430}
431
432/// Negates a number, returning `None` if the result is not representable.
433pub trait CheckedNeg {
434    type Output;
435
436    fn checked_neg(self) -> Option<Self::Output>;
437}
438
439/// Finds the smallest integer power of 2 greater than or equal to a number, returning `None` if the
440/// result is not representable.
441pub trait CheckedNextPowerOf2 {
442    type Output;
443
444    fn checked_next_power_of_2(self) -> Option<Self::Output>;
445}
446
447/// Raises a number to a power, returning `None` if the result is not representable.
448pub trait CheckedPow<RHS> {
449    type Output;
450
451    fn checked_pow(self, exp: RHS) -> Option<Self::Output>;
452}
453
454/// Squares a number, returning `None` if the result is not representable.
455pub trait CheckedSquare {
456    type Output;
457
458    fn checked_square(self) -> Option<Self::Output>;
459}
460
461/// Subtracts two numbers, returning `None` if the result is not representable.
462pub trait CheckedSub<RHS = Self> {
463    type Output;
464
465    fn checked_sub(self, other: RHS) -> Option<Self::Output>;
466}
467
468/// Subtracts a number by the product of two other numbers, returning `None` if the result is not
469/// representable.
470pub trait CheckedSubMul<Y = Self, Z = Self> {
471    type Output;
472
473    fn checked_sub_mul(self, y: Y, z: Z) -> Option<Self::Output>;
474}
475
476/// Determines whether two numbers are coprime.
477pub trait CoprimeWith<RHS = Self> {
478    fn coprime_with(self, other: RHS) -> bool;
479}
480
481/// Combines two congruences by the Chinese remainder theorem, returning `None` if the moduli are
482/// not coprime. The residues must be already reduced modulo their moduli.
483pub trait Crt<M1 = Self, R2 = Self, M2 = Self> {
484    type Output;
485
486    fn crt(self, m1: M1, r2: R2, m2: M2) -> Option<Self::Output>;
487}
488
489/// Combines two congruences by the Chinese remainder theorem, returning the representative of
490/// smallest absolute value, or `None` if the moduli are not coprime. The first residue may be
491/// negative.
492pub trait BalancedCrt<M1 = Self, R2 = Self, M2 = Self> {
493    type Output;
494
495    fn balanced_crt(self, m1: M1, r2: R2, m2: M2) -> Option<Self::Output>;
496}
497
498/// Divides two numbers, assuming the first exactly divides the second.
499///
500/// If it doesn't, the `div_exact` function may panic or return a meaningless result.
501pub trait DivExact<RHS = Self> {
502    type Output;
503
504    fn div_exact(self, other: RHS) -> Self::Output;
505}
506
507/// Divides a number by another number in place, assuming the first exactly divides the second.
508///
509/// If it doesn't, this function may panic or assign a meaningless number to the first number.
510pub trait DivExactAssign<RHS = Self> {
511    fn div_exact_assign(&mut self, other: RHS);
512}
513
514/// Divides two numbers, returning the quotient and remainder. The quotient is rounded towards
515/// negative infinity, and the remainder has the same sign as the divisor (second input).
516///
517/// The quotient and remainder satisfy $x = qy + r$ and $0 \leq |r| < |y|$.
518pub trait DivMod<RHS = Self> {
519    type DivOutput;
520    type ModOutput;
521
522    fn div_mod(self, other: RHS) -> (Self::DivOutput, Self::ModOutput);
523}
524
525/// Divides two numbers, returning the quotient and remainder. The quotient is rounded towards
526/// negative infinity, and the remainder has the same sign as the divisor (second input).
527///
528/// The quotient and remainder satisfy $x = qy + r$ and $0 \leq |r| < |y|$.
529///
530/// If multiple divisions by the same divisor are necessary, it can be quicker to precompute some
531/// piece of data based on the divisor and reuse it in the division calls. This trait provides a
532/// function for precomputing the data and a function for using it during division.
533pub trait DivModPrecomputed<RHS = Self> {
534    type DivOutput;
535    type ModOutput;
536    type Data;
537
538    /// Precomputes some data to use for division.
539    fn precompute_div_mod_data(other: &RHS) -> Self::Data;
540
541    fn div_mod_precomputed(
542        self,
543        other: RHS,
544        data: &Self::Data,
545    ) -> (Self::DivOutput, Self::ModOutput);
546}
547
548/// Divides a number by another number in place, returning the remainder. The quotient is rounded
549/// towards negative infinity, and the remainder has the same sign as the divisor (second input).
550///
551/// The quotient and remainder satisfy $x = qy + r$ and $0 \leq |r| < |y|$.
552///
553/// If multiple divisions by the same divisor are necessary, it can be quicker to precompute some
554/// piece of data based on the divisor and reuse it in the division calls. This trait provides a
555/// function for using precomputed data during division. For precomputing the data, use the
556/// [`precompute_div_mod_data`](DivModPrecomputed::precompute_div_mod_data) function in
557/// [`DivModPrecomputed`].
558pub trait DivAssignModPrecomputed<RHS = Self>: DivModPrecomputed<RHS> {
559    fn div_assign_mod_precomputed(&mut self, other: RHS, data: &Self::Data) -> Self::ModOutput;
560}
561
562/// Divides two numbers, returning just the quotient. The quotient is rounded towards the quotient
563/// that makes the remainder nonnegative.
564///
565/// If the remainder were computed, the quotient and remainder would satisfy $x = qy + r$ and $0
566/// \leq r < |y|$.
567pub trait DivEuclidean<RHS = Self> {
568    type Output;
569
570    fn div_euclidean(self, other: RHS) -> Self::Output;
571}
572
573/// Divides a number by another number in place, keeping just the quotient. The quotient is rounded
574/// towards the quotient that makes the remainder nonnegative.
575///
576/// If the remainder were computed, the quotient and remainder would satisfy $x = qy + r$ and $0
577/// \leq r < |y|$.
578pub trait DivEuclideanAssign<RHS = Self> {
579    fn div_euclidean_assign(&mut self, other: RHS);
580}
581
582/// Divides two numbers, returning the quotient and remainder. The quotient is rounded towards the
583/// quotient that makes the remainder nonnegative, and the remainder is always nonnegative.
584///
585/// The quotient and remainder satisfy $x = qy + r$ and $0 \leq r < |y|$.
586pub trait DivModEuclidean<RHS = Self> {
587    type DivOutput;
588    type ModOutput;
589
590    fn div_mod_euclidean(self, other: RHS) -> (Self::DivOutput, Self::ModOutput);
591}
592
593/// Divides a number by another number in place, returning the remainder. The quotient is rounded
594/// towards negative infinity, and the remainder has the same sign as the divisor (second input).
595///
596/// The quotient and remainder satisfy $x = qy + r$ and $0 \leq |r| < |y|$.
597pub trait DivAssignMod<RHS = Self> {
598    type ModOutput;
599
600    fn div_assign_mod(&mut self, other: RHS) -> Self::ModOutput;
601}
602
603/// Divides a number by another number in place, returning the remainder. The quotient is rounded
604/// towards the quotient that makes the remainder nonnegative, and the remainder is always
605/// nonnegative.
606///
607/// The quotient and remainder satisfy $x = qy + r$ and $0 \leq r < |y|$.
608pub trait DivAssignModEuclidean<RHS = Self> {
609    type ModOutput;
610
611    fn div_assign_mod_euclidean(&mut self, other: RHS) -> Self::ModOutput;
612}
613
614/// Divides two numbers, returning the quotient and remainder. The quotient is rounded towards zero,
615/// and the remainder has the same sign as the dividend (first input).
616///
617/// The quotient and remainder satisfy $x = qy + r$ and $0 \leq |r| < |y|$.
618pub trait DivRem<RHS = Self> {
619    type DivOutput;
620    type RemOutput;
621
622    fn div_rem(self, other: RHS) -> (Self::DivOutput, Self::RemOutput);
623}
624
625/// Divides a number by another number in place, returning the remainder. The quotient is rounded
626/// towards zero, and the remainder has the same sign as the dividend (first input).
627///
628/// The quotient and remainder satisfy $x = qy + r$ and $0 \leq |r| < |y|$.
629pub trait DivAssignRem<RHS = Self> {
630    type RemOutput;
631
632    fn div_assign_rem(&mut self, other: RHS) -> Self::RemOutput;
633}
634
635/// Divides a number by another number, returning the ceiling of the quotient and the remainder of
636/// the negative of the first number divided by the second.
637///
638/// The quotient and remainder satisfy $x = qy - r$ and $0 \leq r < y$.
639pub trait CeilingDivNegMod<RHS = Self> {
640    type DivOutput;
641    type ModOutput;
642
643    fn ceiling_div_neg_mod(self, other: RHS) -> (Self::DivOutput, Self::ModOutput);
644}
645
646/// Divides a number by another number in place, taking the ceiling of the quotient and returning
647/// the remainder of the negative of the first number divided by the second.
648///
649/// The quotient and remainder satisfy $x = qy - r$ and $0 \leq r < y$.
650pub trait CeilingDivAssignNegMod<RHS = Self> {
651    type ModOutput;
652
653    fn ceiling_div_assign_neg_mod(&mut self, other: RHS) -> Self::ModOutput;
654}
655
656/// Divides a number by another number, returning the quotient and remainder. The quotient is
657/// rounded towards positive infinity and the remainder has the opposite sign as the divisor (second
658/// input).
659///
660/// The quotient and remainder satisfy $x = qy + r$ and $0 \leq |r| < |y|$.
661pub trait CeilingDivMod<RHS = Self> {
662    type DivOutput;
663    type ModOutput;
664
665    fn ceiling_div_mod(self, other: RHS) -> (Self::DivOutput, Self::ModOutput);
666}
667
668/// Divides a number by another number in place, taking the quotient and returning the remainder.
669/// The quotient is rounded towards positive infinity and the remainder has the opposite sign of the
670/// divisor (second input).
671///
672/// The quotient and remainder satisfy $x = qy + r$ and $0 \leq |r| < |y|$.
673pub trait CeilingDivAssignMod<RHS = Self> {
674    type ModOutput;
675
676    fn ceiling_div_assign_mod(&mut self, other: RHS) -> Self::ModOutput;
677}
678
679/// Divides a number by another number and rounds according to a specified rounding mode. An
680/// [`Ordering`] is also returned, indicating whether the returned value is less than, equal to, or
681/// greater than the exact value.
682pub trait DivRound<RHS = Self> {
683    type Output;
684
685    fn div_round(self, other: RHS, rm: RoundingMode) -> (Self::Output, Ordering);
686}
687
688/// Divides a number by another number in place and rounds according to a specified rounding mode.
689/// An [`Ordering`] is returned, indicating whether the assigned value is less than, equal to, or
690/// greater than the exact value.
691pub trait DivRoundAssign<RHS = Self> {
692    fn div_round_assign(&mut self, other: RHS, rm: RoundingMode) -> Ordering;
693}
694
695/// Determines whether a number is divisible by $2^k$.
696pub trait DivisibleByPowerOf2 {
697    fn divisible_by_power_of_2(self, pow: u64) -> bool;
698}
699
700/// Determines whether a number is divisible by another number.
701pub trait DivisibleBy<RHS = Self> {
702    fn divisible_by(self, other: RHS) -> bool;
703}
704
705/// Determines whether a number is equivalent to another number modulo $2^k$.
706pub trait EqModPowerOf2<RHS = Self> {
707    fn eq_mod_power_of_2(self, other: RHS, pow: u64) -> bool;
708}
709
710/// Determines whether a number is equivalent to another number modulo $m$.
711pub trait EqMod<RHS = Self, M = Self> {
712    fn eq_mod(self, other: RHS, m: M) -> bool;
713}
714
715/// Computes the GCD (greatest common divisor) of two numbers $a$ and $b$, and also the coefficients
716/// $x$ and $y$ in Bézout's identity $ax+by=\gcd(a,b)$.
717///
718/// The are infinitely many $x$, $y$ that satisfy the identity, so the full specification is more
719/// detailed:
720///
721/// - $f(0, 0) = (0, 0, 0)$.
722/// - $f(a, ak) = (a, 1, 0)$ if $a > 0$ and $k \neq 1$.
723/// - $f(a, ak) = (-a, -1, 0)$ if $a < 0$ and $k \neq 1$.
724/// - $f(bk, b) = (b, 0, 1)$ if $b > 0$.
725/// - $f(bk, b) = (-b, 0, -1)$ if $b < 0$.
726/// - $f(a, b) = (g, x, y)$ if $a \neq 0$ and $b \neq 0$ and $\gcd(a, b) \neq \min(|a|, |b|)$, where
727///   $g = \gcd(a, b) \geq 0$, $ax + by = g$, $x \leq \lfloor b/g \rfloor$, and $y \leq \lfloor a/g
728///   \rfloor$.
729pub trait ExtendedGcd<RHS = Self> {
730    type Gcd;
731    type Cofactor;
732
733    fn extended_gcd(self, other: RHS) -> (Self::Gcd, Self::Cofactor, Self::Cofactor);
734}
735
736/// Computes the $n$th Bell number: the number of ways to partition a set of $n$ elements.
737pub trait BellNumber {
738    fn bell_number(n: u64) -> Self;
739}
740
741/// Computes the $n$th Bell number, returning `None` if the result is too large to be represented.
742pub trait CheckedBellNumber: Sized {
743    fn checked_bell_number(n: u64) -> Option<Self>;
744}
745
746/// Computes the factorial of a `u64`.
747pub trait Factorial {
748    fn factorial(n: u64) -> Self;
749}
750
751/// Computes the factorial of a `u64`, returning `None` if the result is too large to be
752/// represented.
753pub trait CheckedFactorial: Sized {
754    fn checked_factorial(n: u64) -> Option<Self>;
755}
756
757/// Computes the double factorial of a `u64`. The double factorial of a non-negative integer is the
758/// product of all the positive integers that are less than or equal to it and have the same parity
759/// as it.
760pub trait DoubleFactorial {
761    fn double_factorial(n: u64) -> Self;
762}
763
764/// Computes the double factorial of a `u64`, returning `None` if the result is too large to be
765/// represented. The double factorial of a non-negative integer is the product of all the positive
766/// integers that are less than or equal to it and have the same parity as it.
767pub trait CheckedDoubleFactorial: Sized {
768    fn checked_double_factorial(n: u64) -> Option<Self>;
769}
770
771/// Computes the $m$-multifactorial of a `u64`. The $m$-multifactorial of a non-negative integer $n$
772/// is the product of all integers $k$ such that $0<k\leq n$ and $k\equiv n \pmod m$.
773pub trait Multifactorial {
774    fn multifactorial(n: u64, m: u64) -> Self;
775}
776
777/// Computes the $m$-multifactorial of a `u64`, returning `None` if the result is too large to be
778/// represented. The $m$-multifactorial of a non-negative integer $n$ is the product of all integers
779/// $k$ such that $0<k\leq n$ and $k\equiv n \pmod m$.
780pub trait CheckedMultifactorial: Sized {
781    fn checked_multifactorial(n: u64, m: u64) -> Option<Self>;
782}
783
784/// Computes the subfactorial of a `u64`. The subfactorial of a non-negative integer $n$ counts the
785/// number of derangements of $n$ elements, which are the permutations in which no element is fixed.
786pub trait Subfactorial {
787    fn subfactorial(n: u64) -> Self;
788}
789
790/// Computes the subfactorial of a `u64`, returning `None` if the result is too large to be
791/// represented. The subfactorial of a non-negative integer $n$ counts the number of derangements of
792/// $n$ elements, which are the permutations in which no element is fixed.
793pub trait CheckedSubfactorial: Sized {
794    fn checked_subfactorial(n: u64) -> Option<Self>;
795}
796
797/// Computes the rising factorial of a number: the product of the `n` consecutive numbers starting
798/// at `self`, or 1 when `n` is 0.
799pub trait RisingFactorial {
800    type Output;
801
802    fn rising_factorial(self, n: u64) -> Self::Output;
803}
804
805/// Computes the rising factorial of a number, returning `None` if the result cannot be represented.
806pub trait CheckedRisingFactorial: Sized {
807    fn checked_rising_factorial(self, n: u64) -> Option<Self>;
808}
809
810/// Computes the falling factorial of a number: the product of the `n` consecutive numbers counting
811/// down from `self`, or 1 when `n` is 0.
812pub trait FallingFactorial {
813    type Output;
814
815    fn falling_factorial(self, n: u64) -> Self::Output;
816}
817
818/// Computes the falling factorial of a number, returning `None` if the result cannot be
819/// represented.
820pub trait CheckedFallingFactorial: Sized {
821    fn checked_falling_factorial(self, n: u64) -> Option<Self>;
822}
823
824/// Computes the $n$th Fibonacci number, either alone or paired with its predecessor:
825/// `fibonacci_pair(n)` returns $(F(n), F(n-1))$.
826pub trait Fibonacci: Sized {
827    fn fibonacci(n: u64) -> Self;
828
829    fn fibonacci_pair(n: u64) -> (Self, Self);
830}
831
832/// Computes the $n$th Fibonacci number, either alone or paired with its predecessor, returning
833/// `None` if the result is too large to be represented.
834pub trait CheckedFibonacci: Sized {
835    fn checked_fibonacci(n: u64) -> Option<Self>;
836
837    fn checked_fibonacci_pair(n: u64) -> Option<(Self, Self)>;
838}
839
840/// Takes the floor of a number.
841pub trait Floor {
842    type Output;
843
844    fn floor(self) -> Self::Output;
845}
846
847/// Replaces a number with its floor.
848pub trait FloorAssign {
849    fn floor_assign(&mut self);
850}
851
852/// Calculates the GCD (greatest common divisor) of two numbers.
853pub trait Gcd<RHS = Self> {
854    type Output;
855
856    fn gcd(self, other: RHS) -> Self::Output;
857}
858
859/// Replaces a number with the GCD (greatest common divisor) of it and another number.
860pub trait GcdAssign<RHS = Self> {
861    fn gcd_assign(&mut self, other: RHS);
862}
863
864/// Calculates the height of a value: the largest of the magnitudes of the parts it is built from.
865///
866/// For a rational number $p/q$ in lowest terms this is $\max(|p|, q)$, the measure in which
867/// Diophantine approximation bounds are usually stated. For something built out of several such
868/// values — a polynomial, or a complex number — it is the largest of their heights.
869pub trait Height {
870    type Output;
871
872    fn to_height(&self) -> Self::Output;
873
874    fn into_height(self) -> Self::Output;
875
876    fn height_significant_bits(&self) -> u64;
877}
878
879/// Lends the height of a value, for the types that already hold it.
880///
881/// Most heights are one of the magnitudes the value is built from, so they can be lent rather than
882/// built: a rational number's height is its numerator or its denominator, and a polynomial's is one
883/// of its coefficients. This is separate from [`Height`] because not every height is stored — the
884/// coefficients of a polynomial over the rationals share one denominator, so a coefficient's height
885/// has to be worked out before it can be compared with the others, and there is nothing to lend.
886///
887/// It is also not worth implementing where the height is a primitive integer, since there is no
888/// clone to avoid.
889pub trait HeightRef: Height {
890    fn height_ref(&self) -> &Self::Output;
891}
892
893/// Determines whether a number is an integer power of 2.
894pub trait IsPowerOf2 {
895    fn is_power_of_2(&self) -> bool;
896}
897
898/// Calculates the LCM (least common multiple) of two numbers.
899pub trait Lcm<RHS = Self> {
900    type Output;
901
902    fn lcm(self, other: RHS) -> Self::Output;
903}
904
905/// Replaces a number with the LCM (least common multiple) of it and another number.
906pub trait LcmAssign<RHS = Self> {
907    fn lcm_assign(&mut self, other: RHS);
908}
909
910/// Splits a value into its content and its primitive part.
911///
912/// This applies to an element of a vector space over the rationals with a distinguished integer
913/// lattice, like a rational polynomial, a Gaussian rational, or a vector of rationals. The content
914/// is the unique non-negative rational $c$ such that the value is $c$ times a lattice element with
915/// coprime coordinates, and the primitive part is that element; the value is the product of the
916/// two. Zero has content 0 and primitive part 0. For an element of the lattice itself, the content
917/// is the GCD of the coordinates, a non-negative integer.
918pub trait ContentAndPrimitivePart {
919    type Content;
920    type PrimitivePart;
921
922    fn content_and_primitive_part(self) -> (Self::Content, Self::PrimitivePart);
923}
924
925/// Computes the content of a value: the unique non-negative rational $c$ such that the value is $c$
926/// times an element of the underlying integer lattice with coprime coordinates. See
927/// [`ContentAndPrimitivePart`].
928pub trait Content {
929    type Output;
930
931    fn content(self) -> Self::Output;
932}
933
934/// Computes the primitive part of a value: the element of the underlying integer lattice, with
935/// coprime coordinates, that the value is a non-negative rational multiple of. See
936/// [`ContentAndPrimitivePart`].
937pub trait PrimitivePart {
938    type Output;
939
940    fn primitive_part(self) -> Self::Output;
941}
942
943/// Computes $e^x$, the exponential of a number.
944pub trait Exp {
945    type Output;
946
947    fn exp(self) -> Self::Output;
948}
949
950/// Replaces a number with its exponential, $e^x$.
951pub trait ExpAssign {
952    fn exp_assign(&mut self);
953}
954
955/// Computes $\cos(x)$, the cosine of a number.
956pub trait Cos {
957    type Output;
958
959    fn cos(self) -> Self::Output;
960}
961
962/// Replaces a number with its cosine, $\cos(x)$.
963pub trait CosAssign {
964    fn cos_assign(&mut self);
965}
966
967/// Computes $\sin(x)$, the sine of a number.
968pub trait Sin {
969    type Output;
970
971    fn sin(self) -> Self::Output;
972}
973
974/// Replaces a number with its sine, $\sin(x)$.
975pub trait SinAssign {
976    fn sin_assign(&mut self);
977}
978
979/// Computes $\sin(x)$ and $\cos(x)$, the sine and cosine of a number, together.
980pub trait SinCos {
981    type Output;
982
983    fn sin_cos(self) -> (Self::Output, Self::Output);
984}
985
986/// Computes $\tan(x)$, the tangent of a number.
987pub trait Tan {
988    type Output;
989
990    fn tan(self) -> Self::Output;
991}
992
993/// Replaces a number with its tangent, $\tan(x)$.
994pub trait TanAssign {
995    fn tan_assign(&mut self);
996}
997
998/// Computes $\sec(x)$, the secant of a number.
999pub trait Sec {
1000    type Output;
1001
1002    fn sec(self) -> Self::Output;
1003}
1004
1005/// Replaces a number with its secant, $\sec(x)$.
1006pub trait SecAssign {
1007    fn sec_assign(&mut self);
1008}
1009
1010/// Computes $\csc(x)$, the cosecant of a number.
1011pub trait Csc {
1012    type Output;
1013
1014    fn csc(self) -> Self::Output;
1015}
1016
1017/// Replaces a number with its cosecant, $\csc(x)$.
1018pub trait CscAssign {
1019    fn csc_assign(&mut self);
1020}
1021
1022/// Computes $\cot(x)$, the cotangent of a number.
1023pub trait Cot {
1024    type Output;
1025
1026    fn cot(self) -> Self::Output;
1027}
1028
1029/// Replaces a number with its cotangent, $\cot(x)$.
1030pub trait CotAssign {
1031    fn cot_assign(&mut self);
1032}
1033
1034/// Computes $\arctan(x)$, the arctangent of a number.
1035pub trait Atan {
1036    type Output;
1037
1038    fn atan(self) -> Self::Output;
1039}
1040
1041/// Replaces a number with its arctangent, $\arctan(x)$.
1042pub trait AtanAssign {
1043    fn atan_assign(&mut self);
1044}
1045
1046/// Computes $\operatorname{atan2}(y,x)$, the angle of the point $(x,y)$ measured from the positive
1047/// $x$-axis.
1048pub trait Atan2<RHS = Self> {
1049    type Output;
1050
1051    fn atan2(self, other: RHS) -> Self::Output;
1052}
1053
1054/// Replaces a number $y$ with $\operatorname{atan2}(y,x)$, the angle of the point $(x,y)$ measured
1055/// from the positive $x$-axis.
1056pub trait Atan2Assign<RHS = Self> {
1057    fn atan2_assign(&mut self, other: RHS);
1058}
1059
1060/// Computes $\arcsin(x)$, the arcsine of a number.
1061pub trait Asin {
1062    type Output;
1063
1064    fn asin(self) -> Self::Output;
1065}
1066
1067/// Replaces a number with its arcsine, $\arcsin(x)$.
1068pub trait AsinAssign {
1069    fn asin_assign(&mut self);
1070}
1071
1072/// Computes $\arccos(x)$, the arccosine of a number.
1073pub trait Acos {
1074    type Output;
1075
1076    fn acos(self) -> Self::Output;
1077}
1078
1079/// Replaces a number with its arccosine, $\arccos(x)$.
1080pub trait AcosAssign {
1081    fn acos_assign(&mut self);
1082}
1083
1084/// Computes $\operatorname{asec}(x)$, the arcsecant of a number.
1085pub trait Asec {
1086    type Output;
1087
1088    fn asec(self) -> Self::Output;
1089}
1090
1091/// Replaces a number with its arcsecant, $\operatorname{asec}(x)$.
1092pub trait AsecAssign {
1093    fn asec_assign(&mut self);
1094}
1095
1096/// Computes $\operatorname{acsc}(x)$, the arccosecant of a number.
1097pub trait Acsc {
1098    type Output;
1099
1100    fn acsc(self) -> Self::Output;
1101}
1102
1103/// Replaces a number with its arccosecant, $\operatorname{acsc}(x)$.
1104pub trait AcscAssign {
1105    fn acsc_assign(&mut self);
1106}
1107
1108/// Computes $\operatorname{acot}(x)$, the arccotangent of a number.
1109pub trait Acot {
1110    type Output;
1111
1112    fn acot(self) -> Self::Output;
1113}
1114
1115/// Replaces a number with its arccotangent, $\operatorname{acot}(x)$.
1116pub trait AcotAssign {
1117    fn acot_assign(&mut self);
1118}
1119
1120/// Computes $\cosh(x)$, the hyperbolic cosine of a number.
1121pub trait Cosh {
1122    type Output;
1123
1124    fn cosh(self) -> Self::Output;
1125}
1126
1127/// Replaces a number with its hyperbolic cosine, $\cosh(x)$.
1128pub trait CoshAssign {
1129    fn cosh_assign(&mut self);
1130}
1131
1132/// Computes $\sinh(x)$, the hyperbolic sine of a number.
1133pub trait Sinh {
1134    type Output;
1135
1136    fn sinh(self) -> Self::Output;
1137}
1138
1139/// Replaces a number with its hyperbolic sine, $\sinh(x)$.
1140pub trait SinhAssign {
1141    fn sinh_assign(&mut self);
1142}
1143
1144/// Computes $\sinh(x)$ and $\cosh(x)$, the hyperbolic sine and cosine of a number, together.
1145pub trait SinhCosh {
1146    type Output;
1147
1148    fn sinh_cosh(self) -> (Self::Output, Self::Output);
1149}
1150
1151/// Replaces a number with its hyperbolic sine, $\sinh(x)$, and writes its hyperbolic cosine,
1152/// $\cosh(x)$, to a second number.
1153pub trait SinhCoshAssign {
1154    fn sinh_cosh_assign(&mut self, cosh: &mut Self);
1155}
1156
1157/// Computes $\tanh(x)$, the hyperbolic tangent of a number.
1158pub trait Tanh {
1159    type Output;
1160
1161    fn tanh(self) -> Self::Output;
1162}
1163
1164/// Replaces a number with its hyperbolic tangent, $\tanh(x)$.
1165pub trait TanhAssign {
1166    fn tanh_assign(&mut self);
1167}
1168
1169/// Computes $\operatorname{sech}(x)$, the hyperbolic secant of a number.
1170pub trait Sech {
1171    type Output;
1172
1173    fn sech(self) -> Self::Output;
1174}
1175
1176/// Replaces a number with its hyperbolic secant, $\operatorname{sech}(x)$.
1177pub trait SechAssign {
1178    fn sech_assign(&mut self);
1179}
1180
1181/// Computes $\operatorname{csch}(x)$, the hyperbolic cosecant of a number.
1182pub trait Csch {
1183    type Output;
1184
1185    fn csch(self) -> Self::Output;
1186}
1187
1188/// Replaces a number with its hyperbolic cosecant, $\operatorname{csch}(x)$.
1189pub trait CschAssign {
1190    fn csch_assign(&mut self);
1191}
1192
1193/// Computes $\coth(x)$, the hyperbolic cotangent of a number.
1194pub trait Coth {
1195    type Output;
1196
1197    fn coth(self) -> Self::Output;
1198}
1199
1200/// Replaces a number with its hyperbolic cotangent, $\coth(x)$.
1201pub trait CothAssign {
1202    fn coth_assign(&mut self);
1203}
1204
1205/// Computes $\operatorname{asinh}(x)$, the inverse hyperbolic sine of a number.
1206pub trait Asinh {
1207    type Output;
1208
1209    fn asinh(self) -> Self::Output;
1210}
1211
1212/// Replaces a number with its inverse hyperbolic sine, $\operatorname{asinh}(x)$.
1213pub trait AsinhAssign {
1214    fn asinh_assign(&mut self);
1215}
1216
1217/// Computes $\operatorname{acosh}(x)$, the inverse hyperbolic cosine of a number.
1218pub trait Acosh {
1219    type Output;
1220
1221    fn acosh(self) -> Self::Output;
1222}
1223
1224/// Replaces a number with its inverse hyperbolic cosine, $\operatorname{acosh}(x)$.
1225pub trait AcoshAssign {
1226    fn acosh_assign(&mut self);
1227}
1228
1229/// Computes $\operatorname{atanh}(x)$, the inverse hyperbolic tangent of a number.
1230pub trait Atanh {
1231    type Output;
1232
1233    fn atanh(self) -> Self::Output;
1234}
1235
1236/// Replaces a number with its inverse hyperbolic tangent, $\operatorname{atanh}(x)$.
1237pub trait AtanhAssign {
1238    fn atanh_assign(&mut self);
1239}
1240
1241/// Computes $\operatorname{asech}(x)$, the inverse hyperbolic secant of a number.
1242pub trait Asech {
1243    type Output;
1244
1245    fn asech(self) -> Self::Output;
1246}
1247
1248/// Replaces a number with its inverse hyperbolic secant, $\operatorname{asech}(x)$.
1249pub trait AsechAssign {
1250    fn asech_assign(&mut self);
1251}
1252
1253/// Computes $\operatorname{acsch}(x)$, the inverse hyperbolic cosecant of a number.
1254pub trait Acsch {
1255    type Output;
1256
1257    fn acsch(self) -> Self::Output;
1258}
1259
1260/// Replaces a number with its inverse hyperbolic cosecant, $\operatorname{acsch}(x)$.
1261pub trait AcschAssign {
1262    fn acsch_assign(&mut self);
1263}
1264
1265/// Computes $\operatorname{acoth}(x)$, the inverse hyperbolic cotangent of a number.
1266pub trait Acoth {
1267    type Output;
1268
1269    fn acoth(self) -> Self::Output;
1270}
1271
1272/// Replaces a number with its inverse hyperbolic cotangent, $\operatorname{acoth}(x)$.
1273pub trait AcothAssign {
1274    fn acoth_assign(&mut self);
1275}
1276
1277/// Replaces a number with its sine, $\sin(x)$, and writes its cosine, $\cos(x)$, to a second
1278/// number.
1279pub trait SinCosAssign {
1280    fn sin_cos_assign(&mut self, cos: &mut Self);
1281}
1282
1283/// Computes $e^x-1$, the exponential of a number, minus one.
1284pub trait ExpXMinus1 {
1285    type Output;
1286
1287    fn exp_x_minus_1(self) -> Self::Output;
1288}
1289
1290/// Replaces a number $x$ with $e^x-1$.
1291pub trait ExpXMinus1Assign {
1292    fn exp_x_minus_1_assign(&mut self);
1293}
1294
1295/// Computes $2^x-1$, two raised to the power of a number, minus one.
1296pub trait PowerOf2XMinus1 {
1297    type Output;
1298
1299    fn power_of_2_x_minus_1(self) -> Self::Output;
1300}
1301
1302/// Replaces a number $x$ with $2^x-1$.
1303pub trait PowerOf2XMinus1Assign {
1304    fn power_of_2_x_minus_1_assign(&mut self);
1305}
1306
1307/// Computes $10^x-1$, ten raised to the power of a number, minus one.
1308pub trait PowerOf10XMinus1 {
1309    type Output;
1310
1311    fn power_of_10_x_minus_1(self) -> Self::Output;
1312}
1313
1314/// Replaces a number $x$ with $10^x-1$.
1315pub trait PowerOf10XMinus1Assign {
1316    fn power_of_10_x_minus_1_assign(&mut self);
1317}
1318
1319/// Takes the natural logarithm of a number.
1320pub trait Ln {
1321    type Output;
1322
1323    fn ln(self) -> Self::Output;
1324}
1325
1326/// Replaces a number with its natural logarithm.
1327pub trait LnAssign {
1328    fn ln_assign(&mut self);
1329}
1330
1331/// Computes $\ln(1+x)$.
1332pub trait Ln1PlusX {
1333    type Output;
1334
1335    fn ln_1_plus_x(self) -> Self::Output;
1336}
1337
1338/// Replaces a number $x$ by $\ln(1+x)$.
1339pub trait Ln1PlusXAssign {
1340    fn ln_1_plus_x_assign(&mut self);
1341}
1342
1343/// Calculates the LCM (least common multiple) of two numbers, returning `None` if the result is not
1344/// representable.
1345pub trait CheckedLcm<RHS = Self> {
1346    type Output;
1347
1348    fn checked_lcm(self, other: RHS) -> Option<Self::Output>;
1349}
1350
1351/// Calculates the Legendre symbol of two numbers. Typically the implementations will be identical
1352/// to those of [`JacobiSymbol`].
1353pub trait LegendreSymbol<RHS = Self> {
1354    fn legendre_symbol(self, other: RHS) -> i8;
1355}
1356
1357/// Calculates the Jacobi symbol of two numbers.
1358pub trait JacobiSymbol<RHS = Self> {
1359    fn jacobi_symbol(self, other: RHS) -> i8;
1360}
1361
1362/// Calculates the Kronecker symbol of two numbers.
1363pub trait KroneckerSymbol<RHS = Self> {
1364    fn kronecker_symbol(self, other: RHS) -> i8;
1365}
1366
1367/// Calculates the base-$b$ logarithm of a number, or returns `None` if the number is not a perfect
1368/// power of $b$.
1369pub trait CheckedLogBase<B = Self> {
1370    type Output;
1371
1372    fn checked_log_base(self, base: B) -> Option<Self::Output>;
1373}
1374
1375/// Calculates the floor of the base-$b$ logarithm of a number.
1376pub trait FloorLogBase<B = Self> {
1377    type Output;
1378
1379    fn floor_log_base(self, base: B) -> Self::Output;
1380}
1381
1382/// Calculates the ceiling of the base-$b$ logarithm of a number.
1383pub trait CeilingLogBase<B = Self> {
1384    type Output;
1385
1386    fn ceiling_log_base(self, base: B) -> Self::Output;
1387}
1388
1389/// Calculates the base-2 logarithm of a number, or returns `None` if the number is not a perfect
1390/// power of 2.
1391pub trait CheckedLogBase2 {
1392    type Output;
1393
1394    fn checked_log_base_2(self) -> Option<Self::Output>;
1395}
1396
1397/// Calculates the base-2 logarithm of a number.
1398pub trait LogBase2 {
1399    type Output;
1400
1401    fn log_base_2(self) -> Self::Output;
1402}
1403
1404/// Replaces a number with its base-2 logarithm.
1405pub trait LogBase2Assign {
1406    fn log_base_2_assign(&mut self);
1407}
1408
1409/// Calculates the base-10 logarithm of a number, rounding the (generally irrational) result.
1410pub trait LogBase10 {
1411    type Output;
1412
1413    fn log_base_10(self) -> Self::Output;
1414}
1415
1416/// Replaces a number with its base-10 logarithm, rounding the (generally irrational) result.
1417pub trait LogBase10Assign {
1418    fn log_base_10_assign(&mut self);
1419}
1420
1421/// Computes $\log_2(1+x)$.
1422pub trait LogBase2Of1PlusX {
1423    type Output;
1424
1425    fn log_base_2_1_plus_x(self) -> Self::Output;
1426}
1427
1428/// Replaces a number $x$ by $\log_2(1+x)$.
1429pub trait LogBase2Of1PlusXAssign {
1430    fn log_base_2_1_plus_x_assign(&mut self);
1431}
1432
1433/// Computes $\log_{2^k}(1+x)$.
1434pub trait LogBasePowerOf2Of1PlusX<POW> {
1435    type Output;
1436
1437    fn log_base_power_of_2_1_plus_x(self, pow: POW) -> Self::Output;
1438}
1439
1440/// Replaces a number $x$ by $\log_{2^k}(1+x)$.
1441pub trait LogBasePowerOf2Of1PlusXAssign<POW> {
1442    fn log_base_power_of_2_1_plus_x_assign(&mut self, pow: POW);
1443}
1444
1445/// Computes $\log_b(1+x)$ for an integer base $b$.
1446pub trait LogBaseOf1PlusX<B = Self> {
1447    type Output;
1448
1449    fn log_base_1_plus_x(self, base: B) -> Self::Output;
1450}
1451
1452/// Replaces a number $x$ by $\log_b(1+x)$ for an integer base $b$.
1453pub trait LogBaseOf1PlusXAssign<B = Self> {
1454    fn log_base_1_plus_x_assign(&mut self, base: B);
1455}
1456
1457/// Computes $\log_{10}(1+x)$.
1458pub trait LogBase10Of1PlusX {
1459    type Output;
1460
1461    fn log_base_10_1_plus_x(self) -> Self::Output;
1462}
1463
1464/// Replaces a number $x$ by $\log_{10}(1+x)$.
1465pub trait LogBase10Of1PlusXAssign {
1466    fn log_base_10_1_plus_x_assign(&mut self);
1467}
1468
1469/// Calculates the floor of the base-2 logarithm of a number.
1470pub trait FloorLogBase2 {
1471    type Output;
1472
1473    fn floor_log_base_2(self) -> Self::Output;
1474}
1475
1476/// Calculates the ceiling of the base-2 logarithm of a number.
1477pub trait CeilingLogBase2 {
1478    type Output;
1479
1480    fn ceiling_log_base_2(self) -> Self::Output;
1481}
1482
1483/// Calculates the base-$2^k$ logarithm of a number, or returns `None` if the number is not a
1484/// perfect power of $2^k$.
1485pub trait CheckedLogBasePowerOf2<POW> {
1486    type Output;
1487
1488    fn checked_log_base_power_of_2(self, pow: POW) -> Option<Self::Output>;
1489}
1490
1491/// Calculates the floor of the base-$2^k$ logarithm of a number.
1492pub trait FloorLogBasePowerOf2<POW> {
1493    type Output;
1494
1495    fn floor_log_base_power_of_2(self, pow: POW) -> Self::Output;
1496}
1497
1498/// Calculates the ceiling of the base-$2^k$ logarithm of a number.
1499pub trait CeilingLogBasePowerOf2<POW> {
1500    type Output;
1501
1502    fn ceiling_log_base_power_of_2(self, pow: POW) -> Self::Output;
1503}
1504
1505/// Calculates the base-$2^k$ logarithm of a number.
1506pub trait LogBasePowerOf2<POW> {
1507    type Output;
1508
1509    fn log_base_power_of_2(self, pow: POW) -> Self::Output;
1510}
1511
1512/// Replaces a number with its base-$2^k$ logarithm.
1513pub trait LogBasePowerOf2Assign<POW> {
1514    fn log_base_power_of_2_assign(&mut self, pow: POW);
1515}
1516
1517/// Calculates the base-$b$ logarithm of a number, rounding the (generally irrational) result.
1518pub trait LogBase<B = Self> {
1519    type Output;
1520
1521    fn log_base(self, base: B) -> Self::Output;
1522}
1523
1524/// Replaces a number with its base-$b$ logarithm, rounding the (generally irrational) result.
1525pub trait LogBaseAssign<B = Self> {
1526    fn log_base_assign(&mut self, base: B);
1527}
1528
1529/// Computes the $n$th Lucas number, either alone or paired with its predecessor:
1530/// `lucas_number_pair(n)` returns $(L(n), L(n-1))$.
1531pub trait LucasNumber: Sized {
1532    fn lucas_number(n: u64) -> Self;
1533
1534    fn lucas_number_pair(n: u64) -> (Self, Self);
1535}
1536
1537/// Computes the $n$th Lucas number, either alone or paired with its predecessor, returning `None`
1538/// if the result is too large to be represented.
1539pub trait CheckedLucasNumber: Sized {
1540    fn checked_lucas_number(n: u64) -> Option<Self>;
1541
1542    fn checked_lucas_number_pair(n: u64) -> Option<(Self, Self)>;
1543}
1544
1545/// Adds two numbers modulo a third number $m$. The inputs must be already reduced modulo $m$.
1546pub trait ModAdd<RHS = Self, M = Self> {
1547    type Output;
1548
1549    fn mod_add(self, other: RHS, m: M) -> Self::Output;
1550}
1551
1552/// Adds two numbers modulo a third number $m$, in place. The inputs must be already reduced modulo
1553/// $m$.
1554pub trait ModAddAssign<RHS = Self, M = Self> {
1555    fn mod_add_assign(&mut self, other: RHS, m: M);
1556}
1557
1558/// Divides a number by another number modulo a third number $m$, returning `None` if no quotient
1559/// exists. The inputs must be already reduced modulo $m$.
1560///
1561/// If the divisor is not invertible modulo $m$, a quotient may exist without being unique; in that
1562/// case one of the quotients is returned.
1563pub trait ModDiv<RHS = Self, M = Self> {
1564    type Output;
1565
1566    fn mod_div(self, other: RHS, m: M) -> Option<Self::Output>;
1567}
1568
1569/// Finds all quotients of a number and another number modulo a third number $m$, returning `None`
1570/// if no quotient exists. The inputs must be already reduced modulo $m$.
1571///
1572/// The quotients form an arithmetic progression: `Some((start, stride, length))` means that the
1573/// quotients are exactly the numbers $\text{start} + \text{stride} \cdot i$ for $0 \leq i <
1574/// \text{length}$, where `start` is the smallest quotient.
1575pub trait ModDivList<RHS = Self, M = Self> {
1576    type Output;
1577
1578    #[allow(clippy::type_complexity)]
1579    fn mod_div_list(self, other: RHS, m: M) -> Option<(Self::Output, Self::Output, Self::Output)>;
1580}
1581
1582/// Finds the multiplicative inverse of a number modulo another number $m$. The input must be
1583/// already reduced modulo $m$.
1584pub trait ModInverse<M = Self> {
1585    type Output;
1586
1587    fn mod_inverse(self, m: M) -> Option<Self::Output>;
1588}
1589
1590/// Checks whether a number is reduced modulo another number $m$.
1591pub trait ModIsReduced<M = Self> {
1592    fn mod_is_reduced(&self, m: &M) -> bool;
1593}
1594
1595/// Multiplies two numbers modulo a third number $m$. The inputs must be already reduced modulo $m$.
1596pub trait ModMul<RHS = Self, M = Self> {
1597    type Output;
1598
1599    fn mod_mul(self, other: RHS, m: M) -> Self::Output;
1600}
1601
1602/// Multiplies two numbers modulo a third number $m$, in place. The inputs must be already reduced
1603/// modulo $m$.
1604pub trait ModMulAssign<RHS = Self, M = Self> {
1605    fn mod_mul_assign(&mut self, other: RHS, m: M);
1606}
1607
1608/// Multiplies two numbers modulo a third number $m$. The inputs must be already reduced modulo $m$.
1609///
1610/// If multiple modular multiplications with the same modulus are necessary, it can be quicker to
1611/// precompute some piece of data and reuse it in the multiplication calls. This trait provides a
1612/// function for precomputing the data and a function for using it during multiplication.
1613pub trait ModMulPrecomputed<RHS = Self, M = Self> {
1614    type Output;
1615    type Data;
1616
1617    /// Precomputes some data to use for modular multiplication.
1618    fn precompute_mod_mul_data(m: &M) -> Self::Data;
1619
1620    fn mod_mul_precomputed(self, other: RHS, m: M, data: &Self::Data) -> Self::Output;
1621}
1622
1623/// Multiplies two numbers modulo a third number $m$, in place.The inputs must be already reduced
1624/// modulo $m$.
1625///
1626/// If multiple modular multiplications with the same modulus are necessary, it can be quicker to
1627/// precompute some piece of data and reuse it in the multiplication calls. This trait provides a
1628/// function for using precomputed data during multiplication. For precomputing the data, use the
1629/// [`precompute_mod_mul_data`](ModMulPrecomputed::precompute_mod_mul_data) function in
1630/// [`ModMulPrecomputed`].
1631pub trait ModMulPrecomputedAssign<RHS = Self, M = Self>: ModMulPrecomputed<RHS, M> {
1632    fn mod_mul_precomputed_assign(&mut self, other: RHS, m: M, data: &Self::Data);
1633}
1634
1635/// Negates a number modulo another number $m$. The input must be already reduced modulo $m$.
1636pub trait ModNeg<M = Self> {
1637    type Output;
1638
1639    fn mod_neg(self, m: M) -> Self::Output;
1640}
1641
1642/// Negates a number modulo another number $m$, in place. The input must be already reduced modulo
1643/// $m$.
1644pub trait ModNegAssign<M = Self> {
1645    fn mod_neg_assign(&mut self, m: M);
1646}
1647
1648/// Divides a number by another number, returning just the remainder. The remainder has the same
1649/// sign as the divisor (second number).
1650///
1651/// If the quotient were computed, the quotient and remainder would satisfy $x = qy + r$ and $0 \leq
1652/// |r| < |y|$.
1653pub trait Mod<RHS = Self> {
1654    type Output;
1655
1656    fn mod_op(self, other: RHS) -> Self::Output;
1657}
1658
1659/// Divides a number by another number, replacing the first number by the remainder. The remainder
1660/// has the same sign as the divisor (second number).
1661///
1662/// If the quotient were computed, the quotient and remainder would satisfy $x = qy + r$ and $0 \leq
1663/// |r| < |y|$.
1664pub trait ModAssign<RHS = Self> {
1665    fn mod_assign(&mut self, other: RHS);
1666}
1667
1668/// Divides a number by another number, returning the balanced remainder: the representative of the
1669/// first number modulo the second that is closest to zero.
1670///
1671/// The remainder $r$ satisfies $-|y|/2 < r \leq |y|/2$, so a remainder of exactly $|y|/2$ is
1672/// positive. It is congruent to $x$ modulo $y$, and those two properties determine it uniquely.
1673pub trait BalancedMod<RHS = Self> {
1674    type Output;
1675
1676    fn balanced_mod(self, other: RHS) -> Self::Output;
1677}
1678
1679/// Divides a number by another number, replacing the first number by the balanced remainder: the
1680/// representative of the first number modulo the second that is closest to zero.
1681///
1682/// The remainder $r$ satisfies $-|y|/2 < r \leq |y|/2$, so a remainder of exactly $|y|/2$ is
1683/// positive.
1684pub trait BalancedModAssign<RHS = Self> {
1685    fn balanced_mod_assign(&mut self, other: RHS);
1686}
1687
1688/// Divides a number by another number, returning just the remainder. The remainder is always
1689/// nonnegative.
1690///
1691/// If the quotient were computed, the quotient and remainder would satisfy $x = qy + r$ and $0 \leq
1692/// r < |y|$.
1693pub trait ModEuclidean<RHS = Self> {
1694    type Output;
1695
1696    fn mod_euclidean(self, other: RHS) -> Self::Output;
1697}
1698
1699/// Divides a number by another number, replacing the first number by the remainder. The remainder
1700/// is always nonnegative.
1701///
1702/// If the quotient were computed, the quotient and remainder would satisfy $x = qy + r$ and $0 \leq
1703/// r < |y|$.
1704pub trait ModEuclideanAssign<RHS = Self> {
1705    fn mod_euclidean_assign(&mut self, other: RHS);
1706}
1707
1708/// Divides the negative of a number by another number, returning the remainder.
1709///
1710/// If the quotient were computed, the quotient and remainder would satisfy $x = qy - r$ and $0 \leq
1711/// r < y$.
1712pub trait NegMod<RHS = Self> {
1713    type Output;
1714
1715    fn neg_mod(self, other: RHS) -> Self::Output;
1716}
1717
1718/// Divides the negative of a number by another number, replacing the first number by the remainder.
1719///
1720/// If the quotient were computed, the quotient and remainder would satisfy $x = qy - r$ and $0 \leq
1721/// r < y$.
1722pub trait NegModAssign<RHS = Self> {
1723    fn neg_mod_assign(&mut self, other: RHS);
1724}
1725
1726/// Divides a number by another number, returning just the remainder. The remainder has the opposite
1727/// sign as the divisor (second number).
1728///
1729/// If the quotient were computed, the quotient and remainder would satisfy $x = qy + r$ and $0 \leq
1730/// |r| < |y|$.
1731pub trait CeilingMod<RHS = Self> {
1732    type Output;
1733
1734    fn ceiling_mod(self, other: RHS) -> Self::Output;
1735}
1736
1737/// Divides a number by another number, replacing the first number by the remainder. The remainder
1738/// has the same sign as the divisor (second number).
1739///
1740/// If the quotient were computed, the quotient and remainder would satisfy $x = qy + r$ and $0 \leq
1741/// |r| < |y|$.
1742pub trait CeilingModAssign<RHS = Self> {
1743    fn ceiling_mod_assign(&mut self, other: RHS);
1744}
1745
1746/// Raises a number to a power modulo another number $m$. The base must be already reduced modulo
1747/// $m$.
1748pub trait ModPow<RHS = Self, M = Self> {
1749    type Output;
1750
1751    fn mod_pow(self, exp: RHS, m: M) -> Self::Output;
1752}
1753
1754/// Raises a number to a power modulo another number $m$, in place. The base must be already reduced
1755/// modulo $m$.
1756pub trait ModPowAssign<RHS = Self, M = Self> {
1757    fn mod_pow_assign(&mut self, exp: RHS, m: M);
1758}
1759
1760/// Raises a number to a power modulo another number $m$. The base must be already reduced modulo
1761/// $m$.
1762///
1763/// If multiple modular exponentiations with the same modulus are necessary, it can be quicker to
1764/// precompute some piece of data and reuse it in the exponentiation calls. This trait provides a
1765/// function for precomputing the data and a function for using it during exponentiation.
1766pub trait ModPowPrecomputed<RHS = Self, M = Self>
1767where
1768    Self: Sized,
1769{
1770    type Output;
1771    type Data;
1772
1773    /// Precomputes some data to use for modular exponentiation.
1774    fn precompute_mod_pow_data(m: &M) -> Self::Data;
1775
1776    fn mod_pow_precomputed(self, exp: RHS, m: M, data: &Self::Data) -> Self::Output;
1777}
1778
1779/// Raises a number to a power modulo another number $m$, in place. The base must be already reduced
1780/// modulo $m$.
1781///
1782/// If multiple modular exponentiations with the same modulus are necessary, it can be quicker to
1783/// precompute some piece of data and reuse it in the exponentiation calls. This trait provides a
1784/// function for using precomputed data during exponentiation. For precomputing the data, use the
1785/// [`precompute_mod_pow_data`](ModPowPrecomputed::precompute_mod_pow_data) function in
1786/// [`ModPowPrecomputed`].
1787pub trait ModPowPrecomputedAssign<RHS: Two = Self, M = Self>: ModPowPrecomputed<RHS, M> {
1788    fn mod_pow_precomputed_assign(&mut self, exp: RHS, m: M, data: &Self::Data);
1789}
1790
1791/// Adds two numbers modulo $2^k$. The inputs must be already reduced modulo $2^k$.
1792pub trait ModPowerOf2Add<RHS = Self> {
1793    type Output;
1794
1795    fn mod_power_of_2_add(self, other: RHS, pow: u64) -> Self::Output;
1796}
1797
1798/// Adds two numbers modulo $2^k$, in place. The inputs must be already reduced modulo $2^k$.
1799pub trait ModPowerOf2AddAssign<RHS = Self> {
1800    fn mod_power_of_2_add_assign(&mut self, other: RHS, pow: u64);
1801}
1802
1803/// Finds the multiplicative inverse of a number modulo $2^k$. The input must be already reduced
1804/// modulo $2^k$.
1805pub trait ModPowerOf2Inverse {
1806    type Output;
1807
1808    fn mod_power_of_2_inverse(self, pow: u64) -> Option<Self::Output>;
1809}
1810
1811/// Checks whether a number is reduced modulo $2^k$.
1812pub trait ModPowerOf2IsReduced {
1813    fn mod_power_of_2_is_reduced(&self, pow: u64) -> bool;
1814}
1815
1816/// Multiplies two numbers modulo $2^k$. The inputs must be already reduced modulo $2^k$.
1817pub trait ModPowerOf2Mul<RHS = Self> {
1818    type Output;
1819
1820    fn mod_power_of_2_mul(self, other: RHS, pow: u64) -> Self::Output;
1821}
1822
1823/// Multiplies two numbers modulo $2^k$, in place. The inputs must be already reduced modulo $2^k$.
1824pub trait ModPowerOf2MulAssign<RHS = Self> {
1825    fn mod_power_of_2_mul_assign(&mut self, other: RHS, pow: u64);
1826}
1827
1828/// Negates a number modulo $2^k$. The input must be already reduced modulo $2^k$.
1829pub trait ModPowerOf2Neg {
1830    type Output;
1831
1832    fn mod_power_of_2_neg(self, pow: u64) -> Self::Output;
1833}
1834
1835/// Negates a number modulo $2^k$ in place. The input must be already reduced modulo $2^k$.
1836pub trait ModPowerOf2NegAssign {
1837    fn mod_power_of_2_neg_assign(&mut self, pow: u64);
1838}
1839
1840/// Raises a number to a power modulo $2^k$. The base must be already reduced modulo $2^k$.
1841pub trait ModPowerOf2Pow<RHS = Self> {
1842    type Output;
1843
1844    fn mod_power_of_2_pow(self, exp: RHS, pow: u64) -> Self::Output;
1845}
1846
1847/// Raises a number to a power modulo $2^k$, in place. The base must be already reduced modulo
1848/// $2^k$.
1849pub trait ModPowerOf2PowAssign<RHS = Self> {
1850    fn mod_power_of_2_pow_assign(&mut self, exp: RHS, pow: u64);
1851}
1852
1853/// Left-shifts a number (multiplies it by a power of 2) modulo $2^k$. The number must be already
1854/// reduced modulo $2^k$.
1855pub trait ModPowerOf2Shl<RHS> {
1856    type Output;
1857
1858    fn mod_power_of_2_shl(self, other: RHS, pow: u64) -> Self::Output;
1859}
1860
1861/// Left-shifts a number (multiplies it by a power of 2) modulo $2^k$, in place. The number must be
1862/// already reduced modulo $2^k$.
1863pub trait ModPowerOf2ShlAssign<RHS> {
1864    fn mod_power_of_2_shl_assign(&mut self, other: RHS, pow: u64);
1865}
1866
1867/// Right-shifts a number (divides it by a power of 2) modulo $2^k$. The number must be already
1868/// reduced modulo $2^k$.
1869pub trait ModPowerOf2Shr<RHS> {
1870    type Output;
1871
1872    fn mod_power_of_2_shr(self, other: RHS, pow: u64) -> Self::Output;
1873}
1874
1875/// Right-shifts a number (divides it by a power of 2) modulo $2^k$, in place. The number must be
1876/// already reduced modulo $2^k$.
1877pub trait ModPowerOf2ShrAssign<RHS> {
1878    fn mod_power_of_2_shr_assign(&mut self, other: RHS, pow: u64);
1879}
1880
1881/// Squares a number modulo $2^k$. The input must be already reduced modulo $2^k$.
1882pub trait ModPowerOf2Square {
1883    type Output;
1884
1885    fn mod_power_of_2_square(self, pow: u64) -> Self::Output;
1886}
1887
1888/// Squares a number modulo $2^k$ in place. The input must be already reduced modulo $2^k$.
1889pub trait ModPowerOf2SquareAssign {
1890    fn mod_power_of_2_square_assign(&mut self, pow: u64);
1891}
1892
1893/// Subtracts two numbers modulo $2^k$. The inputs must be already reduced modulo $2^k$.
1894pub trait ModPowerOf2Sub<RHS = Self> {
1895    type Output;
1896
1897    fn mod_power_of_2_sub(self, other: RHS, pow: u64) -> Self::Output;
1898}
1899
1900/// Subtracts two numbers modulo $2^k$, in place. The inputs must be already reduced modulo $2^k$.
1901pub trait ModPowerOf2SubAssign<RHS = Self> {
1902    fn mod_power_of_2_sub_assign(&mut self, other: RHS, pow: u64);
1903}
1904
1905/// Divides a number by $2^k$, returning just the remainder. The remainder is non-negative.
1906///
1907/// If the quotient were computed, the quotient and remainder would satisfy $x = q2^k + r$ and $0
1908/// \leq r < 2^k$.
1909pub trait ModPowerOf2 {
1910    type Output;
1911
1912    fn mod_power_of_2(self, other: u64) -> Self::Output;
1913}
1914
1915/// Divides a number by $2^k$, replacing the number by the remainder. The remainder is non-negative.
1916///
1917/// If the quotient were computed, the quotient and remainder would satisfy $x = q2^k + r$ and $0
1918/// \leq r < 2^k$.
1919pub trait ModPowerOf2Assign {
1920    fn mod_power_of_2_assign(&mut self, other: u64);
1921}
1922
1923/// Divides a number by $2^k$, returning just the remainder. The remainder has the same sign as the
1924/// number.
1925///
1926/// If the quotient were computed, the quotient and remainder would satisfy $x = q2^k + r$ and $0
1927/// \leq |r| < 2^k$.
1928pub trait RemPowerOf2 {
1929    type Output;
1930
1931    fn rem_power_of_2(self, other: u64) -> Self::Output;
1932}
1933
1934/// Divides a number by $2^k$, replacing the number by the remainder. The remainder has the same
1935/// sign as the number.
1936///
1937/// If the quotient were computed, the quotient and remainder would satisfy $x = q2^k + r$ and $0
1938/// \leq |r| < 2^k$.
1939pub trait RemPowerOf2Assign {
1940    fn rem_power_of_2_assign(&mut self, other: u64);
1941}
1942
1943/// Divides the negative of a number by $2^k$, returning the remainder.
1944///
1945/// If the quotient were computed, the quotient and remainder would satisfy $x = q2^k - r$ and $0
1946/// \leq r < 2^k$.
1947pub trait NegModPowerOf2 {
1948    type Output;
1949
1950    fn neg_mod_power_of_2(self, other: u64) -> Self::Output;
1951}
1952
1953/// Divides the negative of a number by $2^k$, replacing the number by the remainder.
1954///
1955/// If the quotient were computed, the quotient and remainder would satisfy $x = q2^k - r$ and $0
1956/// \leq r < 2^k$.
1957pub trait NegModPowerOf2Assign {
1958    fn neg_mod_power_of_2_assign(&mut self, other: u64);
1959}
1960
1961/// Divides a number by $2^k$, returning just the remainder. The remainder is non-positive.
1962///
1963/// If the quotient were computed, the quotient and remainder would satisfy $x = q2^k + r$ and $0
1964/// \leq -r < 2^k$.
1965pub trait CeilingModPowerOf2 {
1966    type Output;
1967
1968    fn ceiling_mod_power_of_2(self, other: u64) -> Self::Output;
1969}
1970
1971/// Divides a number by $2^k$, replacing the number by the remainder. The remainder is non-positive.
1972///
1973/// If the quotient were computed, the quotient and remainder would satisfy $x = q2^k + r$ and $0
1974/// \leq -r < 2^k$.
1975pub trait CeilingModPowerOf2Assign {
1976    fn ceiling_mod_power_of_2_assign(&mut self, other: u64);
1977}
1978
1979/// Left-shifts a number (multiplies it by a power of 2) modulo another number $m$. The number must
1980/// be already reduced modulo $m$.
1981pub trait ModShl<RHS, M = Self> {
1982    type Output;
1983
1984    fn mod_shl(self, other: RHS, m: M) -> Self::Output;
1985}
1986
1987/// Left-shifts a number (multiplies it by a power of 2) modulo another number $m$, in place. The
1988/// number must be already reduced modulo $m$.
1989pub trait ModShlAssign<RHS, M = Self> {
1990    fn mod_shl_assign(&mut self, other: RHS, m: M);
1991}
1992
1993/// Right-shifts a number (divides it by a power of 2) modulo another number $m$. The number must be
1994/// already reduced modulo $m$.
1995pub trait ModShr<RHS, M = Self> {
1996    type Output;
1997
1998    fn mod_shr(self, other: RHS, m: M) -> Self::Output;
1999}
2000
2001/// Right-shifts a number (divides it by a power of 2) modulo another number $m$, in place. The
2002/// number must be already reduced modulo $m$.
2003pub trait ModShrAssign<RHS, M = Self> {
2004    fn mod_shr_assign(&mut self, other: RHS, m: M);
2005}
2006
2007/// Computes a square root of a number modulo another number $m$, returning `None` if no root is
2008/// found. The input must be already reduced modulo $m$.
2009///
2010/// The modulus should be an odd prime: for such moduli a root is found whenever one exists. The
2011/// behavior for other moduli is deterministic and never hangs, but a root may be missed, and a
2012/// returned value may fail to be a root.
2013pub trait ModSqrt<M = Self> {
2014    type Output;
2015
2016    fn mod_sqrt(self, m: M) -> Option<Self::Output>;
2017}
2018
2019/// Squares a number modulo another number $m$. The input must be already reduced modulo $m$.
2020pub trait ModSquare<M = Self> {
2021    type Output;
2022
2023    fn mod_square(self, m: M) -> Self::Output;
2024}
2025
2026/// Squares a number modulo another number $m$, in place. The input must be already reduced modulo
2027/// $m$.
2028pub trait ModSquareAssign<M = Self> {
2029    fn mod_square_assign(&mut self, m: M);
2030}
2031
2032/// Squares a number modulo another number $m$. The input must be already reduced modulo $m$.
2033///
2034/// If multiple modular squarings with the same modulus are necessary, it can be quicker to
2035/// precompute some piece of data using
2036/// [`precompute_mod_pow_data`](ModPowPrecomputed::precompute_mod_pow_data) function in
2037/// [`ModMulPrecomputed`] and reuse it in the squaring calls.
2038pub trait ModSquarePrecomputed<RHS = Self, M = Self>: ModPowPrecomputed<RHS, M>
2039where
2040    Self: Sized,
2041{
2042    fn mod_square_precomputed(self, m: M, data: &Self::Data) -> Self::Output;
2043}
2044
2045/// Squares a number modulo another number $m$, in place. The input must be already reduced modulo
2046/// $m$.
2047///
2048/// If multiple modular squarings with the same modulus are necessary, it can be quicker to
2049/// precompute some piece of data using
2050/// [`precompute_mod_pow_data`](ModPowPrecomputed::precompute_mod_pow_data) function in
2051/// [`ModMulPrecomputed`] and reuse it in the squaring calls.
2052pub trait ModSquarePrecomputedAssign<RHS = Self, M = Self>: ModPowPrecomputed<RHS, M> {
2053    fn mod_square_precomputed_assign(&mut self, m: M, data: &Self::Data);
2054}
2055
2056/// Adds two numbers modulo a third number $m$. The inputs must be already reduced modulo $m$.
2057pub trait ModSub<RHS = Self, M = Self> {
2058    type Output;
2059
2060    fn mod_sub(self, other: RHS, m: M) -> Self::Output;
2061}
2062
2063/// Adds two numbers modulo a third number $m$, in place. The inputs must be already reduced modulo
2064/// $m$.
2065pub trait ModSubAssign<RHS = Self, M = Self> {
2066    fn mod_sub_assign(&mut self, other: RHS, m: M);
2067}
2068
2069/// Replaces a number with its negative. Assumes the result is representable.
2070pub trait NegAssign {
2071    fn neg_assign(&mut self);
2072}
2073
2074/// Returns the smallest power of 2 greater than or equal to a number. Assumes the result is
2075/// representable.
2076pub trait NextPowerOf2 {
2077    type Output;
2078
2079    fn next_power_of_2(self) -> Self::Output;
2080}
2081
2082/// Replaces a number with the smallest power of 2 greater than or equal it. Assumes the result is
2083/// representable.
2084pub trait NextPowerOf2Assign {
2085    fn next_power_of_2_assign(&mut self);
2086}
2087
2088/// Takes the absolute value of a number.
2089///
2090/// Returns a tuple of the result along with a boolean indicating whether an arithmetic overflow
2091/// occurred. If an overflow occurred, then the wrapped number is returned.
2092pub trait OverflowingAbs {
2093    type Output;
2094
2095    fn overflowing_abs(self) -> (Self::Output, bool);
2096}
2097
2098/// Replaces a number with its absolute value.
2099///
2100/// Returns a boolean indicating whether an arithmetic overflow occurred. If an overflow occurred,
2101/// then the wrapped number is assigned.
2102pub trait OverflowingAbsAssign {
2103    fn overflowing_abs_assign(&mut self) -> bool;
2104}
2105
2106/// Adds two numbers.
2107///
2108/// Returns a tuple of the sum along with a boolean indicating whether an arithmetic overflow
2109/// occurred. If an overflow occurred, then the wrapped number is returned.
2110pub trait OverflowingAdd<RHS = Self> {
2111    type Output;
2112
2113    fn overflowing_add(self, other: RHS) -> (Self::Output, bool);
2114}
2115
2116/// Adds a number to another number in place.
2117///
2118/// Returns a boolean indicating whether an arithmetic overflow occurred. If an overflow occurred,
2119/// then the wrapped number is assigned.
2120pub trait OverflowingAddAssign<RHS = Self> {
2121    fn overflowing_add_assign(&mut self, other: RHS) -> bool;
2122}
2123
2124/// Adds a number and the product of two other numbers.
2125///
2126/// Returns a tuple of the result along with a boolean indicating whether an arithmetic overflow
2127/// occurred. If an overflow occurred, then the wrapped number is returned.
2128pub trait OverflowingAddMul<Y = Self, Z = Self> {
2129    type Output;
2130
2131    fn overflowing_add_mul(self, y: Y, z: Z) -> (Self::Output, bool);
2132}
2133
2134/// Adds a number and the product of two other numbers, in place.
2135///
2136/// Returns a tuple of the result along with a boolean indicating whether an arithmetic overflow
2137/// occurred. If an overflow occurred, then the wrapped number is returned.
2138pub trait OverflowingAddMulAssign<Y = Self, Z = Self> {
2139    fn overflowing_add_mul_assign(&mut self, y: Y, z: Z) -> bool;
2140}
2141
2142/// Adds the products of two pairs of numbers.
2143///
2144/// Returns a tuple of the result along with a boolean indicating whether an arithmetic overflow
2145/// occurred. If an overflow occurred, then the wrapped result is returned.
2146pub trait OverflowingMulAddMul<Y = Self, Z = Self, W = Self> {
2147    type Output;
2148
2149    fn overflowing_mul_add_mul(self, y: Y, z: Z, w: W) -> (Self::Output, bool);
2150}
2151
2152/// Adds the products of two pairs of numbers, in place.
2153///
2154/// Returns a boolean indicating whether an arithmetic overflow occurred. If an overflow occurred,
2155/// then the wrapped result is assigned.
2156pub trait OverflowingMulAddMulAssign<Y = Self, Z = Self, W = Self> {
2157    fn overflowing_mul_add_mul_assign(&mut self, y: Y, z: Z, w: W) -> bool;
2158}
2159
2160/// Subtracts the product of one pair of numbers from the product of another.
2161///
2162/// Returns a tuple of the result along with a boolean indicating whether an arithmetic overflow
2163/// occurred. If an overflow occurred, then the wrapped result is returned.
2164pub trait OverflowingMulSubMul<Y = Self, Z = Self, W = Self> {
2165    type Output;
2166
2167    fn overflowing_mul_sub_mul(self, y: Y, z: Z, w: W) -> (Self::Output, bool);
2168}
2169
2170/// Subtracts the product of one pair of numbers from the product of another, in place.
2171///
2172/// Returns a boolean indicating whether an arithmetic overflow occurred. If an overflow occurred,
2173/// then the wrapped result is assigned.
2174pub trait OverflowingMulSubMulAssign<Y = Self, Z = Self, W = Self> {
2175    fn overflowing_mul_sub_mul_assign(&mut self, y: Y, z: Z, w: W) -> bool;
2176}
2177
2178/// Divides two numbers.
2179///
2180/// Returns a tuple of the sum along with a boolean indicating whether an arithmetic overflow
2181/// occurred. If an overflow occurred, then the wrapped number is returned.
2182pub trait OverflowingDiv<RHS = Self> {
2183    type Output;
2184
2185    fn overflowing_div(self, other: RHS) -> (Self::Output, bool);
2186}
2187
2188/// Divides a number by another number in place.
2189///
2190/// Returns a boolean indicating whether an arithmetic overflow occurred. If an overflow occurred,
2191/// then the wrapped number is assigned.
2192pub trait OverflowingDivAssign<RHS = Self> {
2193    fn overflowing_div_assign(&mut self, other: RHS) -> bool;
2194}
2195
2196/// Multiplies two numbers.
2197///
2198/// Returns a tuple of the sum along with a boolean indicating whether an arithmetic overflow
2199/// occurred. If an overflow occurred, then the wrapped number is returned.
2200pub trait OverflowingMul<RHS = Self> {
2201    type Output;
2202
2203    fn overflowing_mul(self, other: RHS) -> (Self::Output, bool);
2204}
2205
2206/// Multiplies a number by another number in place.
2207///
2208/// Returns a boolean indicating whether an arithmetic overflow occurred. If an overflow occurred,
2209/// then the wrapped number is assigned.
2210pub trait OverflowingMulAssign<RHS = Self> {
2211    fn overflowing_mul_assign(&mut self, other: RHS) -> bool;
2212}
2213
2214/// Negates a number.
2215///
2216/// Returns a tuple of the sum along with a boolean indicating whether an arithmetic overflow
2217/// occurred. If an overflow occurred, then the wrapped number is returned.
2218pub trait OverflowingNeg {
2219    type Output;
2220
2221    fn overflowing_neg(self) -> (Self::Output, bool);
2222}
2223
2224/// Negates a number in place.
2225///
2226/// Returns a boolean indicating whether an arithmetic overflow occurred. If an overflow occurred,
2227/// then the wrapped number is assigned.
2228pub trait OverflowingNegAssign {
2229    fn overflowing_neg_assign(&mut self) -> bool;
2230}
2231
2232/// Raises a number to a power.
2233///
2234/// Returns a tuple of the sum along with a boolean indicating whether an arithmetic overflow
2235/// occurred. If an overflow occurred, then the wrapped number is returned.
2236pub trait OverflowingPow<RHS> {
2237    type Output;
2238
2239    fn overflowing_pow(self, exp: RHS) -> (Self::Output, bool);
2240}
2241
2242/// Raises a number to a power in place.
2243///
2244/// Returns a boolean indicating whether an arithmetic overflow occurred. If an overflow occurred,
2245/// then the wrapped number is assigned.
2246pub trait OverflowingPowAssign<RHS = Self> {
2247    fn overflowing_pow_assign(&mut self, exp: RHS) -> bool;
2248}
2249
2250/// Squares a number.
2251///
2252/// Returns a tuple of the sum along with a boolean indicating whether an arithmetic overflow
2253/// occurred. If an overflow occurred, then the wrapped number is returned.
2254pub trait OverflowingSquare {
2255    type Output;
2256
2257    fn overflowing_square(self) -> (Self::Output, bool);
2258}
2259
2260/// Squares a number in place.
2261///
2262/// Returns a boolean indicating whether an arithmetic overflow occurred. If an overflow occurred,
2263/// then the wrapped number is assigned.
2264pub trait OverflowingSquareAssign {
2265    fn overflowing_square_assign(&mut self) -> bool;
2266}
2267
2268/// Subtracts two numbers.
2269///
2270/// Returns a tuple of the sum along with a boolean indicating whether an arithmetic overflow
2271/// occurred. If an overflow occurred, then the wrapped number is returned.
2272pub trait OverflowingSub<RHS = Self> {
2273    type Output;
2274
2275    fn overflowing_sub(self, other: RHS) -> (Self::Output, bool);
2276}
2277
2278/// Subtracts a number by another number in place.
2279///
2280/// Returns a boolean indicating whether an arithmetic overflow occurred. If an overflow occurred,
2281/// then the wrapped number is assigned.
2282pub trait OverflowingSubAssign<RHS = Self> {
2283    fn overflowing_sub_assign(&mut self, other: RHS) -> bool;
2284}
2285
2286/// Subtracts a number by the product of two other numbers.
2287///
2288/// Returns a tuple of the result along with a boolean indicating whether an arithmetic overflow
2289/// occurred. If an overflow occurred, then the wrapped number is returned.
2290pub trait OverflowingSubMul<Y = Self, Z = Self> {
2291    type Output;
2292
2293    fn overflowing_sub_mul(self, y: Y, z: Z) -> (Self::Output, bool);
2294}
2295
2296/// Subtracts a number by the product of two other numbers, in place.
2297///
2298/// Returns a tuple of the result along with a boolean indicating whether an arithmetic overflow
2299/// occurred. If an overflow occurred, then the wrapped number is returned.
2300pub trait OverflowingSubMulAssign<Y = Self, Z = Self> {
2301    fn overflowing_sub_mul_assign(&mut self, y: Y, z: Z) -> bool;
2302}
2303
2304/// Determines whether a number is even or odd.
2305pub trait Parity {
2306    /// Determines whether a number is even.
2307    fn even(self) -> bool;
2308
2309    /// Determines whether a number is odd.
2310    fn odd(self) -> bool;
2311}
2312
2313/// Raises a number to a power. Assumes the result is representable.
2314pub trait Pow<RHS> {
2315    type Output;
2316
2317    fn pow(self, exp: RHS) -> Self::Output;
2318}
2319
2320/// Raises a number to a power in place. Assumes the result is representable.
2321pub trait PowAssign<RHS = Self> {
2322    fn pow_assign(&mut self, exp: RHS);
2323}
2324
2325/// Raises 2 to a power.
2326pub trait PowerOf2<POW> {
2327    fn power_of_2(pow: POW) -> Self;
2328}
2329
2330/// Replaces a number with 2 raised to the power of that number.
2331pub trait PowerOf2Assign {
2332    fn power_of_2_assign(&mut self);
2333}
2334
2335/// Raises 10 to a power.
2336pub trait PowerOf10<POW> {
2337    fn power_of_10(pow: POW) -> Self;
2338}
2339
2340/// Replaces a number with 10 raised to the power of that number.
2341pub trait PowerOf10Assign {
2342    fn power_of_10_assign(&mut self);
2343}
2344
2345pub trait Primorial {
2346    fn primorial(n: u64) -> Self;
2347
2348    fn product_of_first_n_primes(n: u64) -> Self;
2349}
2350
2351pub trait CheckedPrimorial: Sized {
2352    fn checked_primorial(n: u64) -> Option<Self>;
2353
2354    fn checked_product_of_first_n_primes(n: u64) -> Option<Self>;
2355}
2356
2357/// Finds the reciprocal (multiplicative inverse) of a number.
2358pub trait Reciprocal {
2359    type Output;
2360
2361    fn reciprocal(self) -> Self::Output;
2362}
2363
2364/// Replaces a number with its reciprocal (multiplicative inverse).
2365pub trait ReciprocalAssign {
2366    fn reciprocal_assign(&mut self);
2367}
2368
2369/// Takes the reciprocal of the square root of a number.
2370pub trait ReciprocalSqrt {
2371    type Output;
2372
2373    fn reciprocal_sqrt(self) -> Self::Output;
2374}
2375
2376/// Replaces a number with the reciprocal of its square root.
2377pub trait ReciprocalSqrtAssign {
2378    fn reciprocal_sqrt_assign(&mut self);
2379}
2380
2381/// Finds the floor of the $n$th root of a number.
2382pub trait FloorRoot<POW> {
2383    type Output;
2384
2385    fn floor_root(self, pow: POW) -> Self::Output;
2386}
2387
2388/// Replaces a number with the floor of its $n$th root.
2389pub trait FloorRootAssign<POW> {
2390    fn floor_root_assign(&mut self, pow: POW);
2391}
2392
2393/// Finds the ceiling of the $n$th root of a number.
2394pub trait CeilingRoot<POW> {
2395    type Output;
2396
2397    fn ceiling_root(self, pow: POW) -> Self::Output;
2398}
2399
2400/// Replaces a number with the ceiling of its $n$th root.
2401pub trait CeilingRootAssign<POW> {
2402    fn ceiling_root_assign(&mut self, pow: POW);
2403}
2404
2405/// Finds the $n$th root of a number, returning `None` if it is not a perfect $n$th power.
2406pub trait CheckedRoot<POW> {
2407    type Output;
2408
2409    fn checked_root(self, pow: POW) -> Option<Self::Output>;
2410}
2411
2412/// Finds the floor of the $n$th root of a number, returning both the root and the remainder.
2413pub trait RootRem<POW> {
2414    type RootOutput;
2415    type RemOutput;
2416
2417    fn root_rem(self, exp: POW) -> (Self::RootOutput, Self::RemOutput);
2418}
2419
2420/// Replaces a number with the floor of its $n$th root, returning the remainder.
2421pub trait RootAssignRem<POW> {
2422    type RemOutput;
2423
2424    fn root_assign_rem(&mut self, exp: POW) -> Self::RemOutput;
2425}
2426
2427/// Takes the $n$th root of a number.
2428pub trait Root<POW> {
2429    type Output;
2430
2431    fn root(self, pow: POW) -> Self::Output;
2432}
2433
2434/// Replaces a number with its $n$th root.
2435pub trait RootAssign<POW> {
2436    fn root_assign(&mut self, pow: POW);
2437}
2438
2439/// Takes the cube root of a number.
2440pub trait Cbrt {
2441    type Output;
2442
2443    fn cbrt(self) -> Self::Output;
2444}
2445
2446/// Replaces a number with its cube root.
2447pub trait CbrtAssign {
2448    fn cbrt_assign(&mut self);
2449}
2450
2451/// Rotates a number left, inserting the leftmost bits into the right end.
2452pub trait RotateLeft {
2453    type Output;
2454
2455    fn rotate_left(self, n: u64) -> Self::Output;
2456}
2457
2458/// Rotates a number left, inserting the leftmost bits into the right end, in place.
2459pub trait RotateLeftAssign {
2460    fn rotate_left_assign(&mut self, n: u64);
2461}
2462
2463/// Rotates a number right, inserting the leftmost bits into the left end.
2464pub trait RotateRight {
2465    type Output;
2466
2467    fn rotate_right(self, n: u64) -> Self::Output;
2468}
2469
2470/// Rotates a number right, inserting the leftmost bits into the left end, in place.
2471pub trait RotateRightAssign {
2472    fn rotate_right_assign(&mut self, n: u64);
2473}
2474
2475/// Rounds a number to a multiple of another number, according to a specified rounding mode. An
2476/// [`Ordering`] is also returned, indicating whether the returned value is less than, equal to, or
2477/// greater than the original value.
2478pub trait RoundToMultiple<RHS = Self> {
2479    type Output;
2480
2481    fn round_to_multiple(self, other: RHS, rm: RoundingMode) -> (Self::Output, Ordering);
2482}
2483
2484/// Rounds a number to a multiple of another number in place, according to a specified rounding
2485/// mode. [`Ordering`] is returned, indicating whether the returned value is less than, equal to, or
2486/// greater than the original value.
2487pub trait RoundToMultipleAssign<RHS = Self> {
2488    fn round_to_multiple_assign(&mut self, other: RHS, rm: RoundingMode) -> Ordering;
2489}
2490
2491/// Rounds a number to a multiple of $2^k$, according to a specified rounding mode. An [`Ordering`]
2492/// is also returned, indicating whether the returned value is less than, equal to, or greater than
2493/// the original value.
2494pub trait RoundToMultipleOfPowerOf2<RHS> {
2495    type Output;
2496
2497    fn round_to_multiple_of_power_of_2(
2498        self,
2499        pow: RHS,
2500        rm: RoundingMode,
2501    ) -> (Self::Output, Ordering);
2502}
2503
2504/// Rounds a number to a multiple of $2^k$ in place, according to a specified rounding mode. An
2505/// [`Ordering`] is returned, indicating whether the returned value is less than, equal to, or
2506/// greater than the original value.
2507pub trait RoundToMultipleOfPowerOf2Assign<RHS> {
2508    fn round_to_multiple_of_power_of_2_assign(&mut self, pow: RHS, rm: RoundingMode) -> Ordering;
2509}
2510
2511/// Takes the absolute value of a number, saturating at the numeric bounds instead of overflowing.
2512pub trait SaturatingAbs {
2513    type Output;
2514
2515    fn saturating_abs(self) -> Self::Output;
2516}
2517
2518/// Replaces a number with its absolute value, saturating at the numeric bounds instead of
2519/// overflowing.
2520pub trait SaturatingAbsAssign {
2521    fn saturating_abs_assign(&mut self);
2522}
2523
2524/// Adds two numbers, saturating at the numeric bounds instead of overflowing.
2525pub trait SaturatingAdd<RHS = Self> {
2526    type Output;
2527
2528    fn saturating_add(self, other: RHS) -> Self::Output;
2529}
2530
2531/// Add a number to another number in place, saturating at the numeric bounds instead of
2532/// overflowing.
2533pub trait SaturatingAddAssign<RHS = Self> {
2534    fn saturating_add_assign(&mut self, other: RHS);
2535}
2536
2537/// Adds a number and the product of two other numbers, saturating at the numeric bounds instead of
2538/// overflowing.
2539pub trait SaturatingAddMul<Y = Self, Z = Self> {
2540    type Output;
2541
2542    fn saturating_add_mul(self, y: Y, z: Z) -> Self::Output;
2543}
2544
2545/// Adds a number and the product of two other numbers in place, saturating at the numeric bounds
2546/// instead of overflowing.
2547pub trait SaturatingAddMulAssign<Y = Self, Z = Self> {
2548    fn saturating_add_mul_assign(&mut self, y: Y, z: Z);
2549}
2550
2551/// Adds the products of two pairs of numbers, saturating at the numeric bounds instead of
2552/// overflowing.
2553pub trait SaturatingMulAddMul<Y = Self, Z = Self, W = Self> {
2554    type Output;
2555
2556    fn saturating_mul_add_mul(self, y: Y, z: Z, w: W) -> Self::Output;
2557}
2558
2559/// Adds the products of two pairs of numbers, in place, saturating at the numeric bounds instead of
2560/// overflowing.
2561pub trait SaturatingMulAddMulAssign<Y = Self, Z = Self, W = Self> {
2562    fn saturating_mul_add_mul_assign(&mut self, y: Y, z: Z, w: W);
2563}
2564
2565/// Subtracts the product of one pair of numbers from the product of another, saturating at the
2566/// numeric bounds instead of overflowing.
2567pub trait SaturatingMulSubMul<Y = Self, Z = Self, W = Self> {
2568    type Output;
2569
2570    fn saturating_mul_sub_mul(self, y: Y, z: Z, w: W) -> Self::Output;
2571}
2572
2573/// Subtracts the product of one pair of numbers from the product of another, in place, saturating
2574/// at the numeric bounds instead of overflowing.
2575pub trait SaturatingMulSubMulAssign<Y = Self, Z = Self, W = Self> {
2576    fn saturating_mul_sub_mul_assign(&mut self, y: Y, z: Z, w: W);
2577}
2578
2579/// Multiplies two numbers, saturating at the numeric bounds instead of overflowing.
2580pub trait SaturatingMul<RHS = Self> {
2581    type Output;
2582
2583    fn saturating_mul(self, other: RHS) -> Self::Output;
2584}
2585
2586/// Multiplies a number by another number in place, saturating at the numeric bounds instead of
2587/// overflowing.
2588pub trait SaturatingMulAssign<RHS = Self> {
2589    fn saturating_mul_assign(&mut self, other: RHS);
2590}
2591
2592/// Negates a number, saturating at the numeric bounds instead of overflowing.
2593pub trait SaturatingNeg {
2594    type Output;
2595
2596    fn saturating_neg(self) -> Self::Output;
2597}
2598
2599/// Negates a number in place, saturating at the numeric bounds instead of overflowing.
2600pub trait SaturatingNegAssign {
2601    fn saturating_neg_assign(&mut self);
2602}
2603
2604/// Raises a number to a power, saturating at the numeric bounds instead of overflowing.
2605pub trait SaturatingPow<RHS> {
2606    type Output;
2607
2608    fn saturating_pow(self, exp: RHS) -> Self::Output;
2609}
2610
2611/// Raises a number to a power in place, saturating at the numeric bounds instead of overflowing.
2612pub trait SaturatingPowAssign<RHS = Self> {
2613    fn saturating_pow_assign(&mut self, exp: RHS);
2614}
2615
2616/// Squares a number, saturating at the numeric bounds instead of overflowing.
2617pub trait SaturatingSquare {
2618    type Output;
2619
2620    fn saturating_square(self) -> Self::Output;
2621}
2622
2623/// Squares a number in place, saturating at the numeric bounds instead of overflowing.
2624pub trait SaturatingSquareAssign {
2625    fn saturating_square_assign(&mut self);
2626}
2627
2628/// Subtracts two numbers, saturating at the numeric bounds instead of overflowing.
2629pub trait SaturatingSub<RHS = Self> {
2630    type Output;
2631
2632    fn saturating_sub(self, other: RHS) -> Self::Output;
2633}
2634
2635/// Subtracts a number by another number in place, saturating at the numeric bounds instead of
2636/// overflowing.
2637pub trait SaturatingSubAssign<RHS = Self> {
2638    fn saturating_sub_assign(&mut self, other: RHS);
2639}
2640
2641/// Subtracts a number by the product of two other numbers, saturating at the numeric bounds instead
2642/// of overflowing.
2643pub trait SaturatingSubMul<Y = Self, Z = Self> {
2644    type Output;
2645
2646    fn saturating_sub_mul(self, y: Y, z: Z) -> Self::Output;
2647}
2648
2649/// Subtracts a number by the product of two other numbers in place, saturating at the numeric
2650/// bounds instead of overflowing.
2651pub trait SaturatingSubMulAssign<Y = Self, Z = Self> {
2652    fn saturating_sub_mul_assign(&mut self, y: Y, z: Z);
2653}
2654
2655/// Left-shifts a number (multiplies it by a power of 2), rounding the result according to a
2656/// specified rounding mode. An [`Ordering`] is also returned, indicating whether the returned value
2657/// is less than, equal to, or greater than the exact value.
2658///
2659/// Rounding might only be necessary if `other` is negative.
2660pub trait ShlRound<RHS> {
2661    type Output;
2662
2663    fn shl_round(self, other: RHS, rm: RoundingMode) -> (Self::Output, Ordering);
2664}
2665
2666/// Left-shifts a number (multiplies it by a power of 2) in place, rounding the result according to
2667/// a specified rounding mode. An [`Ordering`] is also returned, indicating whether the assigned
2668/// value is less than, equal to, or greater than the exact value.
2669///
2670/// Rounding might only be necessary if `other` is negative.
2671pub trait ShlRoundAssign<RHS> {
2672    fn shl_round_assign(&mut self, other: RHS, rm: RoundingMode) -> Ordering;
2673}
2674
2675/// Right-shifts a number (divides it by a power of 2), rounding the result according to a specified
2676/// rounding mode. An [`Ordering`] is also returned, indicating whether the returned value is less
2677/// than, equal to, or greater than the exact value.
2678///
2679/// Rounding might only be necessary if `other` is positive.
2680pub trait ShrRound<RHS> {
2681    type Output;
2682
2683    fn shr_round(self, other: RHS, rm: RoundingMode) -> (Self::Output, Ordering);
2684}
2685
2686/// Right-shifts a number (divides it by a power of 2) in place, rounding the result according to a
2687/// specified rounding mode. An [`Ordering`] is also returned, indicating whether the assigned value
2688/// is less than, equal to, or greater than the exact value.
2689///
2690/// Rounding might only be necessary if `other` is positive.
2691pub trait ShrRoundAssign<RHS> {
2692    fn shr_round_assign(&mut self, other: RHS, rm: RoundingMode) -> Ordering;
2693}
2694
2695/// Returns `Greater`, `Equal`, or `Less`, depending on whether a number is positive, zero, or
2696/// negative, respectively.
2697pub trait Sign {
2698    fn sign(&self) -> Ordering;
2699}
2700
2701/// Takes the square root of a number.
2702pub trait Sqrt {
2703    type Output;
2704
2705    fn sqrt(self) -> Self::Output;
2706}
2707
2708/// Replaces a number with its square root.
2709pub trait SqrtAssign {
2710    fn sqrt_assign(&mut self);
2711}
2712
2713/// Finds the floor of the square root of a number.
2714pub trait FloorSqrt {
2715    type Output;
2716
2717    fn floor_sqrt(self) -> Self::Output;
2718}
2719
2720/// Replaces a number with the floor of its square root.
2721pub trait FloorSqrtAssign {
2722    fn floor_sqrt_assign(&mut self);
2723}
2724
2725/// Finds the ceiling of the square root of a number.
2726pub trait CeilingSqrt {
2727    type Output;
2728
2729    fn ceiling_sqrt(self) -> Self::Output;
2730}
2731
2732/// Replaces a number with the ceiling of its square root.
2733pub trait CeilingSqrtAssign {
2734    fn ceiling_sqrt_assign(&mut self);
2735}
2736
2737/// Finds the square root of a number, returning `None` if it is not a perfect square.
2738pub trait CheckedSqrt {
2739    type Output;
2740
2741    fn checked_sqrt(self) -> Option<Self::Output>;
2742}
2743
2744/// Finds the floor of the square root of a number, returning both the root and the remainder.
2745pub trait SqrtRem {
2746    type SqrtOutput;
2747    type RemOutput;
2748
2749    fn sqrt_rem(self) -> (Self::SqrtOutput, Self::RemOutput);
2750}
2751
2752/// Replaces a number with the floor of its square root, returning the remainder.
2753pub trait SqrtAssignRem {
2754    type RemOutput;
2755
2756    fn sqrt_assign_rem(&mut self) -> Self::RemOutput;
2757}
2758
2759/// Squares a number.
2760pub trait Square {
2761    type Output;
2762
2763    fn square(self) -> Self::Output;
2764}
2765
2766/// Replaces a number with its square.
2767pub trait SquareAssign {
2768    fn square_assign(&mut self);
2769}
2770
2771/// Subtracts a number by the product of two other numbers.
2772///
2773/// Depending on the implementing type, the fused operation may compute the same value as the
2774/// unfused `self - y * z` more efficiently; or, for types with rounding, it may compute a *more
2775/// accurate* value -- the product enters the subtraction exactly, with a single rounding at the end
2776/// -- but *less* efficiently, since the exact product must be computed in full. See each
2777/// implementation's documentation for which contract it provides.
2778pub trait SubMul<Y = Self, Z = Self> {
2779    type Output;
2780
2781    fn sub_mul(self, y: Y, z: Z) -> Self::Output;
2782}
2783
2784/// Subtracts a number by the product of two other numbers, in place.
2785///
2786/// Depending on the implementing type, the fused operation may compute the same value as the
2787/// unfused `*self - y * z` more efficiently; or, for types with rounding, it may compute a *more
2788/// accurate* value -- the product enters the subtraction exactly, with a single rounding at the end
2789/// -- but *less* efficiently, since the exact product must be computed in full. See each
2790/// implementation's documentation for which contract it provides.
2791pub trait SubMulAssign<Y = Self, Z = Self> {
2792    fn sub_mul_assign(&mut self, y: Y, z: Z);
2793}
2794
2795/// Takes the absolute value of a number, wrapping around at the boundary of the type.
2796pub trait WrappingAbs {
2797    type Output;
2798
2799    fn wrapping_abs(self) -> Self::Output;
2800}
2801
2802/// Replaces a number with its absolute value, wrapping around at the boundary of the type.
2803pub trait WrappingAbsAssign {
2804    fn wrapping_abs_assign(&mut self);
2805}
2806
2807/// Adds two numbers, wrapping around at the boundary of the type.
2808pub trait WrappingAdd<RHS = Self> {
2809    type Output;
2810
2811    fn wrapping_add(self, other: RHS) -> Self::Output;
2812}
2813
2814/// Adds a number to another number in place, wrapping around at the boundary of the type.
2815pub trait WrappingAddAssign<RHS = Self> {
2816    fn wrapping_add_assign(&mut self, other: RHS);
2817}
2818
2819/// Adds a number and the product of two other numbers, wrapping around at the boundary of the type.
2820pub trait WrappingAddMul<Y = Self, Z = Self> {
2821    type Output;
2822
2823    fn wrapping_add_mul(self, y: Y, z: Z) -> Self::Output;
2824}
2825
2826/// Adds a number and the product of two other numbers, in place, wrapping around at the boundary of
2827/// the type.
2828pub trait WrappingAddMulAssign<Y = Self, Z = Self> {
2829    fn wrapping_add_mul_assign(&mut self, y: Y, z: Z);
2830}
2831
2832/// Adds the products of two pairs of numbers, wrapping around at the boundary of the type.
2833pub trait WrappingMulAddMul<Y = Self, Z = Self, W = Self> {
2834    type Output;
2835
2836    fn wrapping_mul_add_mul(self, y: Y, z: Z, w: W) -> Self::Output;
2837}
2838
2839/// Adds the products of two pairs of numbers, in place, wrapping around at the boundary of the
2840/// type.
2841pub trait WrappingMulAddMulAssign<Y = Self, Z = Self, W = Self> {
2842    fn wrapping_mul_add_mul_assign(&mut self, y: Y, z: Z, w: W);
2843}
2844
2845/// Subtracts the product of one pair of numbers from the product of another, wrapping around at the
2846/// boundary of the type.
2847pub trait WrappingMulSubMul<Y = Self, Z = Self, W = Self> {
2848    type Output;
2849
2850    fn wrapping_mul_sub_mul(self, y: Y, z: Z, w: W) -> Self::Output;
2851}
2852
2853/// Subtracts the product of one pair of numbers from the product of another, in place, wrapping
2854/// around at the boundary of the type.
2855pub trait WrappingMulSubMulAssign<Y = Self, Z = Self, W = Self> {
2856    fn wrapping_mul_sub_mul_assign(&mut self, y: Y, z: Z, w: W);
2857}
2858
2859/// Divides a number by another number, wrapping around at the boundary of the type.
2860pub trait WrappingDiv<RHS = Self> {
2861    type Output;
2862
2863    fn wrapping_div(self, other: RHS) -> Self::Output;
2864}
2865
2866/// Divides a number by another number in place, wrapping around at the boundary of the type.
2867pub trait WrappingDivAssign<RHS = Self> {
2868    fn wrapping_div_assign(&mut self, other: RHS);
2869}
2870
2871/// Multiplies two numbers, wrapping around at the boundary of the type.
2872pub trait WrappingMul<RHS = Self> {
2873    type Output;
2874
2875    fn wrapping_mul(self, other: RHS) -> Self::Output;
2876}
2877
2878/// Multiplies a number by another number in place, wrapping around at the boundary of the type.
2879pub trait WrappingMulAssign<RHS = Self> {
2880    fn wrapping_mul_assign(&mut self, other: RHS);
2881}
2882
2883/// Negates a number, wrapping around at the boundary of the type.
2884pub trait WrappingNeg {
2885    type Output;
2886
2887    fn wrapping_neg(self) -> Self::Output;
2888}
2889
2890/// Negates a number in place, wrapping around at the boundary of the type.
2891pub trait WrappingNegAssign {
2892    fn wrapping_neg_assign(&mut self);
2893}
2894
2895/// Raises a number to a power, wrapping around at the boundary of the type.
2896pub trait WrappingPow<RHS> {
2897    type Output;
2898
2899    fn wrapping_pow(self, exp: RHS) -> Self::Output;
2900}
2901
2902/// Raises a number to a power in place, wrapping around at the boundary of the type.
2903pub trait WrappingPowAssign<RHS = Self> {
2904    fn wrapping_pow_assign(&mut self, exp: RHS);
2905}
2906
2907/// Squares a number, wrapping around at the boundary of the type.
2908pub trait WrappingSquare {
2909    type Output;
2910
2911    fn wrapping_square(self) -> Self::Output;
2912}
2913
2914/// Squares a number in place, wrapping around at the boundary of the type.
2915pub trait WrappingSquareAssign {
2916    fn wrapping_square_assign(&mut self);
2917}
2918
2919/// Subtracts two numbers, wrapping around at the boundary of the type.
2920pub trait WrappingSub<RHS = Self> {
2921    type Output;
2922
2923    fn wrapping_sub(self, other: RHS) -> Self::Output;
2924}
2925
2926/// Subtracts a number by another number in place, wrapping around at the boundary of the type.
2927pub trait WrappingSubAssign<RHS = Self> {
2928    fn wrapping_sub_assign(&mut self, other: RHS);
2929}
2930
2931/// Subtracts a number by the product of two other numbers, wrapping around at the boundary of the
2932/// type.
2933pub trait WrappingSubMul<Y = Self, Z = Self> {
2934    type Output;
2935
2936    fn wrapping_sub_mul(self, y: Y, z: Z) -> Self::Output;
2937}
2938
2939/// Subtracts a number by the product of two other numbers, in place, wrapping around at the
2940/// boundary of the type.
2941pub trait WrappingSubMulAssign<Y = Self, Z = Self> {
2942    fn wrapping_sub_mul_assign(&mut self, y: Y, z: Z);
2943}
2944
2945/// Multiplies two numbers, returning the product as a pair of `Self` values.
2946///
2947/// The more significant number always comes first.
2948pub trait XMulYToZZ: Sized {
2949    fn x_mul_y_to_zz(x: Self, y: Self) -> (Self, Self);
2950}
2951
2952/// Adds two numbers, each composed of two `Self` values, returning the sum as a pair of `Self`
2953/// values.
2954///
2955/// The more significant number always comes first. Addition is wrapping, and overflow is not
2956/// indicated.
2957pub trait XXAddYYToZZ: Sized {
2958    fn xx_add_yy_to_zz(x_1: Self, x_0: Self, y_1: Self, y_0: Self) -> (Self, Self);
2959}
2960
2961/// Computes the quotient and remainder of two numbers. The first is composed of two `Self` values,
2962/// and the second of a single one.
2963///
2964/// `x_1` must be less than `y`.
2965pub trait XXDivModYToQR: Sized {
2966    fn xx_div_mod_y_to_qr(x_1: Self, x_0: Self, y: Self) -> (Self, Self);
2967}
2968
2969/// Subtracts two numbers, each composed of two `Self` values, returing the difference as a pair of
2970/// `Self` values.
2971///
2972/// The more significant number always comes first. Subtraction is wrapping, and overflow is not
2973/// indicated.
2974pub trait XXSubYYToZZ: Sized {
2975    fn xx_sub_yy_to_zz(x_1: Self, x_0: Self, y_1: Self, y_0: Self) -> (Self, Self);
2976}
2977
2978/// Adds two numbers, each composed of three `Self` values, returning the sum as a triple of `Self`
2979/// values.
2980///
2981/// The more significant number always comes first. Addition is wrapping, and overflow is not
2982/// indicated.
2983pub trait XXXAddYYYToZZZ: Sized {
2984    fn xxx_add_yyy_to_zzz(
2985        x_2: Self,
2986        x_1: Self,
2987        x_0: Self,
2988        y_2: Self,
2989        y_1: Self,
2990        y_0: Self,
2991    ) -> (Self, Self, Self);
2992}
2993
2994/// Subtracts two numbers, each composed of three `Self` values, returing the difference as a triple
2995/// of `Self` values.
2996///
2997/// The more significant number always comes first. Subtraction is wrapping, and overflow is not
2998/// indicated.
2999pub trait XXXSubYYYToZZZ: Sized {
3000    fn xxx_sub_yyy_to_zzz(
3001        x_2: Self,
3002        x_1: Self,
3003        x_0: Self,
3004        y_2: Self,
3005        y_1: Self,
3006        y_0: Self,
3007    ) -> (Self, Self, Self);
3008}
3009
3010/// Adds two numbers, each composed of four `Self` values, returning the sum as a quadruple of
3011/// `Self` values.
3012///
3013/// The more significant number always comes first. Addition is wrapping, and overflow is not
3014/// indicated.
3015pub trait XXXXAddYYYYToZZZZ: Sized {
3016    #[allow(clippy::too_many_arguments)]
3017    fn xxxx_add_yyyy_to_zzzz(
3018        x_3: Self,
3019        x_2: Self,
3020        x_1: Self,
3021        x_0: Self,
3022        y_3: Self,
3023        y_2: Self,
3024        y_1: Self,
3025        y_0: Self,
3026    ) -> (Self, Self, Self, Self);
3027}