Skip to content

concordia-fi/rational_math

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Rational Math

Building and testing

Install the Aptos CLI then:

aptos move compile --named-addresses rational_math=<address>
aptos move test --named-addresses rational_math=<address>

Features

Structs

Decimal struct has a value and a scale. Scale is an exponent of 10.

struct Decimal {
    value: u128,
    scale: u8
  }

Utility Functions

new

Returns a decimal with the given value and scale.

public fun new(v: u128, s: u8): Decimal 
is_zero

Returns true if the decimal is zero.

  public fun is_zero(d: &Decimal): bool 
adjust_scale

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
denominator

Returns 10 ^ scale.

  public fun denominator(d: &Decimal): u128
pow

Returns base ^ exponent.

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

Arithmetic

add

Adds two decimals of the same scale.

  public fun add(d1: Decimal, d2: Decimal): Decimal
  • Panics on different scales
sub

Subtracts two decimals of the same scale.

public fun sub(larger: Decimal, smaller: Decimal): Decimal 
  • Panics on different scales
mul

Multiples two decimals of same or different scales.

public fun mul(d1: Decimal, d2: Decimal): Decimal
  • Keeps the scale of the first decimal
div_floor

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
div_ceiling

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

Comparison

eq

Returns true if the two decimals are equal.

public fun eq(d1: Decimal, d2: Decimal): bool 
lt

Returns true if the first decimal is less than the second.

public fun lt(d1: Decimal, d2: Decimal): bool 
gt

Returns true if the first decimal is greater than the second.

public fun gt(d1: Decimal, d2: Decimal): bool 
lte

Returns true if the first decimal is less than or equal to the second.

public fun le(d1: Decimal, d2: Decimal): bool 
gte

Returns true if the first decimal is greater than or equal to the second.

public fun ge(d1: Decimal, d2: Decimal): bool 

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages