malachite_base/num/arithmetic/traits.rs
1// Copyright © 2026 Mikhail Hogrefe
2//
3// This file is part of Malachite.
4//
5// Malachite is free software: you can redistribute it and/or modify it under the terms of the GNU
6// Lesser General Public License (LGPL) as published by the Free Software Foundation; either version
7// 3 of the License, or (at your option) any later version. See <https://www.gnu.org/licenses/>.
8
9use crate::num::basic::traits::Two;
10use crate::rounding_modes::RoundingMode;
11use core::cmp::Ordering;
12
13/// Takes the absolute value of a number. Assumes that the number has a representable absolute
14/// value.
15pub trait Abs {
16 type Output;
17
18 fn abs(self) -> Self::Output;
19}
20
21/// Replaces a number with its absolute value. Assumes that the number has a representable absolute
22/// value.
23pub trait AbsAssign {
24 fn abs_assign(&mut self);
25}
26
27/// Takes the absolute value of a number and converts to the unsigned equivalent.
28pub trait UnsignedAbs {
29 type Output;
30
31 fn unsigned_abs(self) -> Self::Output;
32}
33
34/// Subtracts two numbers and takes the absolute value of the difference.
35pub trait AbsDiff<RHS = Self> {
36 type Output;
37
38 fn abs_diff(self, other: RHS) -> Self::Output;
39}
40
41/// Replaces a number with the absolute value of its difference with another number.
42pub trait AbsDiffAssign<RHS = Self> {
43 fn abs_diff_assign(&mut self, other: RHS);
44}
45
46/// Adds a number and the product of two other numbers.
47pub trait AddMul<Y = Self, Z = Self> {
48 type Output;
49
50 fn add_mul(self, y: Y, z: Z) -> Self::Output;
51}
52
53/// Adds a number and the product of two other numbers, in place.
54pub trait AddMulAssign<Y = Self, Z = Self> {
55 fn add_mul_assign(&mut self, y: Y, z: Z);
56}
57
58/// Calculates the AGM (arithmetic-geometric mean) of two numbers.
59pub trait Agm<RHS = Self> {
60 type Output;
61
62 fn agm(self, other: RHS) -> Self::Output;
63}
64
65/// Replaces a number with the AGM (arithmetic-geometric mean) of it and another number.
66pub trait AgmAssign<RHS = Self> {
67 fn agm_assign(&mut self, other: RHS);
68}
69
70/// Left-shifts a number (multiplies it by a power of 2), returning `None` if the result is not
71/// representable.
72pub trait ArithmeticCheckedShl<RHS> {
73 type Output;
74
75 fn arithmetic_checked_shl(self, other: RHS) -> Option<Self::Output>;
76}
77
78/// Right-shifts a number (divides it by a power of 2), returning `None` if the result is not
79/// representable.
80pub trait ArithmeticCheckedShr<RHS> {
81 type Output;
82
83 fn arithmetic_checked_shr(self, other: RHS) -> Option<Self::Output>;
84}
85
86pub trait BinomialCoefficient<T = Self> {
87 fn binomial_coefficient(n: T, k: T) -> Self;
88}
89
90pub trait CheckedBinomialCoefficient<T = Self>: Sized {
91 fn checked_binomial_coefficient(n: T, k: T) -> Option<Self>;
92}
93
94/// Takes the ceiling of a number.
95pub trait Ceiling {
96 type Output;
97
98 fn ceiling(self) -> Self::Output;
99}
100
101/// Replaces a number with its ceiling.
102pub trait CeilingAssign {
103 fn ceiling_assign(&mut self);
104}
105
106/// Takes the absolute valie of a number, returning `None` if the result is not representable.
107pub trait CheckedAbs {
108 type Output;
109
110 fn checked_abs(self) -> Option<Self::Output>;
111}
112
113/// Adds two numbers, returning `None` if the result is not representable.
114pub trait CheckedAdd<RHS = Self> {
115 type Output;
116
117 fn checked_add(self, other: RHS) -> Option<Self::Output>;
118}
119
120/// Adds a number and the product of two other numbers, returning `None` if the result is not
121/// representable.
122pub trait CheckedAddMul<Y = Self, Z = Self> {
123 type Output;
124
125 fn checked_add_mul(self, y: Y, z: Z) -> Option<Self::Output>;
126}
127
128/// Divides two numbers, returning `None` if the result is not representable.
129pub trait CheckedDiv<RHS = Self> {
130 type Output;
131
132 fn checked_div(self, other: RHS) -> Option<Self::Output>;
133}
134
135/// Multiplies two numbers, returning `None` if the result is not representable.
136pub trait CheckedMul<RHS = Self> {
137 type Output;
138
139 fn checked_mul(self, other: RHS) -> Option<Self::Output>;
140}
141
142/// Negates a number, returning `None` if the result is not representable.
143pub trait CheckedNeg {
144 type Output;
145
146 fn checked_neg(self) -> Option<Self::Output>;
147}
148
149/// Finds the smallest integer power of 2 greater than or equal to a number, returning `None` if the
150/// result is not representable.
151pub trait CheckedNextPowerOf2 {
152 type Output;
153
154 fn checked_next_power_of_2(self) -> Option<Self::Output>;
155}
156
157/// Raises a number to a power, returning `None` if the result is not representable.
158pub trait CheckedPow<RHS> {
159 type Output;
160
161 fn checked_pow(self, exp: RHS) -> Option<Self::Output>;
162}
163
164/// Squares a number, returning `None` if the result is not representable.
165pub trait CheckedSquare {
166 type Output;
167
168 fn checked_square(self) -> Option<Self::Output>;
169}
170
171/// Subtracts two numbers, returning `None` if the result is not representable.
172pub trait CheckedSub<RHS = Self> {
173 type Output;
174
175 fn checked_sub(self, other: RHS) -> Option<Self::Output>;
176}
177
178/// Subtracts a number by the product of two other numbers, returning `None` if the result is not
179/// representable.
180pub trait CheckedSubMul<Y = Self, Z = Self> {
181 type Output;
182
183 fn checked_sub_mul(self, y: Y, z: Z) -> Option<Self::Output>;
184}
185
186/// Determines whether two numbers are coprime.
187pub trait CoprimeWith<RHS = Self> {
188 fn coprime_with(self, other: RHS) -> bool;
189}
190
191/// Divides two numbers, assuming the first exactly divides the second.
192///
193/// If it doesn't, the `div_exact` function may panic or return a meaningless result.
194pub trait DivExact<RHS = Self> {
195 type Output;
196
197 fn div_exact(self, other: RHS) -> Self::Output;
198}
199
200/// Divides a number by another number in place, assuming the first exactly divides the second.
201///
202/// If it doesn't, this function may panic or assign a meaningless number to the first number.
203pub trait DivExactAssign<RHS = Self> {
204 fn div_exact_assign(&mut self, other: RHS);
205}
206
207/// Divides two numbers, returning the quotient and remainder. The quotient is rounded towards
208/// negative infinity, and the remainder has the same sign as the divisor (second input).
209///
210/// The quotient and remainder satisfy $x = qy + r$ and $0 \leq |r| < |y|$.
211pub trait DivMod<RHS = Self> {
212 type DivOutput;
213 type ModOutput;
214
215 fn div_mod(self, other: RHS) -> (Self::DivOutput, Self::ModOutput);
216}
217
218/// Divides a number by another number in place, returning the remainder. The quotient is rounded
219/// towards negative infinity, and the remainder has the same sign as the divisor (second input).
220///
221/// The quotient and remainder satisfy $x = qy + r$ and $0 \leq |r| < |y|$.
222pub trait DivAssignMod<RHS = Self> {
223 type ModOutput;
224
225 fn div_assign_mod(&mut self, other: RHS) -> Self::ModOutput;
226}
227
228/// Divides two numbers, returning the quotient and remainder. The quotient is rounded towards zero,
229/// and the remainder has the same sign as the dividend (first input).
230///
231/// The quotient and remainder satisfy $x = qy + r$ and $0 \leq |r| < |y|$.
232pub trait DivRem<RHS = Self> {
233 type DivOutput;
234 type RemOutput;
235
236 fn div_rem(self, other: RHS) -> (Self::DivOutput, Self::RemOutput);
237}
238
239/// Divides a number by another number in place, returning the remainder. The quotient is rounded
240/// towards zero, and the remainder has the same sign as the dividend (first input).
241///
242/// The quotient and remainder satisfy $x = qy + r$ and $0 \leq |r| < |y|$.
243pub trait DivAssignRem<RHS = Self> {
244 type RemOutput;
245
246 fn div_assign_rem(&mut self, other: RHS) -> Self::RemOutput;
247}
248
249/// Divides a number by another number, returning the ceiling of the quotient and the remainder of
250/// the negative of the first number divided by the second.
251///
252/// The quotient and remainder satisfy $x = qy - r$ and $0 \leq r < y$.
253pub trait CeilingDivNegMod<RHS = Self> {
254 type DivOutput;
255 type ModOutput;
256
257 fn ceiling_div_neg_mod(self, other: RHS) -> (Self::DivOutput, Self::ModOutput);
258}
259
260/// Divides a number by another number in place, taking the ceiling of the quotient and returning
261/// the remainder of the negative of the first number divided by the second.
262///
263/// The quotient and remainder satisfy $x = qy - r$ and $0 \leq r < y$.
264pub trait CeilingDivAssignNegMod<RHS = Self> {
265 type ModOutput;
266
267 fn ceiling_div_assign_neg_mod(&mut self, other: RHS) -> Self::ModOutput;
268}
269
270/// Divides a number by another number, returning the quotient and remainder. The quotient is
271/// rounded towards positive infinity and the remainder has the opposite sign as the divisor (second
272/// input).
273///
274/// The quotient and remainder satisfy $x = qy + r$ and $0 \leq |r| < |y|$.
275pub trait CeilingDivMod<RHS = Self> {
276 type DivOutput;
277 type ModOutput;
278
279 fn ceiling_div_mod(self, other: RHS) -> (Self::DivOutput, Self::ModOutput);
280}
281
282/// Divides a number by another number in place, taking the quotient and returning the remainder.
283/// The quotient is rounded towards positive infinity and the remainder has the opposite sign of the
284/// divisor (second input).
285///
286/// The quotient and remainder satisfy $x = qy + r$ and $0 \leq |r| < |y|$.
287pub trait CeilingDivAssignMod<RHS = Self> {
288 type ModOutput;
289
290 fn ceiling_div_assign_mod(&mut self, other: RHS) -> Self::ModOutput;
291}
292
293/// Divides a number by another number and rounds according to a specified rounding mode. An
294/// [`Ordering`] is also returned, indicating whether the returned value is less than, equal to, or
295/// greater than the exact value.
296pub trait DivRound<RHS = Self> {
297 type Output;
298
299 fn div_round(self, other: RHS, rm: RoundingMode) -> (Self::Output, Ordering);
300}
301
302/// Divides a number by another number in place and rounds according to a specified rounding mode.
303/// An [`Ordering`] is returned, indicating whether the assigned value is less than, equal to, or
304/// greater than the exact value.
305pub trait DivRoundAssign<RHS = Self> {
306 fn div_round_assign(&mut self, other: RHS, rm: RoundingMode) -> Ordering;
307}
308
309/// Determines whether a number is divisible by $2^k$.
310pub trait DivisibleByPowerOf2 {
311 fn divisible_by_power_of_2(self, pow: u64) -> bool;
312}
313
314/// Determines whether a number is divisible by another number.
315pub trait DivisibleBy<RHS = Self> {
316 fn divisible_by(self, other: RHS) -> bool;
317}
318
319/// Determines whether a number is equivalent to another number modulo $2^k$.
320pub trait EqModPowerOf2<RHS = Self> {
321 fn eq_mod_power_of_2(self, other: RHS, pow: u64) -> bool;
322}
323
324/// Determines whether a number is equivalent to another number modulo $m$.
325pub trait EqMod<RHS = Self, M = Self> {
326 fn eq_mod(self, other: RHS, m: M) -> bool;
327}
328
329/// Computes the GCD (greatest common divisor) of two numbers $a$ and $b$, and also the coefficients
330/// $x$ and $y$ in Bézout's identity $ax+by=\gcd(a,b)$.
331///
332/// The are infinitely many $x$, $y$ that satisfy the identity, so the full specification is more
333/// detailed:
334///
335/// - $f(0, 0) = (0, 0, 0)$.
336/// - $f(a, ak) = (a, 1, 0)$ if $a > 0$ and $k \neq 1$.
337/// - $f(a, ak) = (-a, -1, 0)$ if $a < 0$ and $k \neq 1$.
338/// - $f(bk, b) = (b, 0, 1)$ if $b > 0$.
339/// - $f(bk, b) = (-b, 0, -1)$ if $b < 0$.
340/// - $f(a, b) = (g, x, y)$ if $a \neq 0$ and $b \neq 0$ and $\gcd(a, b) \neq \min(|a|, |b|)$, where
341/// $g = \gcd(a, b) \geq 0$, $ax + by = g$, $x \leq \lfloor b/g \rfloor$, and $y \leq \lfloor a/g
342/// \rfloor$.
343pub trait ExtendedGcd<RHS = Self> {
344 type Gcd;
345 type Cofactor;
346
347 fn extended_gcd(self, other: RHS) -> (Self::Gcd, Self::Cofactor, Self::Cofactor);
348}
349
350/// Computes the factorial of a `u64`.
351pub trait Factorial {
352 fn factorial(n: u64) -> Self;
353}
354
355/// Computes the factorial of a `u64`, returning `None` if the result is too large to be
356/// represented.
357pub trait CheckedFactorial: Sized {
358 fn checked_factorial(n: u64) -> Option<Self>;
359}
360
361/// Computes the double factorial of a `u64`. The double factorial of a non-negative integer is the
362/// product of all the positive integers that are less than or equal to it and have the same parity
363/// as it.
364pub trait DoubleFactorial {
365 fn double_factorial(n: u64) -> Self;
366}
367
368/// Computes the double factorial of a `u64`, returning `None` if the result is too large to be
369/// represented. The double factorial of a non-negative integer is the product of all the positive
370/// integers that are less than or equal to it and have the same parity as it.
371pub trait CheckedDoubleFactorial: Sized {
372 fn checked_double_factorial(n: u64) -> Option<Self>;
373}
374
375/// Computes the $m$-multifactorial of a `u64`. The $m$-multifactorial of a non-negative integer $n$
376/// is the product of all integers $k$ such that $0<k\leq n$ and $k\equiv n \pmod m$.
377pub trait Multifactorial {
378 fn multifactorial(n: u64, m: u64) -> Self;
379}
380
381/// Computes the $m$-multifactorial of a `u64`, returning `None` if the result is too large to be
382/// represented. The $m$-multifactorial of a non-negative integer $n$ is the product of all integers
383/// $k$ such that $0<k\leq n$ and $k\equiv n \pmod m$.
384pub trait CheckedMultifactorial: Sized {
385 fn checked_multifactorial(n: u64, m: u64) -> Option<Self>;
386}
387
388/// Computes the subfactorial of a `u64`. The subfactorial of a non-negative integer $n$ counts the
389/// number of derangements of $n$ elements, which are the permutations in which no element is fixed.
390pub trait Subfactorial {
391 fn subfactorial(n: u64) -> Self;
392}
393
394/// Computes the subfactorial of a `u64`, returning `None` if the result is too large to be
395/// represented. The subfactorial of a non-negative integer $n$ counts the number of derangements of
396/// $n$ elements, which are the permutations in which no element is fixed.
397pub trait CheckedSubfactorial: Sized {
398 fn checked_subfactorial(n: u64) -> Option<Self>;
399}
400
401/// Takes the floor of a number.
402pub trait Floor {
403 type Output;
404
405 fn floor(self) -> Self::Output;
406}
407
408/// Replaces a number with its floor.
409pub trait FloorAssign {
410 fn floor_assign(&mut self);
411}
412
413/// Calculates the GCD (greatest common divisor) of two numbers.
414pub trait Gcd<RHS = Self> {
415 type Output;
416
417 fn gcd(self, other: RHS) -> Self::Output;
418}
419
420/// Replaces a number with the GCD (greatest common divisor) of it and another number.
421pub trait GcdAssign<RHS = Self> {
422 fn gcd_assign(&mut self, other: RHS);
423}
424
425/// Determines whether a number is an integer power of 2.
426pub trait IsPowerOf2 {
427 fn is_power_of_2(&self) -> bool;
428}
429
430/// Calculates the LCM (least common multiple) of two numbers.
431pub trait Lcm<RHS = Self> {
432 type Output;
433
434 fn lcm(self, other: RHS) -> Self::Output;
435}
436
437/// Replaces a number with the LCM (least common multiple) of it and another number.
438pub trait LcmAssign<RHS = Self> {
439 fn lcm_assign(&mut self, other: RHS);
440}
441
442/// Takes the natural logarithm of a number.
443pub trait Ln {
444 type Output;
445
446 fn ln(self) -> Self::Output;
447}
448
449/// Calculates the LCM (least common multiple) of two numbers, returning `None` if the result is not
450/// representable.
451pub trait CheckedLcm<RHS = Self> {
452 type Output;
453
454 fn checked_lcm(self, other: RHS) -> Option<Self::Output>;
455}
456
457/// Calculates the Legendre symbol of two numbers. Typically the implementations will be identical
458/// to those of [`JacobiSymbol`].
459pub trait LegendreSymbol<RHS = Self> {
460 fn legendre_symbol(self, other: RHS) -> i8;
461}
462
463/// Calculates the Jacobi symbol of two numbers.
464pub trait JacobiSymbol<RHS = Self> {
465 fn jacobi_symbol(self, other: RHS) -> i8;
466}
467
468/// Calculates the Kronecker symbol of two numbers.
469pub trait KroneckerSymbol<RHS = Self> {
470 fn kronecker_symbol(self, other: RHS) -> i8;
471}
472
473/// Calculates the base-$b$ logarithm of a number, or returns `None` if the number is not a perfect
474/// power of $b$.
475pub trait CheckedLogBase<B = Self> {
476 type Output;
477
478 fn checked_log_base(self, base: B) -> Option<Self::Output>;
479}
480
481/// Calculates the floor of the base-$b$ logarithm of a number.
482pub trait FloorLogBase<B = Self> {
483 type Output;
484
485 fn floor_log_base(self, base: B) -> Self::Output;
486}
487
488/// Calculates the ceiling of the base-$b$ logarithm of a number.
489pub trait CeilingLogBase<B = Self> {
490 type Output;
491
492 fn ceiling_log_base(self, base: B) -> Self::Output;
493}
494
495/// Calculates the base-2 logarithm of a number, or returns `None` if the number is not a perfect
496/// power of 2.
497pub trait CheckedLogBase2 {
498 type Output;
499
500 fn checked_log_base_2(self) -> Option<Self::Output>;
501}
502
503/// Calculates the floor of the base-2 logarithm of a number.
504pub trait FloorLogBase2 {
505 type Output;
506
507 fn floor_log_base_2(self) -> Self::Output;
508}
509
510/// Calculates the ceiling of the base-2 logarithm of a number.
511pub trait CeilingLogBase2 {
512 type Output;
513
514 fn ceiling_log_base_2(self) -> Self::Output;
515}
516
517/// Calculates the base-$2^k$ logarithm of a number, or returns `None` if the number is not a
518/// perfect power of $2^k$.
519pub trait CheckedLogBasePowerOf2<POW> {
520 type Output;
521
522 fn checked_log_base_power_of_2(self, pow: POW) -> Option<Self::Output>;
523}
524
525/// Calculates the floor of the base-$2^k$ logarithm of a number.
526pub trait FloorLogBasePowerOf2<POW> {
527 type Output;
528
529 fn floor_log_base_power_of_2(self, pow: POW) -> Self::Output;
530}
531
532/// Calculates the ceiling of the base-$2^k$ logarithm of a number.
533pub trait CeilingLogBasePowerOf2<POW> {
534 type Output;
535
536 fn ceiling_log_base_power_of_2(self, pow: POW) -> Self::Output;
537}
538
539/// Adds two numbers modulo a third number $m$. The inputs must be already reduced modulo $m$.
540pub trait ModAdd<RHS = Self, M = Self> {
541 type Output;
542
543 fn mod_add(self, other: RHS, m: M) -> Self::Output;
544}
545
546/// Adds two numbers modulo a third number $m$, in place. The inputs must be already reduced modulo
547/// $m$.
548pub trait ModAddAssign<RHS = Self, M = Self> {
549 fn mod_add_assign(&mut self, other: RHS, m: M);
550}
551
552/// Finds the multiplicative inverse of a number modulo another number $m$. The input must be
553/// already reduced modulo $m$.
554pub trait ModInverse<M = Self> {
555 type Output;
556
557 fn mod_inverse(self, m: M) -> Option<Self::Output>;
558}
559
560/// Checks whether a number is reduced modulo another number $m$.
561pub trait ModIsReduced<M = Self> {
562 fn mod_is_reduced(&self, m: &M) -> bool;
563}
564
565/// Multiplies two numbers modulo a third number $m$. The inputs must be already reduced modulo $m$.
566pub trait ModMul<RHS = Self, M = Self> {
567 type Output;
568
569 fn mod_mul(self, other: RHS, m: M) -> Self::Output;
570}
571
572/// Multiplies two numbers modulo a third number $m$, in place. The inputs must be already reduced
573/// modulo $m$.
574pub trait ModMulAssign<RHS = Self, M = Self> {
575 fn mod_mul_assign(&mut self, other: RHS, m: M);
576}
577
578/// Multiplies two numbers modulo a third number $m$. The inputs must be already reduced modulo $m$.
579///
580/// If multiple modular multiplications with the same modulus are necessary, it can be quicker to
581/// precompute some piece of data and reuse it in the multiplication calls. This trait provides a
582/// function for precomputing the data and a function for using it during multiplication.
583pub trait ModMulPrecomputed<RHS = Self, M = Self> {
584 type Output;
585 type Data;
586
587 /// Precomputes some data to use for modular multiplication.
588 fn precompute_mod_mul_data(m: &M) -> Self::Data;
589
590 fn mod_mul_precomputed(self, other: RHS, m: M, data: &Self::Data) -> Self::Output;
591}
592
593/// Multiplies two numbers modulo a third number $m$, in place.The inputs must be already reduced
594/// modulo $m$.
595///
596/// If multiple modular multiplications with the same modulus are necessary, it can be quicker to
597/// precompute some piece of data and reuse it in the multiplication calls. This trait provides a
598/// function for using precomputed data during multiplication. For precomputing the data, use the
599/// [`precompute_mod_mul_data`](ModMulPrecomputed::precompute_mod_mul_data) function in
600/// [`ModMulPrecomputed`].
601pub trait ModMulPrecomputedAssign<RHS = Self, M = Self>: ModMulPrecomputed<RHS, M> {
602 fn mod_mul_precomputed_assign(&mut self, other: RHS, m: M, data: &Self::Data);
603}
604
605/// Negates a number modulo another number $m$. The input must be already reduced modulo $m$.
606pub trait ModNeg<M = Self> {
607 type Output;
608
609 fn mod_neg(self, m: M) -> Self::Output;
610}
611
612/// Negates a number modulo another number $m$, in place. The input must be already reduced modulo
613/// $m$.
614pub trait ModNegAssign<M = Self> {
615 fn mod_neg_assign(&mut self, m: M);
616}
617
618/// Divides a number by another number, returning just the remainder. The remainder has the same
619/// sign as the divisor (second number).
620///
621/// If the quotient were computed, the quotient and remainder would satisfy $x = qy + r$ and $0 \leq
622/// |r| < |y|$.
623pub trait Mod<RHS = Self> {
624 type Output;
625
626 fn mod_op(self, other: RHS) -> Self::Output;
627}
628
629/// Divides a number by another number, replacing the first number by the remainder. The remainder
630/// has the same sign as the divisor (second number).
631///
632/// If the quotient were computed, the quotient and remainder would satisfy $x = qy + r$ and $0 \leq
633/// |r| < |y|$.
634pub trait ModAssign<RHS = Self> {
635 fn mod_assign(&mut self, other: RHS);
636}
637
638/// Divides the negative of a number by another number, returning the remainder.
639///
640/// If the quotient were computed, the quotient and remainder would satisfy $x = qy - r$ and $0 \leq
641/// r < y$.
642pub trait NegMod<RHS = Self> {
643 type Output;
644
645 fn neg_mod(self, other: RHS) -> Self::Output;
646}
647
648/// Divides the negative of a number by another number, replacing the first number by the remainder.
649///
650/// If the quotient were computed, the quotient and remainder would satisfy $x = qy - r$ and $0 \leq
651/// r < y$.
652pub trait NegModAssign<RHS = Self> {
653 fn neg_mod_assign(&mut self, other: RHS);
654}
655
656/// Divides a number by another number, returning just the remainder. The remainder has the opposite
657/// sign as the divisor (second number).
658///
659/// If the quotient were computed, the quotient and remainder would satisfy $x = qy + r$ and $0 \leq
660/// |r| < |y|$.
661pub trait CeilingMod<RHS = Self> {
662 type Output;
663
664 fn ceiling_mod(self, other: RHS) -> Self::Output;
665}
666
667/// Divides a number by another number, replacing the first number by the remainder. The remainder
668/// has the same sign as the divisor (second number).
669///
670/// If the quotient were computed, the quotient and remainder would satisfy $x = qy + r$ and $0 \leq
671/// |r| < |y|$.
672pub trait CeilingModAssign<RHS = Self> {
673 fn ceiling_mod_assign(&mut self, other: RHS);
674}
675
676/// Raises a number to a power modulo another number $m$. The base must be already reduced modulo
677/// $m$.
678pub trait ModPow<RHS = Self, M = Self> {
679 type Output;
680
681 fn mod_pow(self, exp: RHS, m: M) -> Self::Output;
682}
683
684/// Raises a number to a power modulo another number $m$, in place. The base must be already reduced
685/// modulo $m$.
686pub trait ModPowAssign<RHS = Self, M = Self> {
687 fn mod_pow_assign(&mut self, exp: RHS, m: M);
688}
689
690/// Raises a number to a power modulo another number $m$. The base must be already reduced modulo
691/// $m$.
692///
693/// If multiple modular exponentiations with the same modulus are necessary, it can be quicker to
694/// precompute some piece of data and reuse it in the exponentiation calls. This trait provides a
695/// function for precomputing the data and a function for using it during exponentiation.
696pub trait ModPowPrecomputed<RHS = Self, M = Self>
697where
698 Self: Sized,
699{
700 type Output;
701 type Data;
702
703 /// Precomputes some data to use for modular exponentiation.
704 fn precompute_mod_pow_data(m: &M) -> Self::Data;
705
706 fn mod_pow_precomputed(self, exp: RHS, m: M, data: &Self::Data) -> Self::Output;
707}
708
709/// Raises a number to a power modulo another number $m$, in place. The base must be already reduced
710/// modulo $m$.
711///
712/// If multiple modular exponentiations with the same modulus are necessary, it can be quicker to
713/// precompute some piece of data and reuse it in the exponentiation calls. This trait provides a
714/// function for using precomputed data during exponentiation. For precomputing the data, use the
715/// [`precompute_mod_pow_data`](ModPowPrecomputed::precompute_mod_pow_data) function in
716/// [`ModPowPrecomputed`].
717pub trait ModPowPrecomputedAssign<RHS: Two = Self, M = Self>: ModPowPrecomputed<RHS, M> {
718 fn mod_pow_precomputed_assign(&mut self, exp: RHS, m: M, data: &Self::Data);
719}
720
721/// Adds two numbers modulo $2^k$. The inputs must be already reduced modulo $2^k$.
722pub trait ModPowerOf2Add<RHS = Self> {
723 type Output;
724
725 fn mod_power_of_2_add(self, other: RHS, pow: u64) -> Self::Output;
726}
727
728/// Adds two numbers modulo $2^k$, in place. The inputs must be already reduced modulo $2^k$.
729pub trait ModPowerOf2AddAssign<RHS = Self> {
730 fn mod_power_of_2_add_assign(&mut self, other: RHS, pow: u64);
731}
732
733/// Finds the multiplicative inverse of a number modulo $2^k$. The input must be already reduced
734/// modulo $2^k$.
735pub trait ModPowerOf2Inverse {
736 type Output;
737
738 fn mod_power_of_2_inverse(self, pow: u64) -> Option<Self::Output>;
739}
740
741/// Checks whether a number is reduced modulo $2^k$.
742pub trait ModPowerOf2IsReduced {
743 fn mod_power_of_2_is_reduced(&self, pow: u64) -> bool;
744}
745
746/// Multiplies two numbers modulo $2^k$. The inputs must be already reduced modulo $2^k$.
747pub trait ModPowerOf2Mul<RHS = Self> {
748 type Output;
749
750 fn mod_power_of_2_mul(self, other: RHS, pow: u64) -> Self::Output;
751}
752
753/// Multiplies two numbers modulo $2^k$, in place. The inputs must be already reduced modulo $2^k$.
754pub trait ModPowerOf2MulAssign<RHS = Self> {
755 fn mod_power_of_2_mul_assign(&mut self, other: RHS, pow: u64);
756}
757
758/// Negates a number modulo $2^k$. The input must be already reduced modulo $2^k$.
759pub trait ModPowerOf2Neg {
760 type Output;
761
762 fn mod_power_of_2_neg(self, pow: u64) -> Self::Output;
763}
764
765/// Negates a number modulo $2^k$ in place. The input must be already reduced modulo $2^k$.
766pub trait ModPowerOf2NegAssign {
767 fn mod_power_of_2_neg_assign(&mut self, pow: u64);
768}
769
770/// Raises a number to a power modulo $2^k$. The base must be already reduced modulo $2^k$.
771pub trait ModPowerOf2Pow<RHS = Self> {
772 type Output;
773
774 fn mod_power_of_2_pow(self, exp: RHS, pow: u64) -> Self::Output;
775}
776
777/// Raises a number to a power modulo $2^k$, in place. The base must be already reduced modulo
778/// $2^k$.
779pub trait ModPowerOf2PowAssign<RHS = Self> {
780 fn mod_power_of_2_pow_assign(&mut self, exp: RHS, pow: u64);
781}
782
783/// Left-shifts a number (multiplies it by a power of 2) modulo $2^k$. The number must be already
784/// reduced modulo $2^k$.
785pub trait ModPowerOf2Shl<RHS> {
786 type Output;
787
788 fn mod_power_of_2_shl(self, other: RHS, pow: u64) -> Self::Output;
789}
790
791/// Left-shifts a number (multiplies it by a power of 2) modulo $2^k$, in place. The number must be
792/// already reduced modulo $2^k$.
793pub trait ModPowerOf2ShlAssign<RHS> {
794 fn mod_power_of_2_shl_assign(&mut self, other: RHS, pow: u64);
795}
796
797/// Right-shifts a number (divides it by a power of 2) modulo $2^k$. The number must be already
798/// reduced modulo $2^k$.
799pub trait ModPowerOf2Shr<RHS> {
800 type Output;
801
802 fn mod_power_of_2_shr(self, other: RHS, pow: u64) -> Self::Output;
803}
804
805/// Right-shifts a number (divides it by a power of 2) modulo $2^k$, in place. The number must be
806/// already reduced modulo $2^k$.
807pub trait ModPowerOf2ShrAssign<RHS> {
808 fn mod_power_of_2_shr_assign(&mut self, other: RHS, pow: u64);
809}
810
811/// Squares a number modulo $2^k$. The input must be already reduced modulo $2^k$.
812pub trait ModPowerOf2Square {
813 type Output;
814
815 fn mod_power_of_2_square(self, pow: u64) -> Self::Output;
816}
817
818/// Squares a number modulo $2^k$ in place. The input must be already reduced modulo $2^k$.
819pub trait ModPowerOf2SquareAssign {
820 fn mod_power_of_2_square_assign(&mut self, pow: u64);
821}
822
823/// Subtracts two numbers modulo $2^k$. The inputs must be already reduced modulo $2^k$.
824pub trait ModPowerOf2Sub<RHS = Self> {
825 type Output;
826
827 fn mod_power_of_2_sub(self, other: RHS, pow: u64) -> Self::Output;
828}
829
830/// Subtracts two numbers modulo $2^k$, in place. The inputs must be already reduced modulo $2^k$.
831pub trait ModPowerOf2SubAssign<RHS = Self> {
832 fn mod_power_of_2_sub_assign(&mut self, other: RHS, pow: u64);
833}
834
835/// Divides a number by $2^k$, returning just the remainder. The remainder is non-negative.
836///
837/// If the quotient were computed, the quotient and remainder would satisfy $x = q2^k + r$ and $0
838/// \leq r < 2^k$.
839pub trait ModPowerOf2 {
840 type Output;
841
842 fn mod_power_of_2(self, other: u64) -> Self::Output;
843}
844
845/// Divides a number by $2^k$, replacing the number by the remainder. The remainder is non-negative.
846///
847/// If the quotient were computed, the quotient and remainder would satisfy $x = q2^k + r$ and $0
848/// \leq r < 2^k$.
849pub trait ModPowerOf2Assign {
850 fn mod_power_of_2_assign(&mut self, other: u64);
851}
852
853/// Divides a number by $2^k$, returning just the remainder. The remainder has the same sign as the
854/// number.
855///
856/// If the quotient were computed, the quotient and remainder would satisfy $x = q2^k + r$ and $0
857/// \leq |r| < 2^k$.
858pub trait RemPowerOf2 {
859 type Output;
860
861 fn rem_power_of_2(self, other: u64) -> Self::Output;
862}
863
864/// Divides a number by $2^k$, replacing the number by the remainder. The remainder has the same
865/// sign as the number.
866///
867/// If the quotient were computed, the quotient and remainder would satisfy $x = q2^k + r$ and $0
868/// \leq |r| < 2^k$.
869pub trait RemPowerOf2Assign {
870 fn rem_power_of_2_assign(&mut self, other: u64);
871}
872
873/// Divides the negative of a number by $2^k$, returning the remainder.
874///
875/// If the quotient were computed, the quotient and remainder would satisfy $x = q2^k - r$ and $0
876/// \leq r < 2^k$.
877pub trait NegModPowerOf2 {
878 type Output;
879
880 fn neg_mod_power_of_2(self, other: u64) -> Self::Output;
881}
882
883/// Divides the negative of a number by $2^k$, replacing the number by the remainder.
884///
885/// If the quotient were computed, the quotient and remainder would satisfy $x = q2^k - r$ and $0
886/// \leq r < 2^k$.
887pub trait NegModPowerOf2Assign {
888 fn neg_mod_power_of_2_assign(&mut self, other: u64);
889}
890
891/// Divides a number by $2^k$, returning just the remainder. The remainder is non-positive.
892///
893/// If the quotient were computed, the quotient and remainder would satisfy $x = q2^k + r$ and $0
894/// \leq -r < 2^k$.
895pub trait CeilingModPowerOf2 {
896 type Output;
897
898 fn ceiling_mod_power_of_2(self, other: u64) -> Self::Output;
899}
900
901/// Divides a number by $2^k$, replacing the number by the remainder. The remainder is non-positive.
902///
903/// If the quotient were computed, the quotient and remainder would satisfy $x = q2^k + r$ and $0
904/// \leq -r < 2^k$.
905pub trait CeilingModPowerOf2Assign {
906 fn ceiling_mod_power_of_2_assign(&mut self, other: u64);
907}
908
909/// Left-shifts a number (multiplies it by a power of 2) modulo another number $m$. The number must
910/// be already reduced modulo $m$.
911pub trait ModShl<RHS, M = Self> {
912 type Output;
913
914 fn mod_shl(self, other: RHS, m: M) -> Self::Output;
915}
916
917/// Left-shifts a number (multiplies it by a power of 2) modulo another number $m$, in place. The
918/// number must be already reduced modulo $m$.
919pub trait ModShlAssign<RHS, M = Self> {
920 fn mod_shl_assign(&mut self, other: RHS, m: M);
921}
922
923/// Left-shifts a number (divides it by a power of 2) modulo another number $m$. The number must be
924/// already reduced modulo $m$.
925pub trait ModShr<RHS, M = Self> {
926 type Output;
927
928 fn mod_shr(self, other: RHS, m: M) -> Self::Output;
929}
930
931/// Left-shifts a number (divides it by a power of 2) modulo another number $m$, in place. The
932/// number must be already reduced modulo $m$.
933pub trait ModShrAssign<RHS, M = Self> {
934 fn mod_shr_assign(&mut self, other: RHS, m: M);
935}
936
937/// Squares a number modulo another number $m$. The input must be already reduced modulo $m$.
938pub trait ModSquare<M = Self> {
939 type Output;
940
941 fn mod_square(self, m: M) -> Self::Output;
942}
943
944/// Squares a number modulo another number $m$, in place. The input must be already reduced modulo
945/// $m$.
946pub trait ModSquareAssign<M = Self> {
947 fn mod_square_assign(&mut self, m: M);
948}
949
950/// Squares a number modulo another number $m$. The input must be already reduced modulo $m$.
951///
952/// If multiple modular squarings with the same modulus are necessary, it can be quicker to
953/// precompute some piece of data using
954/// [`precompute_mod_pow_data`](ModPowPrecomputed::precompute_mod_pow_data) function in
955/// [`ModMulPrecomputed`] and reuse it in the squaring calls.
956pub trait ModSquarePrecomputed<RHS = Self, M = Self>: ModPowPrecomputed<RHS, M>
957where
958 Self: Sized,
959{
960 fn mod_square_precomputed(self, m: M, data: &Self::Data) -> Self::Output;
961}
962
963/// Squares a number modulo another number $m$, in place. The input must be already reduced modulo
964/// $m$.
965///
966/// If multiple modular squarings with the same modulus are necessary, it can be quicker to
967/// precompute some piece of data using
968/// [`precompute_mod_pow_data`](ModPowPrecomputed::precompute_mod_pow_data) function in
969/// [`ModMulPrecomputed`] and reuse it in the squaring calls.
970pub trait ModSquarePrecomputedAssign<RHS = Self, M = Self>: ModPowPrecomputed<RHS, M> {
971 fn mod_square_precomputed_assign(&mut self, m: M, data: &Self::Data);
972}
973
974/// Adds two numbers modulo a third number $m$. The inputs must be already reduced modulo $m$.
975pub trait ModSub<RHS = Self, M = Self> {
976 type Output;
977
978 fn mod_sub(self, other: RHS, m: M) -> Self::Output;
979}
980
981/// Adds two numbers modulo a third number $m$, in place. The inputs must be already reduced modulo
982/// $m$.
983pub trait ModSubAssign<RHS = Self, M = Self> {
984 fn mod_sub_assign(&mut self, other: RHS, m: M);
985}
986
987/// Replaces a number with its negative. Assumes the result is representable.
988pub trait NegAssign {
989 fn neg_assign(&mut self);
990}
991
992/// Returns the smallest power of 2 greater than or equal to a number. Assumes the result is
993/// representable.
994pub trait NextPowerOf2 {
995 type Output;
996
997 fn next_power_of_2(self) -> Self::Output;
998}
999
1000/// Replaces a number with the smallest power of 2 greater than or equal it. Assumes the result is
1001/// representable.
1002pub trait NextPowerOf2Assign {
1003 fn next_power_of_2_assign(&mut self);
1004}
1005
1006/// Takes the absolute value of a number.
1007///
1008/// Returns a tuple of the result along with a boolean indicating whether an arithmetic overflow
1009/// occurred. If an overflow occurred, then the wrapped number is returned.
1010pub trait OverflowingAbs {
1011 type Output;
1012
1013 fn overflowing_abs(self) -> (Self::Output, bool);
1014}
1015
1016/// Replaces a number with its absolute value.
1017///
1018/// Returns a boolean indicating whether an arithmetic overflow occurred. If an overflow occurred,
1019/// then the wrapped number is assigned.
1020pub trait OverflowingAbsAssign {
1021 fn overflowing_abs_assign(&mut self) -> bool;
1022}
1023
1024/// Adds two numbers.
1025///
1026/// Returns a tuple of the sum along with a boolean indicating whether an arithmetic overflow
1027/// occurred. If an overflow occurred, then the wrapped number is returned.
1028pub trait OverflowingAdd<RHS = Self> {
1029 type Output;
1030
1031 fn overflowing_add(self, other: RHS) -> (Self::Output, bool);
1032}
1033
1034/// Adds a number to another number in place.
1035///
1036/// Returns a boolean indicating whether an arithmetic overflow occurred. If an overflow occurred,
1037/// then the wrapped number is assigned.
1038pub trait OverflowingAddAssign<RHS = Self> {
1039 fn overflowing_add_assign(&mut self, other: RHS) -> bool;
1040}
1041
1042/// Adds a number and the product of two other numbers.
1043///
1044/// Returns a tuple of the result along with a boolean indicating whether an arithmetic overflow
1045/// occurred. If an overflow occurred, then the wrapped number is returned.
1046pub trait OverflowingAddMul<Y = Self, Z = Self> {
1047 type Output;
1048
1049 fn overflowing_add_mul(self, y: Y, z: Z) -> (Self::Output, bool);
1050}
1051
1052/// Adds a number and the product of two other numbers, in place.
1053///
1054/// Returns a tuple of the result along with a boolean indicating whether an arithmetic overflow
1055/// occurred. If an overflow occurred, then the wrapped number is returned.
1056pub trait OverflowingAddMulAssign<Y = Self, Z = Self> {
1057 fn overflowing_add_mul_assign(&mut self, y: Y, z: Z) -> bool;
1058}
1059
1060/// Divides two numbers.
1061///
1062/// Returns a tuple of the sum along with a boolean indicating whether an arithmetic overflow
1063/// occurred. If an overflow occurred, then the wrapped number is returned.
1064pub trait OverflowingDiv<RHS = Self> {
1065 type Output;
1066
1067 fn overflowing_div(self, other: RHS) -> (Self::Output, bool);
1068}
1069
1070/// Divides a number by another number in place.
1071///
1072/// Returns a boolean indicating whether an arithmetic overflow occurred. If an overflow occurred,
1073/// then the wrapped number is assigned.
1074pub trait OverflowingDivAssign<RHS = Self> {
1075 fn overflowing_div_assign(&mut self, other: RHS) -> bool;
1076}
1077
1078/// Multiplies two numbers.
1079///
1080/// Returns a tuple of the sum along with a boolean indicating whether an arithmetic overflow
1081/// occurred. If an overflow occurred, then the wrapped number is returned.
1082pub trait OverflowingMul<RHS = Self> {
1083 type Output;
1084
1085 fn overflowing_mul(self, other: RHS) -> (Self::Output, bool);
1086}
1087
1088/// Multiplies a number by another number in place.
1089///
1090/// Returns a boolean indicating whether an arithmetic overflow occurred. If an overflow occurred,
1091/// then the wrapped number is assigned.
1092pub trait OverflowingMulAssign<RHS = Self> {
1093 fn overflowing_mul_assign(&mut self, other: RHS) -> bool;
1094}
1095
1096/// Negates a number.
1097///
1098/// Returns a tuple of the sum along with a boolean indicating whether an arithmetic overflow
1099/// occurred. If an overflow occurred, then the wrapped number is returned.
1100pub trait OverflowingNeg {
1101 type Output;
1102
1103 fn overflowing_neg(self) -> (Self::Output, bool);
1104}
1105
1106/// Negates a number in place.
1107///
1108/// Returns a boolean indicating whether an arithmetic overflow occurred. If an overflow occurred,
1109/// then the wrapped number is assigned.
1110pub trait OverflowingNegAssign {
1111 fn overflowing_neg_assign(&mut self) -> bool;
1112}
1113
1114/// Raises a number to a power.
1115///
1116/// Returns a tuple of the sum along with a boolean indicating whether an arithmetic overflow
1117/// occurred. If an overflow occurred, then the wrapped number is returned.
1118pub trait OverflowingPow<RHS> {
1119 type Output;
1120
1121 fn overflowing_pow(self, exp: RHS) -> (Self::Output, bool);
1122}
1123
1124/// Raises a number to a power in place.
1125///
1126/// Returns a boolean indicating whether an arithmetic overflow occurred. If an overflow occurred,
1127/// then the wrapped number is assigned.
1128pub trait OverflowingPowAssign<RHS = Self> {
1129 fn overflowing_pow_assign(&mut self, exp: RHS) -> bool;
1130}
1131
1132/// Squares a number.
1133///
1134/// Returns a tuple of the sum along with a boolean indicating whether an arithmetic overflow
1135/// occurred. If an overflow occurred, then the wrapped number is returned.
1136pub trait OverflowingSquare {
1137 type Output;
1138
1139 fn overflowing_square(self) -> (Self::Output, bool);
1140}
1141
1142/// Squares a number in place.
1143///
1144/// Returns a boolean indicating whether an arithmetic overflow occurred. If an overflow occurred,
1145/// then the wrapped number is assigned.
1146pub trait OverflowingSquareAssign {
1147 fn overflowing_square_assign(&mut self) -> bool;
1148}
1149
1150/// Subtracts two numbers.
1151///
1152/// Returns a tuple of the sum along with a boolean indicating whether an arithmetic overflow
1153/// occurred. If an overflow occurred, then the wrapped number is returned.
1154pub trait OverflowingSub<RHS = Self> {
1155 type Output;
1156
1157 fn overflowing_sub(self, other: RHS) -> (Self::Output, bool);
1158}
1159
1160/// Subtracts a number by another number in place.
1161///
1162/// Returns a boolean indicating whether an arithmetic overflow occurred. If an overflow occurred,
1163/// then the wrapped number is assigned.
1164pub trait OverflowingSubAssign<RHS = Self> {
1165 fn overflowing_sub_assign(&mut self, other: RHS) -> bool;
1166}
1167
1168/// Subtracts a number by the product of two other numbers.
1169///
1170/// Returns a tuple of the result along with a boolean indicating whether an arithmetic overflow
1171/// occurred. If an overflow occurred, then the wrapped number is returned.
1172pub trait OverflowingSubMul<Y = Self, Z = Self> {
1173 type Output;
1174
1175 fn overflowing_sub_mul(self, y: Y, z: Z) -> (Self::Output, bool);
1176}
1177
1178/// Subtracts a number by the product of two other numbers, in place.
1179///
1180/// Returns a tuple of the result along with a boolean indicating whether an arithmetic overflow
1181/// occurred. If an overflow occurred, then the wrapped number is returned.
1182pub trait OverflowingSubMulAssign<Y = Self, Z = Self> {
1183 fn overflowing_sub_mul_assign(&mut self, y: Y, z: Z) -> bool;
1184}
1185
1186/// Determines whether a number is even or odd.
1187pub trait Parity {
1188 /// Determines whether a number is even.
1189 fn even(self) -> bool;
1190
1191 /// Determines whether a number is odd.
1192 fn odd(self) -> bool;
1193}
1194
1195/// Raises a number to a power. Assumes the result is representable.
1196pub trait Pow<RHS> {
1197 type Output;
1198
1199 fn pow(self, exp: RHS) -> Self::Output;
1200}
1201
1202/// Raises a number to a power in place. Assumes the result is representable.
1203pub trait PowAssign<RHS = Self> {
1204 fn pow_assign(&mut self, exp: RHS);
1205}
1206
1207/// Raises 2 to a power.
1208pub trait PowerOf2<POW> {
1209 fn power_of_2(pow: POW) -> Self;
1210}
1211
1212pub trait Primorial {
1213 fn primorial(n: u64) -> Self;
1214
1215 fn product_of_first_n_primes(n: u64) -> Self;
1216}
1217
1218pub trait CheckedPrimorial: Sized {
1219 fn checked_primorial(n: u64) -> Option<Self>;
1220
1221 fn checked_product_of_first_n_primes(n: u64) -> Option<Self>;
1222}
1223
1224/// Finds the reciprocal (multiplicative inverse) of a number.
1225pub trait Reciprocal {
1226 type Output;
1227
1228 fn reciprocal(self) -> Self::Output;
1229}
1230
1231/// Replaces a number with its reciprocal (multiplicative inverse).
1232pub trait ReciprocalAssign {
1233 fn reciprocal_assign(&mut self);
1234}
1235
1236/// Takes the reciprocal of the square root of a number.
1237pub trait ReciprocalSqrt {
1238 type Output;
1239
1240 fn reciprocal_sqrt(self) -> Self::Output;
1241}
1242
1243/// Replaces a number with the reciprocal of its square root.
1244pub trait ReciprocalSqrtAssign {
1245 fn reciprocal_sqrt_assign(&mut self);
1246}
1247
1248/// Finds the floor of the $n$th root of a number.
1249pub trait FloorRoot<POW> {
1250 type Output;
1251
1252 fn floor_root(self, pow: POW) -> Self::Output;
1253}
1254
1255/// Replaces a number with the floor of its $n$th root.
1256pub trait FloorRootAssign<POW> {
1257 fn floor_root_assign(&mut self, pow: POW);
1258}
1259
1260/// Finds the ceiling of the $n$th root of a number.
1261pub trait CeilingRoot<POW> {
1262 type Output;
1263
1264 fn ceiling_root(self, pow: POW) -> Self::Output;
1265}
1266
1267/// Replaces a number with the ceiling of its $n$th root.
1268pub trait CeilingRootAssign<POW> {
1269 fn ceiling_root_assign(&mut self, pow: POW);
1270}
1271
1272/// Finds the $n$th root of a number, returning `None` if it is not a perfect $n$th power.
1273pub trait CheckedRoot<POW> {
1274 type Output;
1275
1276 fn checked_root(self, pow: POW) -> Option<Self::Output>;
1277}
1278
1279/// Finds the floor of the $n$th root of a number, returning both the root and the remainder.
1280pub trait RootRem<POW> {
1281 type RootOutput;
1282 type RemOutput;
1283
1284 fn root_rem(self, exp: POW) -> (Self::RootOutput, Self::RemOutput);
1285}
1286
1287/// Replaces a number with the floor of its $n$th root, returning the remainder.
1288pub trait RootAssignRem<POW> {
1289 type RemOutput;
1290
1291 fn root_assign_rem(&mut self, exp: POW) -> Self::RemOutput;
1292}
1293
1294/// Rotates a number left, inserting the leftmost bits into the right end.
1295pub trait RotateLeft {
1296 type Output;
1297
1298 fn rotate_left(self, n: u64) -> Self::Output;
1299}
1300
1301/// Rotates a number left, inserting the leftmost bits into the right end, in place.
1302pub trait RotateLeftAssign {
1303 fn rotate_left_assign(&mut self, n: u64);
1304}
1305
1306/// Rotates a number right, inserting the leftmost bits into the left end.
1307pub trait RotateRight {
1308 type Output;
1309
1310 fn rotate_right(self, n: u64) -> Self::Output;
1311}
1312
1313/// Rotates a number right, inserting the leftmost bits into the left end, in place.
1314pub trait RotateRightAssign {
1315 fn rotate_right_assign(&mut self, n: u64);
1316}
1317
1318/// Rounds a number to a multiple of another number, according to a specified rounding mode. An
1319/// [`Ordering`] is also returned, indicating whether the returned value is less than, equal to, or
1320/// greater than the original value.
1321pub trait RoundToMultiple<RHS = Self> {
1322 type Output;
1323
1324 fn round_to_multiple(self, other: RHS, rm: RoundingMode) -> (Self::Output, Ordering);
1325}
1326
1327/// Rounds a number to a multiple of another number in place, according to a specified rounding
1328/// mode. [`Ordering`] is returned, indicating whether the returned value is less than, equal to, or
1329/// greater than the original value.
1330pub trait RoundToMultipleAssign<RHS = Self> {
1331 fn round_to_multiple_assign(&mut self, other: RHS, rm: RoundingMode) -> Ordering;
1332}
1333
1334/// Rounds a number to a multiple of $2^k$, according to a specified rounding mode. An [`Ordering`]
1335/// is also returned, indicating whether the returned value is less than, equal to, or greater than
1336/// the original value.
1337pub trait RoundToMultipleOfPowerOf2<RHS> {
1338 type Output;
1339
1340 fn round_to_multiple_of_power_of_2(
1341 self,
1342 pow: RHS,
1343 rm: RoundingMode,
1344 ) -> (Self::Output, Ordering);
1345}
1346
1347/// Rounds a number to a multiple of $2^k$ in place, according to a specified rounding mode. An
1348/// [`Ordering`] is returned, indicating whether the returned value is less than, equal to, or
1349/// greater than the original value.
1350pub trait RoundToMultipleOfPowerOf2Assign<RHS> {
1351 fn round_to_multiple_of_power_of_2_assign(&mut self, pow: RHS, rm: RoundingMode) -> Ordering;
1352}
1353
1354/// Takes the absolute value of a number, saturating at the numeric bounds instead of overflowing.
1355pub trait SaturatingAbs {
1356 type Output;
1357
1358 fn saturating_abs(self) -> Self::Output;
1359}
1360
1361/// Replaces a number with its absolute value, saturating at the numeric bounds instead of
1362/// overflowing.
1363pub trait SaturatingAbsAssign {
1364 fn saturating_abs_assign(&mut self);
1365}
1366
1367/// Adds two numbers, saturating at the numeric bounds instead of overflowing.
1368pub trait SaturatingAdd<RHS = Self> {
1369 type Output;
1370
1371 fn saturating_add(self, other: RHS) -> Self::Output;
1372}
1373
1374/// Add a number to another number in place, saturating at the numeric bounds instead of
1375/// overflowing.
1376pub trait SaturatingAddAssign<RHS = Self> {
1377 fn saturating_add_assign(&mut self, other: RHS);
1378}
1379
1380/// Adds a number and the product of two other numbers, saturating at the numeric bounds instead of
1381/// overflowing.
1382pub trait SaturatingAddMul<Y = Self, Z = Self> {
1383 type Output;
1384
1385 fn saturating_add_mul(self, y: Y, z: Z) -> Self::Output;
1386}
1387
1388/// Adds a number and the product of two other numbers in place, saturating at the numeric bounds
1389/// instead of overflowing.
1390pub trait SaturatingAddMulAssign<Y = Self, Z = Self> {
1391 fn saturating_add_mul_assign(&mut self, y: Y, z: Z);
1392}
1393
1394/// Multiplies two numbers, saturating at the numeric bounds instead of overflowing.
1395pub trait SaturatingMul<RHS = Self> {
1396 type Output;
1397
1398 fn saturating_mul(self, other: RHS) -> Self::Output;
1399}
1400
1401/// Multiplies a number by another number in place, saturating at the numeric bounds instead of
1402/// overflowing.
1403pub trait SaturatingMulAssign<RHS = Self> {
1404 fn saturating_mul_assign(&mut self, other: RHS);
1405}
1406
1407/// Negates a number, saturating at the numeric bounds instead of overflowing.
1408pub trait SaturatingNeg {
1409 type Output;
1410
1411 fn saturating_neg(self) -> Self::Output;
1412}
1413
1414/// Negates a number in place, saturating at the numeric bounds instead of overflowing.
1415pub trait SaturatingNegAssign {
1416 fn saturating_neg_assign(&mut self);
1417}
1418
1419/// Raises a number to a power, saturating at the numeric bounds instead of overflowing.
1420pub trait SaturatingPow<RHS> {
1421 type Output;
1422
1423 fn saturating_pow(self, exp: RHS) -> Self::Output;
1424}
1425
1426/// Raises a number to a power in place, saturating at the numeric bounds instead of overflowing.
1427pub trait SaturatingPowAssign<RHS = Self> {
1428 fn saturating_pow_assign(&mut self, exp: RHS);
1429}
1430
1431/// Squares a number, saturating at the numeric bounds instead of overflowing.
1432pub trait SaturatingSquare {
1433 type Output;
1434
1435 fn saturating_square(self) -> Self::Output;
1436}
1437
1438/// Squares a number in place, saturating at the numeric bounds instead of overflowing.
1439pub trait SaturatingSquareAssign {
1440 fn saturating_square_assign(&mut self);
1441}
1442
1443/// Subtracts two numbers, saturating at the numeric bounds instead of overflowing.
1444pub trait SaturatingSub<RHS = Self> {
1445 type Output;
1446
1447 fn saturating_sub(self, other: RHS) -> Self::Output;
1448}
1449
1450/// Subtracts a number by another number in place, saturating at the numeric bounds instead of
1451/// overflowing.
1452pub trait SaturatingSubAssign<RHS = Self> {
1453 fn saturating_sub_assign(&mut self, other: RHS);
1454}
1455
1456/// Subtracts a number by the product of two other numbers, saturating at the numeric bounds instead
1457/// of overflowing.
1458pub trait SaturatingSubMul<Y = Self, Z = Self> {
1459 type Output;
1460
1461 fn saturating_sub_mul(self, y: Y, z: Z) -> Self::Output;
1462}
1463
1464/// Subtracts a number by the product of two other numbers in place, saturating at the numeric
1465/// bounds instead of overflowing.
1466pub trait SaturatingSubMulAssign<Y = Self, Z = Self> {
1467 fn saturating_sub_mul_assign(&mut self, y: Y, z: Z);
1468}
1469
1470/// Left-shifts a number (multiplies it by a power of 2), rounding the result according to a
1471/// specified rounding mode. An [`Ordering`] is also returned, indicating whether the returned value
1472/// is less than, equal to, or greater than the exact value.
1473///
1474/// Rounding might only be necessary if `other` is negative.
1475pub trait ShlRound<RHS> {
1476 type Output;
1477
1478 fn shl_round(self, other: RHS, rm: RoundingMode) -> (Self::Output, Ordering);
1479}
1480
1481/// Left-shifts a number (multiplies it by a power of 2) in place, rounding the result according to
1482/// a specified rounding mode. An [`Ordering`] is also returned, indicating whether the assigned
1483/// value is less than, equal to, or greater than the exact value.
1484///
1485/// Rounding might only be necessary if `other` is negative.
1486pub trait ShlRoundAssign<RHS> {
1487 fn shl_round_assign(&mut self, other: RHS, rm: RoundingMode) -> Ordering;
1488}
1489
1490/// Right-shifts a number (divides it by a power of 2), rounding the result according to a specified
1491/// rounding mode. An [`Ordering`] is also returned, indicating whether the returned value is less
1492/// than, equal to, or greater than the exact value.
1493///
1494/// Rounding might only be necessary if `other` is positive.
1495pub trait ShrRound<RHS> {
1496 type Output;
1497
1498 fn shr_round(self, other: RHS, rm: RoundingMode) -> (Self::Output, Ordering);
1499}
1500
1501/// Right-shifts a number (divides it by a power of 2) in place, rounding the result according to a
1502/// specified rounding mode. An [`Ordering`] is also returned, indicating whether the assigned value
1503/// is less than, equal to, or greater than the exact value.
1504///
1505/// Rounding might only be necessary if `other` is positive.
1506pub trait ShrRoundAssign<RHS> {
1507 fn shr_round_assign(&mut self, other: RHS, rm: RoundingMode) -> Ordering;
1508}
1509
1510/// Returns `Greater`, `Equal`, or `Less`, depending on whether a number is positive, zero, or
1511/// negative, respectively.
1512pub trait Sign {
1513 fn sign(&self) -> Ordering;
1514}
1515
1516/// Takes the square root of a number.
1517pub trait Sqrt {
1518 type Output;
1519
1520 fn sqrt(self) -> Self::Output;
1521}
1522
1523/// Replaces a number with its square root.
1524pub trait SqrtAssign {
1525 fn sqrt_assign(&mut self);
1526}
1527
1528/// Finds the floor of the square root of a number.
1529pub trait FloorSqrt {
1530 type Output;
1531
1532 fn floor_sqrt(self) -> Self::Output;
1533}
1534
1535/// Replaces a number with the floor of its square root.
1536pub trait FloorSqrtAssign {
1537 fn floor_sqrt_assign(&mut self);
1538}
1539
1540/// Finds the ceiling of the square root of a number.
1541pub trait CeilingSqrt {
1542 type Output;
1543
1544 fn ceiling_sqrt(self) -> Self::Output;
1545}
1546
1547/// Replaces a number with the ceiling of its square root.
1548pub trait CeilingSqrtAssign {
1549 fn ceiling_sqrt_assign(&mut self);
1550}
1551
1552/// Finds the square root of a number, returning `None` if it is not a perfect square.
1553pub trait CheckedSqrt {
1554 type Output;
1555
1556 fn checked_sqrt(self) -> Option<Self::Output>;
1557}
1558
1559/// Finds the floor of the square root of a number, returning both the root and the remainder.
1560pub trait SqrtRem {
1561 type SqrtOutput;
1562 type RemOutput;
1563
1564 fn sqrt_rem(self) -> (Self::SqrtOutput, Self::RemOutput);
1565}
1566
1567/// Replaces a number with the floor of its square root, returning the remainder.
1568pub trait SqrtAssignRem {
1569 type RemOutput;
1570
1571 fn sqrt_assign_rem(&mut self) -> Self::RemOutput;
1572}
1573
1574/// Squares a number.
1575pub trait Square {
1576 type Output;
1577
1578 fn square(self) -> Self::Output;
1579}
1580
1581/// Replaces a number with its square.
1582pub trait SquareAssign {
1583 fn square_assign(&mut self);
1584}
1585
1586/// Subtracts a number by the product of two other numbers.
1587pub trait SubMul<Y = Self, Z = Self> {
1588 type Output;
1589
1590 fn sub_mul(self, y: Y, z: Z) -> Self::Output;
1591}
1592
1593/// Subtracts a number by the product of two other numbers, in place.
1594pub trait SubMulAssign<Y = Self, Z = Self> {
1595 fn sub_mul_assign(&mut self, y: Y, z: Z);
1596}
1597
1598/// Takes the absolute value of a number, wrapping around at the boundary of the type.
1599pub trait WrappingAbs {
1600 type Output;
1601
1602 fn wrapping_abs(self) -> Self::Output;
1603}
1604
1605/// Replaces a number with its absolute value, wrapping around at the boundary of the type.
1606pub trait WrappingAbsAssign {
1607 fn wrapping_abs_assign(&mut self);
1608}
1609
1610/// Adds two numbers, wrapping around at the boundary of the type.
1611pub trait WrappingAdd<RHS = Self> {
1612 type Output;
1613
1614 fn wrapping_add(self, other: RHS) -> Self::Output;
1615}
1616
1617/// Adds a number to another number in place, wrapping around at the boundary of the type.
1618pub trait WrappingAddAssign<RHS = Self> {
1619 fn wrapping_add_assign(&mut self, other: RHS);
1620}
1621
1622/// Adds a number and the product of two other numbers, wrapping around at the boundary of the type.
1623pub trait WrappingAddMul<Y = Self, Z = Self> {
1624 type Output;
1625
1626 fn wrapping_add_mul(self, y: Y, z: Z) -> Self::Output;
1627}
1628
1629/// Adds a number and the product of two other numbers, in place, wrapping around at the boundary of
1630/// the type.
1631pub trait WrappingAddMulAssign<Y = Self, Z = Self> {
1632 fn wrapping_add_mul_assign(&mut self, y: Y, z: Z);
1633}
1634
1635/// Divides a number by another number, wrapping around at the boundary of the type.
1636pub trait WrappingDiv<RHS = Self> {
1637 type Output;
1638
1639 fn wrapping_div(self, other: RHS) -> Self::Output;
1640}
1641
1642/// Divides a number by another number in place, wrapping around at the boundary of the type.
1643pub trait WrappingDivAssign<RHS = Self> {
1644 fn wrapping_div_assign(&mut self, other: RHS);
1645}
1646
1647/// Multiplies two numbers, wrapping around at the boundary of the type.
1648pub trait WrappingMul<RHS = Self> {
1649 type Output;
1650
1651 fn wrapping_mul(self, other: RHS) -> Self::Output;
1652}
1653
1654/// Multiplies a number by another number in place, wrapping around at the boundary of the type.
1655pub trait WrappingMulAssign<RHS = Self> {
1656 fn wrapping_mul_assign(&mut self, other: RHS);
1657}
1658
1659/// Negates a number, wrapping around at the boundary of the type.
1660pub trait WrappingNeg {
1661 type Output;
1662
1663 fn wrapping_neg(self) -> Self::Output;
1664}
1665
1666/// Negates a number in place, wrapping around at the boundary of the type.
1667pub trait WrappingNegAssign {
1668 fn wrapping_neg_assign(&mut self);
1669}
1670
1671/// Raises a number to a power, wrapping around at the boundary of the type.
1672pub trait WrappingPow<RHS> {
1673 type Output;
1674
1675 fn wrapping_pow(self, exp: RHS) -> Self::Output;
1676}
1677
1678/// Raises a number to a power in place, wrapping around at the boundary of the type.
1679pub trait WrappingPowAssign<RHS = Self> {
1680 fn wrapping_pow_assign(&mut self, exp: RHS);
1681}
1682
1683/// Squares a number, wrapping around at the boundary of the type.
1684pub trait WrappingSquare {
1685 type Output;
1686
1687 fn wrapping_square(self) -> Self::Output;
1688}
1689
1690/// Squares a number in place, wrapping around at the boundary of the type.
1691pub trait WrappingSquareAssign {
1692 fn wrapping_square_assign(&mut self);
1693}
1694
1695/// Subtracts two numbers, wrapping around at the boundary of the type.
1696pub trait WrappingSub<RHS = Self> {
1697 type Output;
1698
1699 fn wrapping_sub(self, other: RHS) -> Self::Output;
1700}
1701
1702/// Subtracts a number by another number in place, wrapping around at the boundary of the type.
1703pub trait WrappingSubAssign<RHS = Self> {
1704 fn wrapping_sub_assign(&mut self, other: RHS);
1705}
1706
1707/// Subtracts a number by the product of two other numbers, wrapping around at the boundary of the
1708/// type.
1709pub trait WrappingSubMul<Y = Self, Z = Self> {
1710 type Output;
1711
1712 fn wrapping_sub_mul(self, y: Y, z: Z) -> Self::Output;
1713}
1714
1715/// Subtracts a number by the product of two other numbers, in place, wrapping around at the
1716/// boundary of the type.
1717pub trait WrappingSubMulAssign<Y = Self, Z = Self> {
1718 fn wrapping_sub_mul_assign(&mut self, y: Y, z: Z);
1719}
1720
1721/// Multiplies two numbers, returning the product as a pair of `Self` values.
1722///
1723/// The more significant number always comes first.
1724pub trait XMulYToZZ: Sized {
1725 fn x_mul_y_to_zz(x: Self, y: Self) -> (Self, Self);
1726}
1727
1728/// Adds two numbers, each composed of two `Self` values, returning the sum as a pair of `Self`
1729/// values.
1730///
1731/// The more significant number always comes first. Addition is wrapping, and overflow is not
1732/// indicated.
1733pub trait XXAddYYToZZ: Sized {
1734 fn xx_add_yy_to_zz(x_1: Self, x_0: Self, y_1: Self, y_0: Self) -> (Self, Self);
1735}
1736
1737/// Computes the quotient and remainder of two numbers. The first is composed of two `Self` values,
1738/// and the second of a single one.
1739///
1740/// `x_1` must be less than `y`.
1741pub trait XXDivModYToQR: Sized {
1742 fn xx_div_mod_y_to_qr(x_1: Self, x_0: Self, y: Self) -> (Self, Self);
1743}
1744
1745/// Subtracts two numbers, each composed of two `Self` values, returing the difference as a pair of
1746/// `Self` values.
1747///
1748/// The more significant number always comes first. Subtraction is wrapping, and overflow is not
1749/// indicated.
1750pub trait XXSubYYToZZ: Sized {
1751 fn xx_sub_yy_to_zz(x_1: Self, x_0: Self, y_1: Self, y_0: Self) -> (Self, Self);
1752}
1753
1754/// Adds two numbers, each composed of three `Self` values, returning the sum as a triple of `Self`
1755/// values.
1756///
1757/// The more significant number always comes first. Addition is wrapping, and overflow is not
1758/// indicated.
1759pub trait XXXAddYYYToZZZ: Sized {
1760 fn xxx_add_yyy_to_zzz(
1761 x_2: Self,
1762 x_1: Self,
1763 x_0: Self,
1764 y_2: Self,
1765 y_1: Self,
1766 y_0: Self,
1767 ) -> (Self, Self, Self);
1768}
1769
1770/// Subtracts two numbers, each composed of three `Self` values, returing the difference as a triple
1771/// of `Self` values.
1772///
1773/// The more significant number always comes first. Subtraction is wrapping, and overflow is not
1774/// indicated.
1775pub trait XXXSubYYYToZZZ: Sized {
1776 fn xxx_sub_yyy_to_zzz(
1777 x_2: Self,
1778 x_1: Self,
1779 x_0: Self,
1780 y_2: Self,
1781 y_1: Self,
1782 y_0: Self,
1783 ) -> (Self, Self, Self);
1784}
1785
1786/// Adds two numbers, each composed of four `Self` values, returning the sum as a quadruple of
1787/// `Self` values.
1788///
1789/// The more significant number always comes first. Addition is wrapping, and overflow is not
1790/// indicated.
1791pub trait XXXXAddYYYYToZZZZ: Sized {
1792 #[allow(clippy::too_many_arguments)]
1793 fn xxxx_add_yyyy_to_zzzz(
1794 x_3: Self,
1795 x_2: Self,
1796 x_1: Self,
1797 x_0: Self,
1798 y_3: Self,
1799 y_2: Self,
1800 y_1: Self,
1801 y_0: Self,
1802 ) -> (Self, Self, Self, Self);
1803}