From 976ada411bf607136196c0d3e206f19eee153b56 Mon Sep 17 00:00:00 2001 From: Matthias Grob Date: Sat, 21 Mar 2020 15:22:49 +0100 Subject: [PATCH] Matrix: min max comments and test style --- matrix/Matrix.hpp | 6 ++++-- test/matrixAssignment.cpp | 24 ++++++++++++------------ 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/matrix/Matrix.hpp b/matrix/Matrix.hpp index 4229bbb7eb..524ecee33a 100644 --- a/matrix/Matrix.hpp +++ b/matrix/Matrix.hpp @@ -615,7 +615,7 @@ template Type min(const Type x, const Type y) { bool x_is_nan = isnan(x); bool y_is_nan = isnan(y); - // z > nan for z != nan is required by C the standard + // take the non-nan value if there is one if (x_is_nan || y_is_nan) { if (x_is_nan && !y_is_nan) { return y; @@ -625,11 +625,12 @@ Type min(const Type x, const Type y) { } return (x < y) ? x : y; } + template Type max(const Type x, const Type y) { bool x_is_nan = isnan(x); bool y_is_nan = isnan(y); - // z > nan for z != nan is required by C the standard + // take the non-nan value if there is one if (x_is_nan || y_is_nan) { if (x_is_nan && !y_is_nan) { return y; @@ -639,6 +640,7 @@ Type max(const Type x, const Type y) { } return (x > y) ? x : y; } + template Type constrain(const Type x, const Type lower_bound, const Type upper_bound) { if (lower_bound > upper_bound) { diff --git a/test/matrixAssignment.cpp b/test/matrixAssignment.cpp index 052b8d6cc8..c3844c769e 100644 --- a/test/matrixAssignment.cpp +++ b/test/matrixAssignment.cpp @@ -194,20 +194,20 @@ int main() TEST(isEqual(constrain(m10, m10_lower_bound, m10_upper_bound), m10_constrained_ref)); // min, max, constrain with NAN - TEST(isEqualF(matrix::typeFunction::min(5.0f,NAN), 5.0f)); - TEST(isEqualF(matrix::typeFunction::min(NAN,5.0f), 5.0f)); - TEST(isEqualF(matrix::typeFunction::min(NAN,NAN), NAN)); - TEST(isEqualF(matrix::typeFunction::max(5.0f,NAN), 5.0f)); - TEST(isEqualF(matrix::typeFunction::max(NAN,5.0f), 5.0f)); - TEST(isEqualF(matrix::typeFunction::max(NAN,NAN), NAN)); - TEST(isEqualF(matrix::typeFunction::constrain(NAN,5.0f,6.0f), NAN)); - TEST(isEqualF(matrix::typeFunction::constrain(1.0f,5.0f,4.0f), NAN)); - TEST(isEqualF(matrix::typeFunction::constrain(6.0f,NAN,5.0f), 5.0f)); - TEST(isEqualF(matrix::typeFunction::constrain(1.0f,5.0f,NAN), 5.0f)); + TEST(isEqualF(matrix::typeFunction::min(5.f, NAN), 5.f)); + TEST(isEqualF(matrix::typeFunction::min(NAN, 5.f), 5.f)); + TEST(isEqualF(matrix::typeFunction::min(NAN, NAN), NAN)); + TEST(isEqualF(matrix::typeFunction::max(5.f, NAN), 5.f)); + TEST(isEqualF(matrix::typeFunction::max(NAN, 5.f), 5.f)); + TEST(isEqualF(matrix::typeFunction::max(NAN, NAN), NAN)); + TEST(isEqualF(matrix::typeFunction::constrain(NAN, 5.f, 6.f), NAN)); + TEST(isEqualF(matrix::typeFunction::constrain(1.f, 5.f, 4.f), NAN)); + TEST(isEqualF(matrix::typeFunction::constrain(6.f, NAN, 5.f), 5.f)); + TEST(isEqualF(matrix::typeFunction::constrain(1.f, 5.f, NAN), 5.f)); Vector2f v1{NAN, 5.0f}; - Vector2f v1_min = min(v1,1.0f); + Vector2f v1_min = min(v1, 1.f); Matrix3f m11 = min(m10_constrained_ref,NAN); - TEST(isEqualF(fmin(NAN,1.0f), float(v1_min(0)))); + TEST(isEqualF(fmin(NAN, 1.f), float(v1_min(0)))); TEST(isEqual(m11, m10_constrained_ref)); // check write_string()