hmath/ratio/arith/
pow.rs

1use crate::Ratio;
2
3impl Ratio {
4
5    #[must_use = "method returns a new number and does not mutate the original value"]
6    pub fn pow_i32(&self, exp: i32) -> Self {
7
8        // Safety: if a and b are coprime, a^n and b^n are coprime.
9        let mut result = Ratio::from_denom_and_numer_raw(
10            self.denom.pow_u32(exp.abs() as u32),
11            self.numer.pow_u32(exp.abs() as u32),
12        );
13
14        if exp < 0 {
15            result.reci_mut();
16        }
17
18        #[cfg(test)] assert!(result.is_valid());
19
20        result
21    }
22
23    pub fn pow_i32_mut(&mut self, exp: i32) {
24        self.denom.pow_u32_mut(exp.abs() as u32);
25        self.numer.pow_u32_mut(exp.abs() as u32);
26
27        if exp < 0 {
28            self.reci_mut();
29        }
30
31        #[cfg(test)] assert!(self.is_valid());
32    }
33
34}