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) / cwith 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.