Browse Source

Math: implement integer modulo for Unit.

It might eventually make sense to add a modulo that takes T instead of
Unit, and returns T, but I need only the first variant now.
pull/638/head
Vladimír Vondruš 2 years ago
parent
commit
6978cb571f
  1. 23
      src/Magnum/Math/Test/UnitTest.cpp
  2. 31
      src/Magnum/Math/Unit.h

23
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)

31
src/Magnum/Math/Unit.h

@ -257,6 +257,37 @@ template<template<class> 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<Derived, T>&
#else
template<class Integral = T> typename std::enable_if<std::is_integral<Integral>::value, Unit<Derived, T>&>::type
#endif
operator%=(Unit<Derived, T> 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<Derived, T>
#else
template<class Integral = T> constexpr typename std::enable_if<std::is_integral<Integral>::value, Unit<Derived, T>>::type
#endif
operator%(Unit<Derived, T> other) const {
return Unit<Derived, T>{_value%other._value};
}
private:
T _value;
};

Loading…
Cancel
Save