Modules decimal128.move and decimal256.move
These modules implement functions that can be used to work with fixed precision (18 decimal digits) unsigned numbers that can be represented in, respectively, 128 and 256 bits.
The modules expose functions to create a Decimal128
/Decimal256
number from a raw value (already premultiplied by 1e18) as well as parsing a number from a string. Note that an issue with number parsing is discussed in issue ref↗.
These functions are implemented in decimal128
:
new
: Creates aDecimal128
from a (premultiplied)u128
value.new_u64
: Creates aDecimal128
from a (premultiplied)u64
value.one
: Creates aDecimal128
with value 1.zero
: Creates aDecimal128
with zero value.from_ratio_u64
: Creates aDecimal128
that represents the ratio between twou64
integers.from_ratio
: Creates aDecimal128
that represents the ratio between twou128
integers.add
: Adds twoDecimal128
, returning a newDecimal128
as a result.sub
: Adds twoDecimal128
, returning a newDecimal128
as a result.mul_u64
: Multiplies aDecimal128
by au64
integer value, returning an unscaledu64
integer result.mul_u128
: Multiplies aDecimal128
by au128
integer value, returning an unscaledu128
integer result.mul
: Multiplies twoDecimal128
numbers, returning aDecimal128
result.div_u64
: Divides aDecimal128
by au64
integer value, returning aDecimal128
result.div
: Divides aDecimal128
by au128
integer value, returning aDecimal128
result.val
: Retrieves the inner value of theDecimal128
(which is scaled by 1e18).is_same
: Compares twoDecimal128
s.from_string
: Parses a number into aDecimal128
.
And these functions are implemented in decimal256
:
new
: Creates aDecimal256
from a (premultiplied)u256
value.new_u64
: Creates aDecimal256
from a (premultiplied)u64
value.new_u128
: Creates aDecimal256
from a (premultiplied)u128
value.one
: Creates aDecimal256
with value 1.zero
: Creates aDecimal256
with zero value.from_ratio_u64
: Creates aDecimal256
that represents the ratio between twou64
integers.from_ratio_u128
: Creates aDecimal256
that represents the ratio between twou128
integers.from_ratio
: Creates aDecimal256
that represents the ratio between twou256
integers.add
: Adds twoDecimal256
, returning a newDecimal256
as a result.sub
: Adds twoDecimal256
, returning a newDecimal256
as a result.mul_u64
: Multiplies aDecimal256
by au64
integer value, returning an unscaledu64
integer result.mul_u128
: Multiplies aDecimal256
by au128
integer value, returning an unscaledu128
integer result.mul_u256
: Multiplies aDecimal256
by au256
integer value, returning an unscaledu256
integer result.mul
: Multiplies twoDecimal256
numbers, returning aDecimal256
result.div_u64
: Divides aDecimal256
by au64
integer value, returning aDecimal256
result.div_u128
: Divides aDecimal256
by au128
integer value, returning aDecimal256
result.div
: Divides aDecimal256
by au256
integer value, returning aDecimal256
result.val
: Retrieves the inner value of theDecimal256
(which is scaled by 1e18).is_same
: Compares twoDecimal256
s.from_string
: Parses a number into aDecimal256
.