Fixed Point Roll

A set of functions to operate over u64 numbers with 1e9 precision.

Interface

roll

It returns 1 ROLL - 1_000_000_000.

public fun roll(): u64
  • @return u64. 1e9

try_mul_down

It tries to x * y / 1_000_000_000 rounding down. It returns zero instead of throwing an overflow error.

public fun try_mul_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 / 1_000_000_000.

try_mul_up

It tries to x * y / 1_000_000_000 rounding up. It returns zero instead of throwing an overflow error.

public fun try_mul_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 / 1_000_000_000.

try_div_down

It tries to x * 1_000_000_000 / y rounding down. It returns zero instead of throwing an overflow error.

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 * 1_000_000_000 / y.

try_div_up

It tries to x * 1_000_000_000 / y rounding up. It returns zero instead of throwing an overflow error.

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 * 1_000_000_000 / y.

mul_down

x * y / 1_000_000_000 rounding down.

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

  • @param y: The second operand.

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

Aborts

  • On overflow. If the result is over the maximum u256 number.

mul_up

x * y / 1_000_000_000 rounding up.

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

  • @param y: The second operand.

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

Aborts

  • On overflow. If the result is over the maximum u256 number.

div_down

x * 1_000_000_000 / 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 * 1_000_000_000 / y.

Aborts

  • On zero division.

div_up

x * 1_000_000_000 / y rounding up.

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

  • @param y: The second operand.

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

Aborts

  • On zero division.

to_roll

It converts x precision to a ROLL, a number with a precision of 1e9.

public fun to_roll(x: u64, decimal_factor: u64): u64
  • @param x: The value to be converted.

  • @param decimal_factor: The current decimal scalar of x.

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

Aborts

  • decimal_factor is zero.

Last updated