Math64

A set of functions to operate over u64 numbers.

Beware that some operations throw on overflow and underflows.

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.

public fun wrapping_sub(x: u64, y: u64): 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.

public fun wrapping_mul(x: u64, y: u64): u64
  • @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.

public fun try_add(x: u64, y: u64): (bool, u64)
  • @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.

public fun try_sub(x: u64, y: u64): (bool, u64)
  • @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.

public fun try_mul(x: u64, y: u64): (bool, u64)
  • @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.

public fun try_div_down(x: u64, y: u64): (bool, u64)
  • @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.

public fun try_div_up(x: u64, y: u64): (bool, u64)
  • @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.

public fun try_mul_div_down(x: u64, y: u64, z: u64): (bool, u64)
  • @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.

public fun try_mul_div_up(x: u64, y: u64, z: u64): (bool, u64)
  • @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.

public fun try_mod(x: u64, y: u64): (bool, u64)
  • @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.

public fun mul(x: u64, y: u64): u64
  • @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.

public fun div_down(x: u64, y: u64): u64
  • @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.

public fun div_up(a: u64, b: u64): u64
  • @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.

public fun mul_div_down(x: u64, y: u64, z: u64): u64
  • @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.

public fun mul_div_up(x: u64, y: u64, z: u64): u64
  • @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.

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

  • @param y: The second operand.

  • @return u64. The lowest number.

max

It returns the largest number.

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

  • @param y: The second operand.

  • @return u64. The largest number.

clamp

Clamps x between the range of [lower, upper]

public fun clamp(x: u64, lower: u64, upper: u64): u64
  • @param x: The first operand.

  • @param y: The second operand.

  • @return u64. The clamped x.

diff

Performs |x - y|.

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

  • @param y: The second operand.

  • @return u64. 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<u64>): u64
  • @param nums: A vector of numbers.

  • @return u64. The sum.

average

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

It does not overflow.

public fun average(x: u64, y: u64): u64
  • @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.

public fun average_vector(nums: vector<u64>): u64
  • @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.

public fun sqrt_down(x: u64): u64
  • @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.

public fun sqrt_up(a: u64): u64
  • @param a: The operand.

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

log2_down

Returns the log2(x) rounding down.

public fun log2_down(value: u64): u8
  • @param x: The operand.

  • @return u8. Log2(x).

log2_up

Returns the log2(x) rounding up.

public fun log2_up(value: u64): u16
  • @param x: The operand.

  • @return u16. Log2(x).

log10_down

Returns the log10(x) rounding down.

public fun log10_down(value: u64): u8
  • @param x: The operand.

  • @return u8. Log10(x)

log10_up

Returns the log10(x) rounding up.

public fun log10_up(value: u64): u8 
  • @param x: The operand.

  • @return u8. Log10(x)

log256_down

Returns the log256(x) rounding down.

public fun log256_down(x: u64): u8
  • @param x: The operand.

  • @return u8. Log256(x).

log256_up

Returns the log256(x) rounding up.

public fun log256_up(x: u64): u8
  • @param x: The operand.

  • @return u8. Log256(x).

Last updated