Install the Aptos CLI then:
aptos move compile --named-addresses rational_math=<address>
aptos move test --named-addresses rational_math=<address>
Decimal struct has a value and a scale. Scale is an exponent of 10.
struct Decimal {
value: u128,
scale: u8
}
Returns a decimal with the given value and scale.
public fun new(v: u128, s: u8): Decimal
Returns true if the decimal is zero.
public fun is_zero(d: &Decimal): bool
Adjusts the scale of the decimal to the given scale.
public fun adjust_scale(d: &mut Decimal, new_scale: u8)
- Cannot be used to set the scale to 0
Returns 10 ^ scale.
public fun denominator(d: &Decimal): u128
Returns base ^ exponent.
public fun pow(base: u128, exponent: u8): u128
Adds two decimals of the same scale.
public fun add(d1: Decimal, d2: Decimal): Decimal
- Panics on different scales
Subtracts two decimals of the same scale.
public fun sub(larger: Decimal, smaller: Decimal): Decimal
- Panics on different scales
Multiples two decimals of same or different scales.
public fun mul(d1: Decimal, d2: Decimal): Decimal
- Keeps the scale of the first decimal
Divides two decimals of same or different scales with floor division.
public fun div_floor(d1: Decimal, d2: Decimal): Decimal
- Keeps the scale of the first decimal
- Panics if the second decimal is zero
Divides two decimals of same or different scales with ceiling division.
public fun div_ceiling(d1: Decimal, d2: Decimal): Decimal
- Keeps the scale of the first decimal
- Panics if the second decimal is zero
Returns true if the two decimals are equal.
public fun eq(d1: Decimal, d2: Decimal): bool
Returns true if the first decimal is less than the second.
public fun lt(d1: Decimal, d2: Decimal): bool
Returns true if the first decimal is greater than the second.
public fun gt(d1: Decimal, d2: Decimal): bool
Returns true if the first decimal is less than or equal to the second.
public fun le(d1: Decimal, d2: Decimal): bool
Returns true if the first decimal is greater than or equal to the second.
public fun ge(d1: Decimal, d2: Decimal): bool