diff --git a/src/Magnum/Math/Test/UnitTest.cpp b/src/Magnum/Math/Test/UnitTest.cpp index 2dbace246..bfee1fe57 100644 --- a/src/Magnum/Math/Test/UnitTest.cpp +++ b/src/Magnum/Math/Test/UnitTest.cpp @@ -48,6 +48,7 @@ struct UnitTest: TestSuite::Tester { void addSubtract(); void multiplyDivide(); void multiplyDivideIntegral(); + void modulo(); }; UnitTest::UnitTest() { @@ -63,7 +64,8 @@ UnitTest::UnitTest() { &UnitTest::promotedNegated, &UnitTest::addSubtract, &UnitTest::multiplyDivide, - &UnitTest::multiplyDivideIntegral}); + &UnitTest::multiplyDivideIntegral, + &UnitTest::modulo}); } /* What's a typedef and not a using differs from the typedefs in root Magnum @@ -297,6 +299,25 @@ void UnitTest::multiplyDivideIntegral() { CORRADE_COMPARE(cc, Seci{125}); } +void UnitTest::modulo() { + CORRADE_COMPARE(Seci{255}%Seci{64}, Seci{63}); + CORRADE_COMPARE(Seci{-6}%Seci{-4}, Seci{-2}); + + Seci a{255}; + Seci b{-6}; + a %= Seci{64}; + b %= Seci{-4}; + CORRADE_COMPARE(a, Seci{63}); + CORRADE_COMPARE(b, Seci{-2}); + + constexpr Seci ca{255}; + constexpr Seci cb{-6}; + constexpr Seci cc = ca % Seci{64}; + constexpr Seci cd = cb % Seci{-4}; + CORRADE_COMPARE(cc, Seci{63}); + CORRADE_COMPARE(cd, Seci{-2}); +} + }}}} CORRADE_TEST_MAIN(Magnum::Math::Test::UnitTest) diff --git a/src/Magnum/Math/Unit.h b/src/Magnum/Math/Unit.h index 34cb498a9..25e338fd9 100644 --- a/src/Magnum/Math/Unit.h +++ b/src/Magnum/Math/Unit.h @@ -257,6 +257,37 @@ template class Derived, class T> class Unit { return _value/other._value; } + /** + * @brief Do modulo of a value and assign + * @m_since_latest + * + * Enabled only for integral types. + */ + #ifdef DOXYGEN_GENERATING_OUTPUT + Unit& + #else + template typename std::enable_if::value, Unit&>::type + #endif + operator%=(Unit other) { + _value %= other._value; + return *this; + } + + /** + * @brief Modulo of a value + * @m_since_latest + * + * Enabled only for integral types. + */ + #ifdef DOXYGEN_GENERATING_OUTPUT + constexpr Unit + #else + template constexpr typename std::enable_if::value, Unit>::type + #endif + operator%(Unit other) const { + return Unit{_value%other._value}; + } + private: T _value; };