Skip to content

Latest commit

 

History

History
130 lines (66 loc) · 3.39 KB

u128.md

File metadata and controls

130 lines (66 loc) · 3.39 KB

Module 0x1::u128

Function max

Return the larger of x and y

public fun max(x: u128, y: u128): u128

Function min

Return the smaller of x and y

public fun min(x: u128, y: u128): u128

Function diff

Return the absolute value of x - y

public fun diff(x: u128, y: u128): u128

Function divide_and_round_up

Calculate x / y, but round up the result.

public fun divide_and_round_up(x: u128, y: u128): u128

Function multiple_and_divide

Returns x * y / z with as little loss of precision as possible and avoid overflow

public fun multiple_and_divide(x: u128, y: u128, z: u128): u128

Function pow

Return the value of a base raised to a power

public fun pow(base: u128, exponent: u8): u128

Function sqrt

Get a nearest lower integer Square Root for x. Given that this function can only operate with integers, it is impossible to get perfect (or precise) integer square root for some numbers.

Example:

math::sqrt(9) => 3
math::sqrt(8) => 2 // the nearest lower square root is 4;

In integer math, one of the possible ways to get results with more precision is to use higher values or temporarily multiply the value by some bigger number. Ideally if this is a square of 10 or 100.

Example:

math::sqrt(8) => 2;
math::sqrt(8 * 10000) => 282;
// now we can use this value as if it was 2.82;
// but to get the actual result, this value needs
// to be divided by 100 (because sqrt(10000)).


math::sqrt(8 * 1000000) => 2828; // same as above, 2828 / 1000 (2.828)
public fun sqrt(x: u128): u128