Browse Source

Math: add fmod function

pull/454/head
Pablo Escobar 6 years ago
parent
commit
daa6d9125e
  1. 26
      src/Magnum/Math/Functions.h
  2. 10
      src/Magnum/Math/Test/FunctionsTest.cpp

26
src/Magnum/Math/Functions.h

@ -408,6 +408,32 @@ template<std::size_t size, class T> inline Vector<size, T> ceil(const Vector<siz
return out; return out;
} }
/**
@brief Floating point division remainder
@param a Numerator
@param b Denumerator
Calculates the remainder @f$ r @f$ of a floating point division: @f[
r = a - b * \operatorname{trunc}(a/b)
@f]
@attention This function differs from the GLSL `mod` function when @f$ a/b @f$ is negative.
The return value has the same sign as the numerator, whereas `mod` keeps the denumerator's sign.
@m_keyword{mod(),GLSL mod(),}
*/
template<class T> inline typename std::enable_if<IsScalar<T>::value, T>::type fmod(T a, T b) {
return T(std::fmod(UnderlyingTypeOf<T>(a), UnderlyingTypeOf<T>(b)));
}
/** @overload */
template<std::size_t size, class T> inline Vector<size, T> fmod(const Vector<size, T>& a, const Vector<size, T>& b) {
Vector<size, T> out{Magnum::NoInit};
for(std::size_t i = 0; i != size; ++i)
out[i] = Math::fmod(a[i], b[i]);
return out;
}
/** /**
@brief Linear interpolation of two values @brief Linear interpolation of two values
@param a First value @param a First value

10
src/Magnum/Math/Test/FunctionsTest.cpp

@ -51,6 +51,7 @@ struct FunctionsTest: Corrade::TestSuite::Tester {
void floor(); void floor();
void round(); void round();
void ceil(); void ceil();
void fmod();
void sqrt(); void sqrt();
void sqrtInverted(); void sqrtInverted();
@ -112,6 +113,7 @@ FunctionsTest::FunctionsTest() {
&FunctionsTest::floor, &FunctionsTest::floor,
&FunctionsTest::round, &FunctionsTest::round,
&FunctionsTest::ceil, &FunctionsTest::ceil,
&FunctionsTest::fmod,
&FunctionsTest::sqrt, &FunctionsTest::sqrt,
&FunctionsTest::sqrtInverted, &FunctionsTest::sqrtInverted,
@ -291,6 +293,14 @@ void FunctionsTest::ceil() {
CORRADE_COMPARE(Math::ceil(2.7_degf), 3.0_degf); CORRADE_COMPARE(Math::ceil(2.7_degf), 3.0_degf);
} }
void FunctionsTest::fmod() {
CORRADE_COMPARE(Math::fmod(5.1f, 3.0f), 2.1f);
CORRADE_COMPARE(Math::fmod(Vector3(5.1f, -5.1f, 6.8f), Vector3(3.0f, 3.0f, 1.1f)), Vector3(2.1f, -2.1f, 0.2f));
/* Wrapped types */
CORRADE_COMPARE(Math::fmod(2.7_degf, 1.3_degf), 0.1_degf);
}
void FunctionsTest::sqrt() { void FunctionsTest::sqrt() {
CORRADE_COMPARE(Math::sqrt(16), 4); CORRADE_COMPARE(Math::sqrt(16), 4);
CORRADE_COMPARE(Math::sqrt(Vector3i(256, 1, 0)), Vector3i(16, 1, 0)); CORRADE_COMPARE(Math::sqrt(Vector3i(256, 1, 0)), Vector3i(16, 1, 0));

Loading…
Cancel
Save