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 475a73f
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions primitives/arithmetic/src/per_things.rs
Original file line number Diff line number Diff line change
Expand Up @@ -639,22 +639,23 @@ 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 > 1 {
if exp % 2 != 0 {
result = result * self;
exp -= 1;
}

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

result * self
}
}

Expand Down

0 comments on commit 475a73f

Please sign in to comment.