Skip to main content

Ratio

Represents a ratio of two integers, which can be used to represent arbitrary precision numbers on-chain.

Associated functions​

from_data​

Ratio::from_data(data: Data) -> Ratio

new​

Ratio::new(top: Int, bottom: Int) -> Ratio

Getters​

bottom​

The denominator of the Ratio.

ratio.bottom -> Int

top​

The numerator of the Ratio.

ratio.top -> Int

Operators​

==

Exact equals: returns true if the denominators are equal and the numerators are equal.

Ratio == Ratio -> Bool

Use ratio.equals() for a non-exact equality comparison.

!=​

Exact not-equals: returns false if the denomitors aren't equal or the numerators aren't equal.

Ratio != Ratio -> Bool

+​

Ratio + Ratio -> Ratio
Ratio + Int -> Ratio

-​

Ratio - Ratio -> Ratio
Ratio - Int -> Ratio

*​

Ratio * Ratio -> Ratio
Ratio * Int -> Ratio

/​

Ratio / Ratio -> Ratio
Ratio / Int -> Ratio

<​

Ratio < Ratio -> Bool
Ratio < Int -> Bool

<=​

Ratio <= Ratio -> Bool
Ratio <= Int -> Bool

>​

Ratio > Ratio -> Bool
Ratio > Int -> Bool

>=​

Ratio >= Ratio -> Bool
Ratio >= Int -> Bool

Methods​

ceil​

Rounds a Ratio upwards, returning an Int.

ratio.ceil() -> Int

equals​

Non-exact equality comparison, which checks that a.top * b.bottom == b.top * a.bottom.

a.equals(b: Ratio) -> Bool

floor​

Rounds a Ratio downwards, returning an Int.

ratio.floor() -> Int

round​

Rounds a Ratio to the nearest Int.

ratio.round() -> Int

to_real​

Truncates the Ratio to Real (a Real is equivalent to a Ratio with 1_000_000 as a denominator).

ratio.to_real() -> Real

trunc​

Rounds a Ratio towards zero, returning an Int.

ratio.trunc() -> Int