remove the panic

This commit is contained in:
Jędrzej Stuczyński
2023-11-24 09:31:46 +00:00
parent ef8e452f30
commit 4c8fa74dfe
@@ -75,9 +75,14 @@ impl Percent {
pub fn pow(&self, exp: u32) -> Self {
match self.0.checked_pow(exp) {
Ok(res) => Percent(res),
Err(overflow) => {
// since the percent is meant to always be strictly less than 1, this should NEVER hapen
panic!("the percent invariant has been broken. the exponentiation result has overflown: {overflow}");
Err(_overflow) => {
// since the percent is meant to always be less than 1, this should NEVER hapen, however,
// when the inevitable happens because of some misuse, just saturate the result
if self.0 < Decimal::one() {
Percent::zero()
} else {
Percent(Decimal::MAX)
}
}
}
}