Skip to main content

Module math

Module math 

Source
Expand description

Checked arithmetic operations.

All operations return ProgramError::ArithmeticOverflow on failure. No panics on-chain. u128 intermediates where needed to prevent premature overflow on real token amounts.

Functionsยง

bps_of
Basis-point fee (floor): amount * bps / 10_000.
bps_of_ceil
Basis-point fee (ceiling): ceil(amount * bps / 10_000).
checked_add
Checked addition.
checked_add_i64
Checked addition for i64.
checked_div
Checked division (returns error on divide by zero).
checked_div_ceil
Checked ceiling division: ceil(a / b).
checked_mul
Checked multiplication.
checked_mul_div
Compute (a * b) / c with u128 intermediate to prevent overflow.
checked_mul_div_ceil
Compute ceil((a * b) / c) with u128 intermediate.
checked_pow
Checked exponentiation via repeated squaring.
checked_sub
Checked subtraction.
checked_sub_i64
Checked subtraction for i64.
div_ceil
Compute ceil(a / b) without overflow (for u64).
scale_amount
Scale a token amount between different decimal precisions (floor).
scale_amount_ceil
Scale a token amount between decimal precisions, rounding up.
scale_bps
Scale a value in basis points (BPS). value * bps / 10_000, with overflow protection via u128 intermediate.
scale_fraction
Scale a value by a fraction (numerator / denominator).
to_u64
Safe narrowing cast from u128 to u64.