stellar_contract_utils/math/
fixed_point.rs

1use soroban_sdk::{contracttype, Env};
2
3use crate::math::soroban_fixed_point::SorobanFixedPoint;
4
5#[contracttype]
6pub enum Rounding {
7    Floor, // Toward negative infinity
8    Ceil,  // Toward positive infinity
9}
10
11/**
12 * Calculates x * y / denominator with full precision, following the
13 * selected rounding direction. Throws if result overflows a i128 or
14 * denominator is zero (handles phantom overflow).
15 */
16pub fn muldiv(e: &Env, x: i128, y: i128, denominator: i128, rounding: Rounding) -> i128 {
17    match rounding {
18        Rounding::Floor => x.fixed_mul_floor(e, &y, &denominator),
19        Rounding::Ceil => x.fixed_mul_ceil(e, &y, &denominator),
20    }
21}