Expand description
Numeric traits for generic mathematics
§Compatibility
The num-traits
crate is tested for rustc 1.60 and greater.
Modules§
Structs§
Enums§
Traits§
- AsPrimitive
- A generic interface for casting between machine scalars with the
as
operator, which admits narrowing and precision loss. Implementers of this traitAsPrimitive
should behave like a primitive numeric type (e.g. a newtype around another primitive), and the intended conversion must never fail. - Bounded
- Numbers which have upper and lower bounds
- Checked
Add - Performs addition that returns
None
instead of wrapping around on overflow. - Checked
Div - Performs division that returns
None
instead of panicking on division by zero and instead of wrapping around on underflow and overflow. - Checked
Euclid - Checked
Mul - Performs multiplication that returns
None
instead of wrapping around on underflow or overflow. - Checked
Neg - Performs negation that returns
None
if the result can’t be represented. - Checked
Rem - Performs an integral remainder that returns
None
instead of panicking on division by zero and instead of wrapping around on underflow and overflow. - Checked
Shl - Performs a left shift that returns
None
on shifts larger than or equal to the type width. - Checked
Shr - Performs a right shift that returns
None
on shifts larger than or equal to the type width. - Checked
Sub - Performs subtraction that returns
None
instead of wrapping around on underflow. - Const
One - Defines an associated constant representing the multiplicative identity
element for
Self
. - Const
Zero - Defines an associated constant representing the additive identity element
for
Self
. - Euclid
- Float
- Generic trait for floating point numbers
- Float
Const - From
Bytes - From
Primitive - A generic trait for converting a number to a value.
- Inv
- Unary operator for retrieving the multiplicative inverse, or reciprocal, of a value.
- MulAdd
- Fused multiply-add. Computes
(self * a) + b
with only one rounding error, yielding a more accurate result than an unfused multiply-add. - MulAdd
Assign - The fused multiply-add assignment operation
*self = (*self * a) + b
- Num
- The base trait for numeric types, covering
0
and1
values, comparisons, basic numeric operations, and string conversion. - NumAssign
- The trait for
Num
types which also implement assignment operators. - NumAssign
Ops - Generic trait for types implementing numeric assignment operators (like
+=
). - NumAssign
Ref - The trait for
NumAssign
types which also implement assignment operations taking the second operand by reference. - NumCast
- An interface for casting between machine scalars.
- NumOps
- Generic trait for types implementing basic numeric operations
- NumRef
- The trait for
Num
types which also implement numeric operations taking the second operand by reference. - One
- Defines a multiplicative identity element for
Self
. - Pow
- Binary operator for raising a value to a power.
- PrimInt
- Generic trait for primitive integers.
- RefNum
- The trait for
Num
references which implement numeric operations, taking the second operand either by value or by reference. - Saturating
- Saturating math operations. Deprecated, use
SaturatingAdd
,SaturatingSub
andSaturatingMul
instead. - Saturating
Add - Performs addition that saturates at the numeric bounds instead of overflowing.
- Saturating
Mul - Performs multiplication that saturates at the numeric bounds instead of overflowing.
- Saturating
Sub - Performs subtraction that saturates at the numeric bounds instead of overflowing.
- Signed
- Useful functions for signed numbers (i.e. numbers that can be negative).
- ToBytes
- ToPrimitive
- A generic trait for converting a value to a number.
- Unsigned
- A trait for values which cannot be negative
- Wrapping
Add - Performs addition that wraps around on overflow.
- Wrapping
Mul - Performs multiplication that wraps around on overflow.
- Wrapping
Neg - Performs a negation that does not panic.
- Wrapping
Shl - Performs a left shift that does not panic.
- Wrapping
Shr - Performs a right shift that does not panic.
- Wrapping
Sub - Performs subtraction that wraps around on overflow.
- Zero
- Defines an additive identity element for
Self
.
Functions§
- abs
- Computes the absolute value.
- abs_sub
- The positive difference of two numbers.
- cast
- Cast from one machine scalar to another.
- checked_
pow - Raises a value to the power of exp, returning
None
if an overflow occurred. - clamp
- A value bounded by a minimum and a maximum
- clamp_
max - A value bounded by a maximum value
- clamp_
min - A value bounded by a minimum value
- one
- Returns the multiplicative identity,
1
. - pow
- Raises a value to the power of exp, using exponentiation by squaring.
- signum
- Returns the sign of the number.
- zero
- Returns the additive identity,
0
.