Fixed Point 64

A library to perform math operations over an unsigned integer with 64-bit precision. Any operation that results in a number larger than the maximum unsigned 128 bit, will be considered an overflow and throw.

Structs

FixedPoint64

struct FixedPoint64 has copy, drop, store { value: u128 }
  • value - The number.

Interface

value

It returns the raw u128 value.

public fun value(self: FixedPoint64): u128
  • @param self: A FixedPoint64

  • @return u128. The raw u128 value.

from

Creates a FixedPoint64 from a u128 number. It scales the number.

public fun from(value: u128): FixedPoint64
  • @param value: A u128 number.

  • @return A FixedPoint64. calculated by right shifting - value << 64.

Aborts

  • The left-shifted value is larger than MAX_U128.

from_raw_value

Creates a FixedPoint64 from a u128 value. It does not scale the value.

public fun from_raw_value(value: u128): FixedPoint64
  • @param value: A u128 number.

  • @return FixedPoint64. It wraps the u128.

from_rational

Creates a FixedPoint64 from a rational number specified by a numerator anddenominator.

0.0125 will round down to 0.012 instead of up to 0.013.

public fun from_rational(numerator: u128, denominator: u128): FixedPoint64
  • @param numerator: The numerator of the rational number.

  • @param denominator: The denominator of the rational number.

  • @return FixedPoint64. A FixedPoint64 from (numerator << 64) / denominator.

Aborts

  • if the denominator is zero

  • if the numerator / denominator is zero

  • if the numerator is nonzero and the ratio is not in the range 2^-64 .. 2^64-1

to_u128

Converts a FixedPoint64 into a u128 number to the closest integer.

public fun to_u128(self: FixedPoint64): u128
  • @param self. A FixedPoint64.

  • @return u128.

to_u128_down

Converts a FixedPoint64 into a u128 number rounding down.

public fun to_u128_down(self: FixedPoint64): u128
  • @param self. A FixedPoint64.

  • @return u128.

to_u128_up

Converts a FixedPoint64 into a u128 number rounding up.

public fun to_u128_up(self: FixedPoint64): u128
  • @param self. A FixedPoint64.

  • @return u128.

is_zero

Checks if self is zero.

public fun is_zero(self: FixedPoint64): bool
  • @param self. A FixedPoint64.

  • @return bool. If the self.value is zero.

eq

Checks if x is equal to y.

public fun eq(x: FixedPoint64, y: FixedPoint64): bool
  • @param x: A FixedPoint64.

  • @param y: A FixedPoint64.

  • @return bool. If the values are equal.

lt

Checks if x is smaller or equal to y.

public fun lt(x: FixedPoint64, y: FixedPoint64): bool
  • @param x: A FixedPoint64.

  • @param y: A FixedPoint64.

  • @return bool. If x is smaller than y.

gt

Checks if x is bigger than y.

public fun gt(x: FixedPoint64, y: FixedPoint64): bool
  • @param x: A FixedPoint64.

  • @param y: A FixedPoint64.

  • @return bool. If x is bigger or equal to y.

lte

Checks if x is smaller or equal to y.

public fun lte(x: FixedPoint64, y: FixedPoint64): bool
  • @param x: A FixedPoint64.

  • @param y: A FixedPoint64.

  • @return bool. If x is smaller or equal to y.

gte

Checks if x is bigger or equal to y.

public fun gte(x: FixedPoint64, y: FixedPoint64): bool
  • @param x: A FixedPoint64.

  • @param y: A FixedPoint64.

  • @return bool. If x is bigger or equal to y.

max

It returns the larger of the two arguments.

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

  • @param y: The second operand.

  • @return FixedPoint64. The larger argument.

min

It returns the smaller of the two arguments.

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

  • @param y: The second operand.

  • @return FixedPoint64. The smaller argument.

sub

It returns x - y.

public fun sub(x: FixedPoint64, y: FixedPoint64): FixedPoint64
  • @param x: The first operand.

  • @param y: The second operand.

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

Aborts

  • y > x

add

It returns x + y.

public fun add(x: FixedPoint64, y: FixedPoint64): FixedPoint64
  • @param x: The first operand.

  • @param y: The second operand.

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

Aborts

  • y + x >= MAX_U128

mul

It returns x * y.

Use {mul_128} if you think the values can overflow.

public fun mul(x: FixedPoint64, y: FixedPoint64): FixedPoint64
  • @param x: The first operand.

  • @param y: The second operand.

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

Aborts

  • inner values overflow.

div

It returns x / y.

public fun div(x: FixedPoint64, y: FixedPoint64): FixedPoint64
  • @param x: The first operand.

  • @param y: The second operand.

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

Aborts

  • if y is zero.

mul_div

Specialized function for x * y / z that omits intermediate shifting.

public fun mul_div(x: FixedPoint64, y: FixedPoint64, z: FixedPoint64): FixedPoint64
  • @param x: A FixedPoint64.

  • @param y: A FixedPoint64.

  • @param z: The third operand.

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

Aborts

  • if z is zero.

mul_u128

It returns x * y.It multiplies a u128 number with a FixedPoint64. It truncates the fractional part of the product. E.g. - 9 * 0.333 = 2.

public fun mul_u128(x: u128, y: FixedPoint64): u128
  • @param x: A FixedPoint64.

  • @param y: A FixedPoint64.

  • @return u128. The result of x * y without the 64-bit precision.

Aborts

  • if the result is larger or equal to MAX_U128.

div_down_u128

It returns numerator / denominator rounded down. It divides a FixedPoint64 by a u128 number.

public fun div_down_u128(numerator: u128, denominator: FixedPoint64): u128
  • @param numerator: The first operand, a u128 number.

  • @param denominator: The first operand, a u128 number.

  • @return u128. The result of numerator / denominator without the 64-bit precision.

Aborts

  • if the result is larger or equal to MAX_U128.

  • if the denominator is zero.

div_up_u128

It returns numerator / denominator rounded up. It divides a FixedPoint64 by a u128 number.

public fun div_up_u128(numerator: u128, denominator: FixedPoint64): u128
  • @param numerator: The first operand, a u128 number.

  • @param denominator: The first operand, a u128 number.

  • @return u128. The result of numerator / denominator without the 64-bit precision.

Aborts

  • if the result is larger or equal to MAX_U128.

  • if the denominator is zero.

pow

It returns base ** exponent.

public fun pow(base: FixedPoint64, exponent: u64): FixedPoint64
  • @param base: The base.

  • @param exponent: The exponent.

  • @return FixedPoint64. The result of base ** exponent.

Aborts

  • if the end result is higher than MAX_U128.

sqrt

Square root of x.

public fun sqrt(x: FixedPoint64): FixedPoint64
  • @param x: The operand.

  • @return FixedPoint64. The result of the square root.

exp

It performs e^x. Exponent function with a precision of 9 digits.

public fun exp(x: FixedPoint64): FixedPoint64
  • @param x: The operand.

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

Last updated