Math256
Structs
Constants
const MAX_U256: u256 = 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff;Interface
try_add
It tries to perform x + y. Checks for overflow.
public fun try_add(x: u256, y: u256): (bool, u256)@param x: The first operand.
@param y: The second operand.
@return bool. If the operation was successful.
@return u256. 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: u256, y: u256): (bool, u256)@param x: The first operand.
@param y: The second operand.
@return bool. If the operation was successful.
@return u256. 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: u256, y: u256): (bool, u256)@param x: The first operand.
@param y: The second operand.
@return bool. If the operation was successful.
@return u256. 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: u256, y: u256): (bool, u256)@param x: The first operand.
@param y: The second operand.
@return bool. If the operation was successful.
@return u256. 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: u256, y: u256): (bool, u256)@param x: The first operand.
@param y: The second operand.
@return bool. If the operation was successful.
@return u256. 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: u256, y: u256, z: u256): (bool, u256)@param x: The first operand.
@param y: The second operand.
@param z: The divisor.
@return bool. If the operation was successful.
@return u256. 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.
public fun try_mul_div_up(x: u256, y: u256, z: u256): (bool, u256)@param x: The first operand.
@param y: The second operand.
@param z: The divisor.
@return bool. If the operation was successful.
@return u256. 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: u256, y: u256): (bool, u256)@param x: The first operand.
@param y: The second operand.
@param z: The divisor.
@return bool. If the operation was successful.
@return u256. The result of
x%y. If it fails, it will be 0.
mul
It performs x * y.
public fun mul(x: u256, y: u256): u256@param x: The first operand.
@param y: The second operand.
@return u256. The result of
x*y.
div_down
It performs x / y rounding down.
public fun div_down(x: u256, y: u256): u256@param x: The first operand.
@param y: The second operand.
@return u256. The result of
x/y.
Abort
It will throw on zero division.
div_up
It performs x / y rounding up.
public fun div_up(x: u256, y: u256): u256@param x: The first operand.
@param y: The second operand.
@return u256. The result of
x/y.
Abort
It will throw on zero division.
mul_div_down
It performs x * y / z rounding down.
public fun mul_div_down(x: u256, y: u256, z: u256): u256@param x: The first operand.
@param y: The second operand.
@param z: The divisor
@return u256. The result of
x*y/z.
mul_div_up
It performs x * y / z rounding up.
public fun mul_div_up(x: u256, y: u256, z: u256): u256@param x: The first operand.
@param y: The second operand.
@param z: The divisor
@return u256. The result of
x*y/z.
min
It returns the lowest number.
public fun min(x: u256, y: u256): u256@param x: The first operand.
@param y: The second operand.
@return u256. The lowest number.
max
It returns the largest number.
public fun max(x: u256, y: u256): u256@param x: The first operand.
@param y: The second operand.
@return u256. The largest number.
clamp
Clamps x between the range of [lower, upper]
public fun clamp(x: u256, lower: u256, upper: u256): u256@param x: The first operand.
@param y: The second operand.
@return u256. The clamped x.
diff
Performs |x - y|.
public fun diff(x: u256, y: u256): u256@param x: The first operand.
@param y: The second operand.
@return u256. The difference.
pow
Performs n^e.
public fun pow(n: u256, e: u256): u256@param n: The base.
@param e: The exponent.
@return u256. The result of n^e.
sum
Adds all x in nums in a vector.
public fun sum(nums: vector<u256>): u256@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(x: u256, y: u256): u256@param x: The first operand.
@param y: The second operand.
@return u256. (
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<u256>): u256@param nums: A vector of numbers.
@return u256. 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(x: u256): u256@param a: The operand.
@return u256. 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(x: u256): u256@param a: The operand.
@return u256. The square root of x rounding up.
log2_down
Returns the log2(x) rounding down.
public fun log2_down(x: u256): u8@param x: The operand.
@return u8. Log2(x).
log2_up
Returns the log2(x) rounding up.
public fun log2_up(x: u256): u16@param x: The operand.
@return u16. Log2(x).
log10_down
Returns the log10(x) rounding down.
public fun log10_down(x: u256): u8@param x: The operand.
@return u8. Log10(x)
log10_up
Returns the log10(x) rounding up.
public fun log10_up(x: u256): u8@param x: The operand.
@return u8. Log10(x)
log256_down
Returns the log256(x) rounding down.
public fun log256_down(x: u256): u8@param x: The operand.
@return u8. Log256(x).
log256_up
Returns the log256(x) rounding up.
public fun log256_up(x: u256): u8@param x: The operand.
@return u8. Log256(x).
Last updated
Was this helpful?