Expand description
The arithmetic types used for safe math.
This is already part of the main prelude
.
Modules§
- biguint
- Infinite precision unsigned integer for substrate runtime.
- fixed_
point - Decimal Fixed Point implementations for Substrate runtime.
Similar to types that implement
PerThing
, these are also fixed-point types, however, they are able to represent larger fractions: - helpers_
128bit - Some helper functions to work with 128bit numbers. Note that the functionality provided here is only sensible to use with 128bit numbers because for smaller sizes, you can always rely on assumptions of a bigger type (u128) being available, or simply create a per-thing and use the multiplication implementation provided there.
- per_
things - Types that implement
PerThing
can be used as a floating-point alternative for numbers that operate within the realm of[0, 1]
. The primary types may you encounter in Substrate would be the following: - rational
- traits
- Primitive traits for the runtime arithmetic.
Macros§
- assert_
eq_ error_ rate - Copied from
sp-runtime
and documented there.
Structs§
- Fixed
I64 - A fixed point number representation in the range. Fixed Point 64 bits signed, range = [-9223372036.854775808, 9223372036.854775807]
- Fixed
I128 - A fixed point number representation in the range. Fixed Point 128 bits signed, range = [-170141183460469231731.687303715884105728, 170141183460469231731.687303715884105727]
- Fixed
U64 - A fixed point number representation in the range. Fixed Point 64 bits unsigned, range = [0.000000000, 18446744073.709551615]
- Fixed
U128 - A fixed point number representation in the range. Fixed Point 128 bits unsigned, range = [0.000000000000000000, 340282366920938463463.374607431768211455]
- PerU16
- A fixed point representation of a number in the range [0, 1].
- Perbill
- A fixed point representation of a number in the range [0, 1].
- Percent
- A fixed point representation of a number in the range [0, 1].
- Permill
- A fixed point representation of a number in the range [0, 1].
- Perquintill
- A fixed point representation of a number in the range [0, 1].
- Rational128
- A wrapper for any rational number with a 128 bit numerator and denominator.
- Rational
Infinite - A wrapper for any rational number with infinitely large numerator and denominator.
Enums§
- Arithmetic
Error - Arithmetic errors.
- Rounding
- The rounding method to use for unsigned quantities.
- Signed
Rounding - The rounding method to use.
Traits§
- AtLeast8
Bit - A meta trait for arithmetic.
- AtLeast8
BitUnsigned - A meta trait for arithmetic. Same as
AtLeast8Bit
, but also bounded to be unsigned. - AtLeast16
Bit - A meta trait for arithmetic.
- AtLeast16
BitUnsigned - A meta trait for arithmetic. Same as
AtLeast16Bit
, but also bounded to be unsigned. - AtLeast32
Bit - A meta trait for arithmetic.
- AtLeast32
BitUnsigned - A meta trait for arithmetic. Same as
AtLeast32Bit
, but also bounded to be unsigned. - Base
Arithmetic - A meta trait for arithmetic type operations, regardless of any limitation on size.
- 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
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. - Ensure
- Ensure
Add - Performs addition that returns
ArithmeticError
instead of wrapping around on overflow. - Ensure
AddAssign - Performs self addition that returns
ArithmeticError
instead of wrapping around on overflow. - Ensure
Div - Performs division that returns
ArithmeticError
instead of wrapping around on overflow. - Ensure
DivAssign - Performs self division that returns
ArithmeticError
instead of wrapping around on overflow. - Ensure
Fixed Point Number - Extends
FixedPointNumber
with the Ensure family functions. - Ensure
From - Similar to
TryFrom
but returning anArithmeticError
error. - Ensure
Into - Similar to
TryInto
but returning anArithmeticError
error. - Ensure
Mul - Performs multiplication that returns
ArithmeticError
instead of wrapping around on overflow. - Ensure
MulAssign - Performs self multiplication that returns
ArithmeticError
instead of wrapping around on overflow. - Ensure
Op - Meta trait that supports all immutable arithmetic
Ensure*
operations - Ensure
OpAssign - Meta trait that supports all assigned arithmetic
Ensure*
operations - Ensure
Sub - Performs subtraction that returns
ArithmeticError
instead of wrapping around on underflow. - Ensure
SubAssign - Performs self subtraction that returns
ArithmeticError
instead of wrapping around on underflow. - Fixed
Point Number - Something that implements a decimal fixed point number.
- Fixed
Point Operand - Integer types that can be used to interact with
FixedPointNumber
implementations. - Integer
Square Root - A trait implementing integer square root.
- Multiply
Arg - Multiply
Rational - Normalizable
- A collection-like object that is made of values of type
T
and can normalize its individual values around a centric point. - One
- Defines a multiplicative identity element for
Self
. - PerThing
- Something that implements a fixed point ration with an arbitrary granularity
X
, as parts perX
. - Rational
Arg - Reciprocal
Arg - Saturated
Conversion - Convenience type to work around the highly unergonomic syntax needed
to invoke the functions of overloaded generic traits, in this case
SaturatedFrom
andSaturatedInto
. - Saturating
- Saturating arithmetic operations, returning maximum or minimum values instead of overflowing.
- Signed
- Useful functions for signed numbers (i.e. numbers that can be negative).
- Threshold
Ord - Trait for comparing two numbers with an threshold.
- Unique
Saturated From - Just like
From
except that if the source value is too big to fit into the destination type then it’ll saturate the destination. - Unique
Saturated Into - Just like
Into
except that if the source value is too big to fit into the destination type then it’ll saturate the destination. - Unsigned
- A trait for values which cannot be negative
- Zero
- Defines an additive identity element for
Self
.
Functions§
- checked_
pow - Raises a value to the power of exp, returning
None
if an overflow occurred. - ensure_
pow - Raises a value to the power of exp, returning
ArithmeticError
if an overflow occurred. - normalize
- Normalize
input
so that the sum of all elements reachestargeted_sum
.