soroban_fixed_point_math/soroban_fixed_point.rs
1use soroban_sdk::Env;
2
3// @dev - more detail about the forced panic can be found here: https://github.com/stellar/rs-soroban-env/pull/1091
4//
5/// Soroban fixed point trait for computing fixed point calculations with Soroban host objects.
6///
7/// Soroban host functions by default are non-recoverable. This means an arithmetic overflow or divide by zero will
8/// result in a host panic instead of returning an error. For consistency, this trait will also panic in the same manner.
9pub trait SorobanFixedPoint: Sized {
10 /// Safely calculates floor(x * y / denominator).
11 ///
12 /// ### Panics
13 /// This method will panic if the denominator is 0, a phantom overflow occurs, or
14 /// the result does not fit in Self.
15 fn fixed_mul_floor(&self, env: &Env, y: &Self, denominator: &Self) -> Self;
16
17 /// Safely calculates ceil(x * y / denominator).
18 ///
19 /// ### Panics
20 /// This method will panic if the denominator is 0, a phantom overflow occurs, or
21 /// the result does not fit in Self.
22 fn fixed_mul_ceil(&self, env: &Env, y: &Self, denominator: &Self) -> Self;
23
24 /// Safely calculates floor(x * denominator / y).
25 ///
26 /// ### Panics
27 /// This method will panic if the denominator is 0, a phantom overflow occurs, or
28 /// the result does not fit in Self.
29 fn fixed_div_floor(&self, env: &Env, y: &Self, denominator: &Self) -> Self;
30
31 /// Safely calculates ceil(x * denominator / y).
32 ///
33 /// ### Panics
34 /// This method will panic if the denominator is 0, a phantom overflow occurs, or
35 /// the result does not fit in Self.
36 fn fixed_div_ceil(&self, env: &Env, y: &Self, denominator: &Self) -> Self;
37}