Math128
A set of functions to operate over u128 numbers.
Beware that some operations throw on overflow and underflows.
Structs
Constants
const MAX_U128: u256 = 340282366920938463463374607431768211455;Interface
try_add
It tries to perform x + y. Checks for overflow.
public fun try_add(x: u128, y: u128): (bool, u128)@param x: The first operand.
@param y: The second operand.
@return bool. If the operation was successful.
@return u128. The result of
x+y. If it fails, it will be 0.
try_sub
It tries to perform x - y. Checks for underflow.
public fun try_sub(x: u128, y: u128): (bool, u128)@param x: The first operand.
@param y: The second operand.
@return bool. If the operation was successful.
@return u128. The result of
x-y. If it fails, it will be 0.
try_mul
It tries to perform x * y.
public fun try_mul(x: u128, y: u128): (bool, u128)@param x: The first operand.
@param y: The second operand.
@return bool. If the operation was successful.
@return u128. The result of
x*y. If it fails, it will be 0.
try_div_down
It tries to perform x / y rounding down.
public fun try_div_down(x: u128, y: u128): (bool, u128)@param x: The first operand.
@param y: The second operand.
@return bool. If the operation was successful.
@return u128. The result of x / y. If it fails, it will be 0.
try_div_up
It tries to perform x / y rounding up.
public fun try_div_up(x: u128, y: u128): (bool, u128)@param x: The first operand.
@param y: The second operand.
@return bool. If the operation was successful.
@return u128. The result of x / y. If it fails, it will be 0.
try_mul_div_down
It tries to perform x * y / z rounding down. Checks for zero division and overflow.
public fun try_mul_div_down(x: u128, y: u128, z: u128): (bool, u128)@param x: The first operand.
@param y: The second operand.
@param z: The divisor.
@return bool. If the operation was successful.
@return u128. The result of
x*y/z. If it fails, it will be 0.
try_mul_div_up
It tries to perform x * y / z rounding up. Checks for zero division and overflow.
public fun try_mul_div_up(x: u128, y: u128, z: u128): (bool, u128)@param x: The first operand.
@param y: The second operand.
@param z: The divisor.
@return bool. If the operation was successful.
@return u128. The result of
x*y/z. If it fails, it will be 0.
try_mod
It tries to perform x % y.
public fun try_mod(x: u128, y: u128): (bool, u128)@param x: The first operand.
@param y: The second operand.
@param z: The divisor.
@return bool. If the operation was successful.
@return u128. The result of
x%y. If it fails, it will be 0.
mul
It performs x * y.
public fun mul(x: u128, y: u128): u128@param x: The first operand.
@param y: The second operand.
@return u128. The result of
x*y.
div_down
It performs x / y rounding down.
public fun div_down(x: u128, y: u128): u128@param x: The first operand.
@param y: The second operand.
@return u128. The result of
x/y.
div_up
It performs x / y rounding up.
public fun div_up(a: u128, b: u128): u128@param x: The first operand.
@param y: The second operand.
@return u128. The result of
x/y.
mul_div_down
It performs x * y / z rounding down.
public fun mul_div_down(x: u128, y: u128, z: u128): u128@param x: The first operand.
@param y: The second operand.
@param z: The divisor
@return u128. The result of
x*y/z.
mul_div_up
It performs x * y / z rounding up.
public fun mul_div_up(x: u128, y: u128, z: u128): u128@param x: The first operand.
@param y: The second operand.
@param z: The divisor
@return u128. The result of
x*y/z.
min
It returns the lowest number.
public fun min(a: u128, b: u128): u128@param x: The first operand.
@param y: The second operand.
@return u128. The lowest number.
max
It returns the largest number.
public fun max(x: u128, y: u128): u128@param x: The first operand.
@param y: The second operand.
@return u128. The largest number.
clamp
Clamps x between the range of [lower, upper]
public fun clamp(x: u128, lower: u128, upper: u128): u128@param x: The first operand.
@param y: The second operand.
@return u128. The clamped x.
diff
Performs |x - y|.
public fun diff(x: u128, y: u128): u128@param x: The first operand.
@param y: The second operand.
@return u128. The difference.
pow
Performs n^e.
public fun pow(n: u128, e: u128): u128@param n: The base.
@param e: The exponent.
@return u128. The result of n^e.
sum
Adds all x in nums in a vector.
public fun sum(nums: vector<u128>): u128@param nums: A vector of numbers.
@return u256. The sum.
average
It returns the average between two numbers (x + y) / 2.
It does not overflow.
public fun average(a: u128, b: u128): u128@param x: The first operand.
@param y: The second operand.
@return u128. (
x+y) / 2.
average_vector
Calculates the average of the vector of numbers sum of vector/length of vector.
public fun average_vector(nums: vector<u128>): u128@param nums: A vector of numbers.
@return u128. The average.
sqrt_down
Returns the square root of x number. If the number is not a perfect square, the x is rounded down.
public fun sqrt_down(a: u128): u128@param a: The operand.
@return u128. The square root of x rounding down.
sqrt_up
Returns the square root of x number. If the number is not a perfect square, the x is rounded up.
public fun sqrt_up(a: u128): u128@param a: The operand.
@return u128. The square root of x rounding up.
log2_down
Returns the log2(x) rounding down.
public fun log2_down(x: u128): u8@param x: The operand.
@return u8. Log2(x).
log2_up
Returns the log2(x) rounding up.
public fun log2_up(x: u128): u16@param x: The operand.
@return u16. Log2(x).
log10_down
Returns the log10(x) rounding down.
public fun log10_down(x: u128): u8@param x: The operand.
@return u8. Log10(x)
log10_up
Returns the log10(x) rounding up.
public fun log10_up(x: u128): u8@param x: The operand.
@return u8. Log10(x)
log256_down
Returns the log256(x) rounding down.
public fun log256_down(x: u128): u8@param x: The operand.
@return u8. Log256(x).
log256_up
Returns the log256(x) rounding up.
public fun log256_up(x: u128): u8@param x: The operand.
@return u8. Log256(x).
Last updated
Was this helpful?