Fixed Point 64
A library to perform math operations over an unsigned integer with 64-bit precision. Any operation that results in a number larger than the maximum unsigned 128 bit, will be considered an overflow and throw.
Structs
FixedPoint64
struct FixedPoint64 has copy, drop, store { value: u128 }value - The number.
Interface
value
It returns the raw u128 value.
public fun value(self: FixedPoint64): u128@param self: A FixedPoint64
@return u128. The raw u128 value.
from
Creates a FixedPoint64 from a u128 number. It scales the number.
public fun from(value: u128): FixedPoint64@param value: A u128 number.
@return A FixedPoint64. calculated by right shifting -
value<< 64.
Aborts
The left-shifted
valueis larger thanMAX_U128.
from_raw_value
Creates a FixedPoint64 from a u128 value. It does not scale the value.
@param value: A u128 number.
@return FixedPoint64. It wraps the u128.
from_rational
Creates a FixedPoint64 from a rational number specified by a numerator anddenominator.
0.0125 will round down to 0.012 instead of up to 0.013.
@param numerator: The numerator of the rational number.
@param denominator: The denominator of the rational number.
@return FixedPoint64. A FixedPoint64 from (
numerator<< 64) /denominator.
Aborts
if the denominator is zero
if the numerator / denominator is zero
if the numerator is nonzero and the ratio is not in the range 2^-64 .. 2^64-1
to_u128
Converts a FixedPoint64 into a u128 number to the closest integer.
@param self. A FixedPoint64.
@return u128.
to_u128_down
Converts a FixedPoint64 into a u128 number rounding down.
@param self. A FixedPoint64.
@return u128.
to_u128_up
Converts a FixedPoint64 into a u128 number rounding up.
@param self. A FixedPoint64.
@return u128.
is_zero
Checks if self is zero.
@param self. A FixedPoint64.
@return bool. If the
self.valueis zero.
eq
Checks if x is equal to y.
@param x: A FixedPoint64.
@param y: A FixedPoint64.
@return bool. If the values are equal.
lt
Checks if x is smaller or equal to y.
@param x: A FixedPoint64.
@param y: A FixedPoint64.
@return bool. If
xis smaller thany.
gt
Checks if x is bigger than y.
@param x: A FixedPoint64.
@param y: A FixedPoint64.
@return bool. If
xis bigger or equal toy.
lte
Checks if x is smaller or equal to y.
@param x: A FixedPoint64.
@param y: A FixedPoint64.
@return bool. If
xis smaller or equal toy.
gte
Checks if x is bigger or equal to y.
@param x: A FixedPoint64.
@param y: A FixedPoint64.
@return bool. If
xis bigger or equal toy.
max
It returns the larger of the two arguments.
@param x: The first operand.
@param y: The second operand.
@return FixedPoint64. The larger argument.
min
It returns the smaller of the two arguments.
@param x: The first operand.
@param y: The second operand.
@return FixedPoint64. The smaller argument.
sub
It returns x - y.
@param x: The first operand.
@param y: The second operand.
@return FixedPoint64. The result of
x-y.
Aborts
y>x
add
It returns x + y.
@param x: The first operand.
@param y: The second operand.
@return FixedPoint64. The result of
x+y.
Aborts
y+x>=MAX_U128
mul
It returns x * y.
Use {mul_128} if you think the values can overflow.
@param x: The first operand.
@param y: The second operand.
@return FixedPoint64. The result of
x*y.
Aborts
inner values overflow.
div
It returns x / y.
@param x: The first operand.
@param y: The second operand.
@return FixedPoint64. The result of
x/y.
Aborts
if
yis zero.
mul_div
Specialized function for x * y / z that omits intermediate shifting.
@param x: A FixedPoint64.
@param y: A FixedPoint64.
@param z: The third operand.
@return FixedPoint64. The result of
x*y/z.
Aborts
if z is zero.
mul_u128
It returns x * y.It multiplies a u128 number with a FixedPoint64. It truncates the fractional part of the product. E.g. - 9 * 0.333 = 2.
@param x: A FixedPoint64.
@param y: A FixedPoint64.
@return u128. The result of
x*ywithout the 64-bit precision.
Aborts
if the result is larger or equal to
MAX_U128.
div_down_u128
It returns numerator / denominator rounded down. It divides a FixedPoint64 by a u128 number.
@param numerator: The first operand, a u128 number.
@param denominator: The first operand, a u128 number.
@return u128. The result of
numerator/denominatorwithout the 64-bit precision.
Aborts
if the result is larger or equal to
MAX_U128.if the
denominatoris zero.
div_up_u128
It returns numerator / denominator rounded up. It divides a FixedPoint64 by a u128 number.
@param numerator: The first operand, a u128 number.
@param denominator: The first operand, a u128 number.
@return u128. The result of
numerator/denominatorwithout the 64-bit precision.
Aborts
if the result is larger or equal to
MAX_U128.if the
denominatoris zero.
pow
It returns base ** exponent.
@param base: The base.
@param exponent: The exponent.
@return FixedPoint64. The result of
base**exponent.
Aborts
if the end result is higher than
MAX_U128.
sqrt
Square root of x.
@param x: The operand.
@return FixedPoint64. The result of the square root.
exp
It performs e^x. Exponent function with a precision of 9 digits.
@param x: The operand.
@return FixedPoint64. The result of e^x.
Last updated
Was this helpful?