Math64

A set of functions to operate over u64 numbers.

Structs

Constants

const MAX_U64: u256 = 18446744073709551615;

Interface

wrapping_add

It performs x + y.

It will wrap around the MAX_U64. MAX_U64 + 1 = 0.

public fun wrapping_add(x: u64, y: u64): u64
  • @param x: The first operand.

  • @param y: The second operand.

  • @return u64. The result of x + y.

wrapping_sub

It performs x - y.

It will wrap around zero. 0 - 1 = MAX_U64.

  • @param x: The first operand.

  • @param y: The second operand.

  • @return u64. The result of x - y.

wrapping_mul

It performs x * y.

It will wrap around. MAX_U64 * MAX_U64 = 0.

  • @param x: The first operand.

  • @param y: The second operand.

  • @return u64. The result of x * y.

try_add

It tries to perform x + y. Checks for overflow.

  • @param x: The first operand.

  • @param y: The second operand.

  • @return bool. If the operation was successful.

  • @return u64. The result of x + y. If it fails, it will be 0.

try_sub

It tries to perform x - y. Checks for underflow.

  • @param x: The first operand.

  • @param y: The second operand.

  • @return bool. If the operation was successful.

  • @return u64. The result of x - y. If it fails, it will be 0.

try_mul

It tries to perform x * y.

  • @param x: The first operand.

  • @param y: The second operand.

  • @return bool. If the operation was successful.

  • @return u64. The result of x * y. If it fails, it will be 0.

try_div_down

It tries to perform x / y rounding down.

  • @param x: The first operand.

  • @param y: The second operand.

  • @return bool. If the operation was successful.

  • @return u64. The result of x / y. If it fails, it will be 0.

try_div_up

It tries to perform x / y rounding up.

  • @param x: The first operand.

  • @param y: The second operand.

  • @return bool. If the operation was successful.

  • @return u64. 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.

  • @param x: The first operand.

  • @param y: The second operand.

  • @param z: The divisor.

  • @return bool. If the operation was successful.

  • @return u64. 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.

  • @param x: The first operand.

  • @param y: The second operand.

  • @param z: The divisor.

  • @return bool. If the operation was successful.

  • @return u64. The result of x * y / z. If it fails, it will be 0.

try_mod

It tries to perform x % y.

  • @param x: The first operand.

  • @param y: The second operand.

  • @param z: The divisor.

  • @return bool. If the operation was successful.

  • @return u64. The result of x % y. If it fails, it will be 0.

mul

It performs x * y.

  • @param x: The first operand.

  • @param y: The second operand.

  • @return u64. The result of x * y.

div_down

It performs x / y rounding down.

  • @param x: The first operand.

  • @param y: The second operand.

  • @return u64. The result of x / y.

div_up

It performs x / y rounding up.

  • @param x: The first operand.

  • @param y: The second operand.

  • @return u64. The result of x / y.

mul_div_down

It performs x * y / z rounding down.

  • @param x: The first operand.

  • @param y: The second operand.

  • @param z: The divisor

  • @return u64. The result of x * y / z.

mul_div_up

It performs x * y / z rounding up.

  • @param x: The first operand.

  • @param y: The second operand.

  • @param z: The divisor

  • @return u64. The result of x * y / z.

min

It returns the lowest number.

  • @param x: The first operand.

  • @param y: The second operand.

  • @return u64. The lowest number.

max

It returns the largest number.

  • @param x: The first operand.

  • @param y: The second operand.

  • @return u64. The largest number.

clamp

Clamps x between the range of [lower, upper]

  • @param x: The first operand.

  • @param y: The second operand.

  • @return u64. The clamped x.

diff

Performs |x - y|.

  • @param x: The first operand.

  • @param y: The second operand.

  • @return u64. The difference.

pow

Performs n^e.

  • @param n: The base.

  • @param e: The exponent.

  • @return u128. The result of n^e.

sum

Adds all x in nums in a vector.

  • @param nums: A vector of numbers.

  • @return u64. The sum.

average

It returns the average between two numbers (x + y) / 2.

  • @param x: The first operand.

  • @param y: The second operand.

  • @return u64. (x + y) / 2.

average_vector

Calculates the average of the vector of numbers sum of vector/length of vector.

  • @param nums: A vector of numbers.

  • @return u64. The average.

sqrt_down

Returns the square root of x number. If the number is not a perfect square, the x is rounded down.

  • @param a: The operand.

  • @return u64. 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.

  • @param a: The operand.

  • @return u64. The square root of x rounding up.

log2_down

Returns the log2(x) rounding down.

  • @param x: The operand.

  • @return u8. Log2(x).

log2_up

Returns the log2(x) rounding up.

  • @param x: The operand.

  • @return u16. Log2(x).

log10_down

Returns the log10(x) rounding down.

  • @param x: The operand.

  • @return u8. Log10(x)

log10_up

Returns the log10(x) rounding up.

  • @param x: The operand.

  • @return u8. Log10(x)

log256_down

Returns the log256(x) rounding down.

  • @param x: The operand.

  • @return u8. Log256(x).

log256_up

Returns the log256(x) rounding up.

  • @param x: The operand.

  • @return u8. Log256(x).

Last updated

Was this helpful?