Skip to content

Commit

Permalink
Calculate GCD in Ratio::new
Browse files Browse the repository at this point in the history
  • Loading branch information
krakow10 committed Feb 1, 2025
1 parent 1d64ce6 commit 3551030
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/animation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,14 @@ impl From<Delay> for Duration {
}
}

#[inline]
const fn gcd(mut a: u32, mut b: u32) -> u32 {
while b != 0 {
(a, b) = (b, a.rem_euclid(b));
}
a
}

#[derive(Copy, Clone, Debug)]
pub(crate) struct Ratio {
numer: u32,
Expand All @@ -314,9 +322,10 @@ impl Ratio {
#[inline]
pub(crate) fn new(numerator: u32, denominator: u32) -> Self {
assert_ne!(denominator, 0);
let divisor = gcd(numerator, denominator);
Self {
numer: numerator,
denom: denominator,
numer: numerator / divisor,
denom: denominator / divisor,
}
}

Expand Down

0 comments on commit 3551030

Please sign in to comment.