Browse Source

Math: sign() should return a unitless type.

Breaking change, but the new behavior makes a lot more sense. Hopefully
not that significant breakage -- I don't assume people regularly worked
with angles this way.
pull/638/head
Vladimír Vondruš 2 years ago
parent
commit
181a3a3511
  1. 3
      doc/changelog.dox
  2. 12
      src/Magnum/Math/Functions.h
  3. 8
      src/Magnum/Math/Test/FunctionsTest.cpp

3
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

12
src/Magnum/Math/Functions.h

@ -397,15 +397,15 @@ template<std::size_t size, class T> inline Vector<size, T> clamp(const Vector<si
Returns `1` if @p x > 0, `0` if @p x = 0 and `-1` if @p x < 0.
*/
template<class T> inline typename std::enable_if<IsScalar<T>::value, T>::type sign(T scalar) {
if(scalar > T(0)) return T(1);
if(scalar < T(0)) return T(-1);
return T(0);
template<class T> inline typename std::enable_if<IsScalar<T>::value, UnderlyingTypeOf<T>>::type sign(T scalar) {
if(scalar > T(0)) return UnderlyingTypeOf<T>(1);
if(scalar < T(0)) return UnderlyingTypeOf<T>(-1);
return UnderlyingTypeOf<T>(0);
}
/** @overload */
template<std::size_t size, class T> inline Vector<size, T> sign(const Vector<size, T>& a) {
Vector<size, T> out{Magnum::NoInit};
template<std::size_t size, class T> inline Vector<size, UnderlyingTypeOf<T>> sign(const Vector<size, T>& a) {
Vector<size, UnderlyingTypeOf<T>> out{Magnum::NoInit};
for(std::size_t i = 0; i != size; ++i)
out[i] = Math::sign(a[i]);
return out;

8
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<Deg>{3.5_degf, -1.9_degf}), (Vector2{1.0f, -1.0f}));
}
void FunctionsTest::abs() {

Loading…
Cancel
Save