Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
arithmetic: fix PerThing::pow
Browse files Browse the repository at this point in the history
  • Loading branch information
andresilva committed Jun 4, 2021
1 parent 6482591 commit 0db0b56
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions primitives/arithmetic/src/per_things.rs
Original file line number Diff line number Diff line change
Expand Up @@ -639,21 +639,22 @@ macro_rules! implement_per_thing {
impl Pow<usize> for $name {
type Output = Self;

fn pow(self, exp: usize) -> Self::Output {
fn pow(mut self, mut exp: usize) -> Self::Output {
if exp == 0 || self.is_one() {
return Self::one()
}
let mut result = self;
let mut exp = exp - 1;
while exp > 0 && !result.is_zero() {
if exp % 2 == 0 {
result = result.square();
exp /= 2;
} else {

let mut result = Self::one();
while exp > 0 {
if exp % 2 != 0 {
result = result * self;
exp -= 1;
}

self = self.square();
exp /= 2;
}

result
}
}
Expand Down

0 comments on commit 0db0b56

Please sign in to comment.