diff --git a/doc/changelog.dox b/doc/changelog.dox index 1722c386d..86c83df4d 100644 --- a/doc/changelog.dox +++ b/doc/changelog.dox @@ -263,6 +263,7 @@ See also: @subsubsection changelog-latest-new-math Math library - Added @ref Math::fmod() (see [mosra/magnum#454](https://github.com/mosra/magnum/pull/454)) +- Added @ref Math::fract() - Added @ref Math::binomialCoefficient() (see [mosra/magnum#461](https://github.com/mosra/magnum/pull/461)) - Added @ref Math::popcount() - Added @ref Math::Color3::fromLinearRgbInt(), diff --git a/src/Magnum/Math/Functions.h b/src/Magnum/Math/Functions.h index 99a0618d7..5c35f3c6f 100644 --- a/src/Magnum/Math/Functions.h +++ b/src/Magnum/Math/Functions.h @@ -524,6 +524,7 @@ Calculates the remainder @f$ r @f$ of a floating point division: @f[ numerator, whereas @glsl mod() @ce keeps the denumerator's sign. @m_keyword{mod(),GLSL mod(),} +@see @ref fract() */ template inline Vector fmod(const Vector::value, int>::type = 0 + #endif +> inline T fract(T a) { + return T(UnderlyingTypeOf(a) - std::floor(UnderlyingTypeOf(a))); +} + +/** +@overload +@m_since_latest +*/ +template inline Vector fract(const Vector& a) { + Vector out{Magnum::NoInit}; + for(std::size_t i = 0; i != size; ++i) + out[i] = Math::fract(a[i]); + return out; +} + /** @brief Linear interpolation of two values @param a First value diff --git a/src/Magnum/Math/Test/FunctionsTest.cpp b/src/Magnum/Math/Test/FunctionsTest.cpp index c15ffdc12..9d5474072 100644 --- a/src/Magnum/Math/Test/FunctionsTest.cpp +++ b/src/Magnum/Math/Test/FunctionsTest.cpp @@ -55,6 +55,7 @@ struct FunctionsTest: TestSuite::Tester { void round(); void ceil(); void fmod(); + void fract(); void binomialCoefficient(); void binomialCoefficientInvalidInput(); @@ -122,6 +123,7 @@ FunctionsTest::FunctionsTest() { &FunctionsTest::round, &FunctionsTest::ceil, &FunctionsTest::fmod, + &FunctionsTest::fract, &FunctionsTest::binomialCoefficient, &FunctionsTest::binomialCoefficientInvalidInput, @@ -369,6 +371,18 @@ void FunctionsTest::fmod() { CORRADE_COMPARE(Math::fmod(Seconds{2.7_sec}, Seconds{1.3_sec}), Seconds{0.1_sec}); } +void FunctionsTest::fract() { + CORRADE_COMPARE(Math::fract(5.132f), 0.132f); + /* It works the other way for negative numbers */ + CORRADE_COMPARE(Math::fract(-5.132f), 0.868f); + CORRADE_COMPARE(Math::fract(Vector3{5.132f, 4.0f, -2.474f}), (Vector3{0.132f, 0.0f, 0.526f})); + + /* Wrapped types */ + CORRADE_COMPARE(Math::fract(2.7_degf), 0.7_degf); + /* Nanoseconds are an integer type */ + CORRADE_COMPARE(Math::fract(Seconds{2.7_sec}), Seconds{0.7_sec}); +} + void FunctionsTest::sqrt() { CORRADE_COMPARE(Math::sqrt(16), 4); CORRADE_COMPARE(Math::sqrt(Vector3i(256, 1, 0)), Vector3i(16, 1, 0)); diff --git a/src/Magnum/TextureTools/Sample.h b/src/Magnum/TextureTools/Sample.h index a8c786eb6..67562d522 100644 --- a/src/Magnum/TextureTools/Sample.h +++ b/src/Magnum/TextureTools/Sample.h @@ -48,7 +48,8 @@ floating-point type. Note that the @p texture is interpreted as having linear colors. Use @ref sampleSrgb() if you want to perform conversion from sRGB values instead. -@see @ref Math::lerp(const T&, const T&, U), @ref Math::unpack(const Integral&) +@see @ref Math::lerp(const T&, const T&, U), + @ref Math::unpack(const Integral&), @ref Math::fract() */ MAGNUM_TEXTURETOOLS_EXPORT Color3 sampleLinear(const Containers::StridedArrayView1D& texture, Float factor);