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