From e31832f5d1bfa64b1848e4adb148286934b488fb Mon Sep 17 00:00:00 2001 From: psi Date: Sun, 3 Apr 2022 15:06:44 +0900 Subject: [PATCH] add --- src/avif/math/Fraction.hpp | 4 ++++ test/math/FractionTest.cpp | 10 ++++++++++ 2 files changed, 14 insertions(+) diff --git a/src/avif/math/Fraction.hpp b/src/avif/math/Fraction.hpp index d8eefc0..1f3f24b 100644 --- a/src/avif/math/Fraction.hpp +++ b/src/avif/math/Fraction.hpp @@ -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_; } diff --git a/test/math/FractionTest.cpp b/test/math/FractionTest.cpp index bde2968..71c2ed8 100644 --- a/test/math/FractionTest.cpp +++ b/test/math/FractionTest.cpp @@ -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()); +}