Fixed Point Wad

A set of functions to operate over u256 numbers with 1e18 precision. It emulates the decimal precision of ERC20 to port some of their advanced math operations such as exp and exp and ln.

Interface

wad

It returns 1 WAD - 1_000_000_000_000_000_000.

public fun wad(): u256
  • @return u64. 1e18

try_mul_down

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

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

try_mul_up

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

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

try_div_down

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

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

try_div_up

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

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

mul_down

x * y / 1_000_000_000_000_000_000 rounding down.

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

  • @param y: The second operand.

  • @return u256. The result of x * y / 1_000_000_000_000_000_000.

Aborts

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

mul_up

x * y / 1_000_000_000_000_000_000 rounding up.

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

  • @param y: The second operand.

  • @return u256. The result of x * y / 1_000_000_000_000_000_000.

Aborts

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

div_down

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

Aborts

  • On zero division.

div_up

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

Aborts

  • On zero division.

to_wad

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

public fun to_wad(x: u256, decimal_factor: u256): u256
  • @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_000_000_000 / y.

Aborts

  • decimal_factor is zero.

exp

It calculates e^x.

All credits to Remco Bloemen and more information here: https://xn--2-umb.com/22/exp-ln/

public fun exp(x: Int): Int
  • @param x: The exponent.

  • @return Int. The result of e^x.

Aborts

  • x is larger than 135305999368893231589.

ln

It calculates ln(x).

All credits to Remco Bloemen and more information here: https://xn--2-umb.com/22/exp-ln/

public fun ln(x: Int): Int
  • @param x: The operand.

  • @return Int. The result of ln(x).

Aborts

  • x is negative or zero.

Last updated