diff --git a/doc/changelog.dox b/doc/changelog.dox index 5654acdf3..e809cbfa8 100644 --- a/doc/changelog.dox +++ b/doc/changelog.dox @@ -1642,6 +1642,9 @@ See also: @ref Math::RectangularMatrix::data() are no longer @cpp constexpr @ce in order to make them return a reference to a fixed-size array instead of a pointer, which was deemed a more useful property. +- @ref Math::sign() now always returns a unitless type instead of the + input type itself, so it's possible to e.g. extract a sign of an angle + value and multiply other angles with it. - @ref MeshTools::primitiveCount() now requires the element count to follow rules defined by a particular primitive to be consistent with requirements of @ref MeshTools::generateIndices() and related APIs. Before it was just diff --git a/src/Magnum/Math/Functions.h b/src/Magnum/Math/Functions.h index 0f52875bd..ce1dbf13c 100644 --- a/src/Magnum/Math/Functions.h +++ b/src/Magnum/Math/Functions.h @@ -397,15 +397,15 @@ template inline Vector clamp(const Vector 0, `0` if @p x = 0 and `-1` if @p x < 0. */ -template inline typename std::enable_if::value, T>::type sign(T scalar) { - if(scalar > T(0)) return T(1); - if(scalar < T(0)) return T(-1); - return T(0); +template inline typename std::enable_if::value, UnderlyingTypeOf>::type sign(T scalar) { + if(scalar > T(0)) return UnderlyingTypeOf(1); + if(scalar < T(0)) return UnderlyingTypeOf(-1); + return UnderlyingTypeOf(0); } /** @overload */ -template inline Vector sign(const Vector& a) { - Vector out{Magnum::NoInit}; +template inline Vector> sign(const Vector& a) { + Vector> out{Magnum::NoInit}; for(std::size_t i = 0; i != size; ++i) out[i] = Math::sign(a[i]); return out; diff --git a/src/Magnum/Math/Test/FunctionsTest.cpp b/src/Magnum/Math/Test/FunctionsTest.cpp index 62d536f17..7a1108240 100644 --- a/src/Magnum/Math/Test/FunctionsTest.cpp +++ b/src/Magnum/Math/Test/FunctionsTest.cpp @@ -263,8 +263,12 @@ void FunctionsTest::sign() { CORRADE_COMPARE(Math::sign(-3.7), -1.0); CORRADE_COMPARE(Math::sign(Vector3i(0, -3, 2)), Vector3i(0, -1, 1)); - /* Wrapped types */ - CORRADE_COMPARE(Math::sign(-3.7_degf), -1.0_degf); + /* Wrapped types. Returns the underlying type instead of the type itself as + it should be possible to do e.g. angle*Math::sign(angle) and get an + angle value back. With returning the type itself such operation wouldn't + have any definable unit. */ + CORRADE_COMPARE(Math::sign(-3.7_degf), -1.0f); + CORRADE_COMPARE(Math::sign(Math::Vector2{3.5_degf, -1.9_degf}), (Vector2{1.0f, -1.0f})); } void FunctionsTest::abs() {