From 7355a29a2a530f62d12a3679f8c4c7654676937c Mon Sep 17 00:00:00 2001 From: TSC21 Date: Sun, 17 Feb 2019 18:51:13 +0000 Subject: [PATCH] tests: use __FLT_EPSILON__ in comparisons --- matrix/SquareMatrix.hpp | 4 ++-- test/helper.cpp | 10 +++++----- test/inverse.cpp | 4 ++-- test/matrixAssignment.cpp | 14 +++++++------- test/setIdentity.cpp | 8 ++++---- test/squareMatrix.cpp | 4 ++-- test/test_data.py | 10 +++++----- test/test_macros.hpp | 1 + test/urt.cpp | 2 +- test/vector2.cpp | 22 +++++++++++----------- 10 files changed, 40 insertions(+), 39 deletions(-) diff --git a/matrix/SquareMatrix.hpp b/matrix/SquareMatrix.hpp index 21cf40cf96..dbcb307b16 100644 --- a/matrix/SquareMatrix.hpp +++ b/matrix/SquareMatrix.hpp @@ -149,7 +149,7 @@ bool inv(const SquareMatrix & A, SquareMatrix & inv) for (size_t n = 0; n < M; n++) { // if diagonal is zero, swap with row below - if (fabs(static_cast(U(n, n))) < 1e-8f) { + if (fabs(static_cast(U(n, n))) < __FLT_EPSILON__) { //printf("trying pivot for row %d\n",n); for (size_t i = n + 1; i < M; i++) { @@ -174,7 +174,7 @@ bool inv(const SquareMatrix & A, SquareMatrix & inv) #endif // failsafe, return zero matrix - if (fabs(static_cast(U(n, n))) < 1e-8f) { + if (fabs(static_cast(U(n, n))) < __FLT_EPSILON__) { return false; } diff --git a/test/helper.cpp b/test/helper.cpp index fb1e779a72..dda3c90df6 100644 --- a/test/helper.cpp +++ b/test/helper.cpp @@ -5,15 +5,15 @@ using namespace matrix; int main() { - TEST(fabs(wrap_pi(4.0) - (4.0 - 2*M_PI)) < 1e-5); - TEST(fabs(wrap_pi(-4.0) - (-4.0 + 2*M_PI)) < 1e-5); - TEST(fabs(wrap_pi(3.0) - (3.0)) < 1e-3); + TEST(fabs(wrap_pi(4.0) - (4.0 - 2*M_PI)) < __FLT_EPSILON__); + TEST(fabs(wrap_pi(-4.0) - (-4.0 + 2*M_PI)) < __FLT_EPSILON__); + TEST(fabs(wrap_pi(3.0) - (3.0)) < __FLT_EPSILON__); TEST(!is_finite(wrap_pi(1000.0f))); TEST(!is_finite(wrap_pi(-1000.0f))); wrap_pi(NAN); - TEST(fabs(wrap_2pi(-4.0) - (-4.0 + 2*M_PI)) < 1e-5); - TEST(fabs(wrap_2pi(3.0) - (3.0)) < 1e-3); + TEST(fabs(wrap_2pi(-4.0) - (-4.0 + 2*M_PI)) < __FLT_EPSILON__); + TEST(fabs(wrap_2pi(3.0) - (3.0)) < __FLT_EPSILON__); TEST(!is_finite(wrap_2pi(1000.0f))); TEST(!is_finite(wrap_2pi(-1000.0f))); wrap_2pi(NAN); diff --git a/test/inverse.cpp b/test/inverse.cpp index 20d69c4282..d5f75296c4 100644 --- a/test/inverse.cpp +++ b/test/inverse.cpp @@ -20,7 +20,7 @@ int main() SquareMatrix A(data); SquareMatrix A_I = inv(A); SquareMatrix A_I_check(data_check); - TEST((A_I - A_I_check).abs().max() < 1e-5); + TEST((A_I - A_I_check).abs().max() < 1e-6f); // stess test SquareMatrix A_large; @@ -63,7 +63,7 @@ int main() SquareMatrix A2(data2); SquareMatrix A2_I = inv(A2); SquareMatrix A2_I_check(data2_check); - TEST((A2_I - A2_I_check).abs().max() < 1e-3); + TEST((A2_I - A2_I_check).abs().max() < 1e-3f); float data3[9] = { 0, 1, 2, 3, 4, 5, diff --git a/test/matrixAssignment.cpp b/test/matrixAssignment.cpp index 60b7c55d4d..81e34120dc 100644 --- a/test/matrixAssignment.cpp +++ b/test/matrixAssignment.cpp @@ -22,7 +22,7 @@ int main() Matrix3f m2(data); for(int i=0; i<9; i++) { - TEST(fabs(data[i] - m2.data()[i]) < 1e-6f); + TEST(fabs(data[i] - m2.data()[i]) < __FLT_EPSILON__); } Matrix3f m_nan; @@ -38,7 +38,7 @@ int main() }; m2 = Matrix3f(data2d); for(int i=0; i<9; i++) { - TEST(fabs(data[i] - m2.data()[i]) < 1e-6f); + TEST(fabs(data[i] - m2.data()[i]) < __FLT_EPSILON__); } float data_times_2[9] = {2, 4, 6, 8, 10, 12, 14, 16, 18}; @@ -100,16 +100,16 @@ int main() m4.swapCols(2, 2); TEST(isEqual(m4, Matrix3f(data))); - TEST(fabs(m4.min() - 1) < 1e-5); - TEST(fabs((-m4).min() + 9) < 1e-5); + TEST(fabs(m4.min() - 1) < __FLT_EPSILON__); + TEST(fabs((-m4).min() + 9) < __FLT_EPSILON__); Scalar s = 1; const Vector & s_vect = s; - TEST(fabs(s - 1) < 1e-5); - TEST(fabs(s_vect(0) - 1.0f) < 1e-5); + TEST(fabs(s - 1) < __FLT_EPSILON__); + TEST(fabs(s_vect(0) - 1.0f) < __FLT_EPSILON__); Matrix m5 = s; - TEST(fabs(m5(0,0) - s) < 1e-5); + TEST(fabs(m5(0,0) - s) < __FLT_EPSILON__); Matrix m6; m6.setRow(0, Vector2f(1, 2)); diff --git a/test/setIdentity.cpp b/test/setIdentity.cpp index 9e1009548b..fd96791a39 100644 --- a/test/setIdentity.cpp +++ b/test/setIdentity.cpp @@ -11,10 +11,10 @@ int main() for (size_t i = 0; i < 3; i++) { for (size_t j = 0; j < 3; j++) { if (i == j) { - TEST(fabs(A(i, j) - 1) < 1e-7); + TEST(fabs(A(i, j) - 1) < __FLT_EPSILON__); } else { - TEST(fabs(A(i, j) - 0) < 1e-7); + TEST(fabs(A(i, j) - 0) < __FLT_EPSILON__); } } } @@ -25,10 +25,10 @@ int main() for (size_t i = 0; i < 3; i++) { for (size_t j = 0; j < 3; j++) { if (i == j) { - TEST(fabs(B(i, j) - 1) < 1e-7); + TEST(fabs(B(i, j) - 1) < __FLT_EPSILON__); } else { - TEST(fabs(B(i, j) - 0) < 1e-7); + TEST(fabs(B(i, j) - 0) < __FLT_EPSILON__); } } } diff --git a/test/squareMatrix.cpp b/test/squareMatrix.cpp index 2c668e4abf..1fc4bed1f7 100644 --- a/test/squareMatrix.cpp +++ b/test/squareMatrix.cpp @@ -14,7 +14,7 @@ int main() Vector3 diag_check(1, 5, 10); TEST(isEqual(A.diag(), diag_check)); - TEST(A.trace() - 16 < 1e-3); + TEST(A.trace() - 16 < __FLT_EPSILON__); float data_check[9] = { 1.01158503f, 0.02190432f, 0.03238144f, @@ -25,7 +25,7 @@ int main() float dt = 0.01f; SquareMatrix eA = expm(SquareMatrix(A*dt), 5); SquareMatrix eA_check(data_check); - TEST((eA - eA_check).abs().max() < 1e-3); + TEST((eA - eA_check).abs().max() < 1e-3f); return 0; } diff --git a/test/test_data.py b/test/test_data.py index 94eed5e278..85fa79c5e8 100644 --- a/test/test_data.py +++ b/test/test_data.py @@ -75,9 +75,9 @@ psi = 0.3 print('euler', phi, theta, psi) q = euler_to_quat(phi, theta, psi) -assert(abs(norm(q) - 1) < 1e-10) -assert(abs(norm(q) - 1) < 1e-10) -assert(norm(array(quat_to_euler(q)) - array([phi, theta, psi])) < 1e-10) +assert(abs(norm(q) - 1) < __FLT_EPSILON__) +assert(abs(norm(q) - 1) < __FLT_EPSILON__) +assert(norm(array(quat_to_euler(q)) - array([phi, theta, psi])) < __FLT_EPSILON__) print('\nq:') pprint(q) @@ -85,8 +85,8 @@ dcm = euler_to_dcm(phi, theta, psi) assert(norm(dcm[:,0]) == 1) assert(norm(dcm[:,1]) == 1) assert(norm(dcm[:,2]) == 1) -assert(abs(dcm[:,0].dot(dcm[:,1])) < 1e-10) -assert(abs(dcm[:,0].dot(dcm[:,2])) < 1e-10) +assert(abs(dcm[:,0].dot(dcm[:,1])) < __FLT_EPSILON__) +assert(abs(dcm[:,0].dot(dcm[:,2])) < __FLT_EPSILON__) print('\ndcm:') pprint(dcm) diff --git a/test/test_macros.hpp b/test/test_macros.hpp index d6ca2d2fb1..f21c5a66a8 100644 --- a/test/test_macros.hpp +++ b/test/test_macros.hpp @@ -10,6 +10,7 @@ #include #include // cmath has to be introduced BEFORE we poison the C library identifiers +#include "float.h" #define TEST(X) if(!(X)) { fprintf(stderr, "test failed on %s:%d\n", __FILE__, __LINE__); return -1;} diff --git a/test/urt.cpp b/test/urt.cpp index 41a772e72d..7b54d0583f 100644 --- a/test/urt.cpp +++ b/test/urt.cpp @@ -14,7 +14,7 @@ int main() SquareMatrix A(data); for(int i=0; i<6; i++) { - TEST(fabs(urt[i] - A.urt().data()[i]) < 1e-6f); + TEST(fabs(urt[i] - A.urt().data()[i]) < __FLT_EPSILON__); } return 0; diff --git a/test/vector2.cpp b/test/vector2.cpp index f46f59fe29..d5bf782241 100644 --- a/test/vector2.cpp +++ b/test/vector2.cpp @@ -10,29 +10,29 @@ int main() { Vector2f a(1, 0); Vector2f b(0, 1); - TEST(fabs(a % b - 1.0f) < 1e-5); + TEST(fabs(a % b - 1.0f) < __FLT_EPSILON__); Vector2f c; - TEST(fabs(c(0) - 0) < 1e-5); - TEST(fabs(c(1) - 0) < 1e-5); + TEST(fabs(c(0) - 0) < __FLT_EPSILON__); + TEST(fabs(c(1) - 0) < __FLT_EPSILON__); Matrix d(a); - TEST(fabs(d(0,0) - 1) < 1e-5); - TEST(fabs(d(1,0) - 0) < 1e-5); + TEST(fabs(d(0,0) - 1) < __FLT_EPSILON__); + TEST(fabs(d(1,0) - 0) < __FLT_EPSILON__); Vector2f e(d); - TEST(fabs(e(0) - 1) < 1e-5); - TEST(fabs(e(1) - 0) < 1e-5); + TEST(fabs(e(0) - 1) < __FLT_EPSILON__); + TEST(fabs(e(1) - 0) < __FLT_EPSILON__); float data[] = {4,5}; Vector2f f(data); - TEST(fabs(f(0) - 4) < 1e-5); - TEST(fabs(f(1) - 5) < 1e-5); + TEST(fabs(f(0) - 4) < __FLT_EPSILON__); + TEST(fabs(f(1) - 5) < __FLT_EPSILON__); Vector3f g(1.23f, 423.4f, 3221.f); Vector2f h(g); - TEST(fabs(h(0) - 1.23f) < 1e-5); - TEST(fabs(h(1) - 423.4f) < 1e-5); + TEST(fabs(h(0) - 1.23f) < __FLT_EPSILON__); + TEST(fabs(h(1) - 423.4f) < __FLT_EPSILON__); return 0; }