Skip to content

Commit

Permalink
add
Browse files Browse the repository at this point in the history
  • Loading branch information
ledyba-z committed Apr 3, 2022
1 parent c911c4c commit e31832f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/avif/math/Fraction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ struct Fraction {
int32_t const r = std::lcm(std::abs(denominator_), std::abs(b.denominator_));
return Fraction((r / denominator_ * numerator_) - (r / b.denominator_ * b.numerator_), r).reduce();
}
[[ nodiscard ]] constexpr Fraction add(Fraction const& b) const {
int32_t const r = std::lcm(std::abs(denominator_), std::abs(b.denominator_));
return Fraction((r / denominator_ * numerator_) + (r / b.denominator_ * b.numerator_), r).reduce();
}
[[ nodiscard ]] constexpr int32_t numerator() const {
return numerator_;
}
Expand Down
10 changes: 10 additions & 0 deletions test/math/FractionTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,13 @@ TEST(FractionTest, Minus) {
ASSERT_EQ(-1, v.numerator());
ASSERT_EQ(2, v.denominator());
}

TEST(FractionTest, Add) {
using avif::math::Fraction;

auto const one = Fraction(1, 1);
auto const half = one.div(2);
auto const v = half.add(one);
ASSERT_EQ(3, v.numerator());
ASSERT_EQ(2, v.denominator());
}

0 comments on commit e31832f

Please sign in to comment.