Fixed error: comparing floating point with == or != is unsafe

This commit is contained in:
Groove 2018-07-28 21:06:00 +02:00
parent d6b0b9b1ef
commit 21d030ad3d

View File

@ -1,3 +1,4 @@
#include <glm/gtc/constants.hpp>
#include <glm/gtc/quaternion.hpp> #include <glm/gtc/quaternion.hpp>
#include <glm/gtc/matrix_transform.hpp> #include <glm/gtc/matrix_transform.hpp>
#include <glm/ext/vector_relational.hpp> #include <glm/ext/vector_relational.hpp>
@ -12,25 +13,25 @@ int test_quat_angle()
glm::quat Q = glm::angleAxis(glm::pi<float>() * 0.25f, glm::vec3(0, 0, 1)); glm::quat Q = glm::angleAxis(glm::pi<float>() * 0.25f, glm::vec3(0, 0, 1));
glm::quat N = glm::normalize(Q); glm::quat N = glm::normalize(Q);
float L = glm::length(N); float L = glm::length(N);
Error += glm::epsilonEqual(L, 1.0f, 0.01f) ? 0 : 1; Error += glm::equal(L, 1.0f, 0.01f) ? 0 : 1;
float A = glm::angle(N); float A = glm::angle(N);
Error += glm::epsilonEqual(A, glm::pi<float>() * 0.25f, 0.01f) ? 0 : 1; Error += glm::equal(A, glm::pi<float>() * 0.25f, 0.01f) ? 0 : 1;
} }
{ {
glm::quat Q = glm::angleAxis(glm::pi<float>() * 0.25f, glm::normalize(glm::vec3(0, 1, 1))); glm::quat Q = glm::angleAxis(glm::pi<float>() * 0.25f, glm::normalize(glm::vec3(0, 1, 1)));
glm::quat N = glm::normalize(Q); glm::quat N = glm::normalize(Q);
float L = glm::length(N); float L = glm::length(N);
Error += glm::epsilonEqual(L, 1.0f, 0.01f) ? 0 : 1; Error += glm::equal(L, 1.0f, 0.01f) ? 0 : 1;
float A = glm::angle(N); float A = glm::angle(N);
Error += glm::epsilonEqual(A, glm::pi<float>() * 0.25f, 0.01f) ? 0 : 1; Error += glm::equal(A, glm::pi<float>() * 0.25f, 0.01f) ? 0 : 1;
} }
{ {
glm::quat Q = glm::angleAxis(glm::pi<float>() * 0.25f, glm::normalize(glm::vec3(1, 2, 3))); glm::quat Q = glm::angleAxis(glm::pi<float>() * 0.25f, glm::normalize(glm::vec3(1, 2, 3)));
glm::quat N = glm::normalize(Q); glm::quat N = glm::normalize(Q);
float L = glm::length(N); float L = glm::length(N);
Error += glm::epsilonEqual(L, 1.0f, 0.01f) ? 0 : 1; Error += glm::equal(L, 1.0f, 0.01f) ? 0 : 1;
float A = glm::angle(N); float A = glm::angle(N);
Error += glm::epsilonEqual(A, glm::pi<float>() * 0.25f, 0.01f) ? 0 : 1; Error += glm::equal(A, glm::pi<float>() * 0.25f, 0.01f) ? 0 : 1;
} }
return Error; return Error;
@ -45,10 +46,10 @@ int test_quat_angleAxis()
glm::quat C = glm::mix(A, B, 0.5f); glm::quat C = glm::mix(A, B, 0.5f);
glm::quat D = glm::angleAxis(glm::pi<float>() * 0.25f, glm::vec3(0, 0, 1)); glm::quat D = glm::angleAxis(glm::pi<float>() * 0.25f, glm::vec3(0, 0, 1));
Error += glm::epsilonEqual(C.x, D.x, 0.01f) ? 0 : 1; Error += glm::equal(C.x, D.x, 0.01f) ? 0 : 1;
Error += glm::epsilonEqual(C.y, D.y, 0.01f) ? 0 : 1; Error += glm::equal(C.y, D.y, 0.01f) ? 0 : 1;
Error += glm::epsilonEqual(C.z, D.z, 0.01f) ? 0 : 1; Error += glm::equal(C.z, D.z, 0.01f) ? 0 : 1;
Error += glm::epsilonEqual(C.w, D.w, 0.01f) ? 0 : 1; Error += glm::equal(C.w, D.w, 0.01f) ? 0 : 1;
return Error; return Error;
} }
@ -62,10 +63,10 @@ int test_quat_mix()
glm::quat C = glm::mix(A, B, 0.5f); glm::quat C = glm::mix(A, B, 0.5f);
glm::quat D = glm::angleAxis(glm::pi<float>() * 0.25f, glm::vec3(0, 0, 1)); glm::quat D = glm::angleAxis(glm::pi<float>() * 0.25f, glm::vec3(0, 0, 1));
Error += glm::epsilonEqual(C.x, D.x, 0.01f) ? 0 : 1; Error += glm::equal(C.x, D.x, 0.01f) ? 0 : 1;
Error += glm::epsilonEqual(C.y, D.y, 0.01f) ? 0 : 1; Error += glm::equal(C.y, D.y, 0.01f) ? 0 : 1;
Error += glm::epsilonEqual(C.z, D.z, 0.01f) ? 0 : 1; Error += glm::equal(C.z, D.z, 0.01f) ? 0 : 1;
Error += glm::epsilonEqual(C.w, D.w, 0.01f) ? 0 : 1; Error += glm::equal(C.w, D.w, 0.01f) ? 0 : 1;
return Error; return Error;
} }
@ -88,19 +89,19 @@ int test_quat_normalize()
glm::quat Q = glm::angleAxis(glm::pi<float>() * 0.25f, glm::vec3(0, 0, 1)); glm::quat Q = glm::angleAxis(glm::pi<float>() * 0.25f, glm::vec3(0, 0, 1));
glm::quat N = glm::normalize(Q); glm::quat N = glm::normalize(Q);
float L = glm::length(N); float L = glm::length(N);
Error += glm::epsilonEqual(L, 1.0f, 0.000001f) ? 0 : 1; Error += glm::equal(L, 1.0f, 0.000001f) ? 0 : 1;
} }
{ {
glm::quat Q = glm::angleAxis(glm::pi<float>() * 0.25f, glm::vec3(0, 0, 2)); glm::quat Q = glm::angleAxis(glm::pi<float>() * 0.25f, glm::vec3(0, 0, 2));
glm::quat N = glm::normalize(Q); glm::quat N = glm::normalize(Q);
float L = glm::length(N); float L = glm::length(N);
Error += glm::epsilonEqual(L, 1.0f, 0.000001f) ? 0 : 1; Error += glm::equal(L, 1.0f, 0.000001f) ? 0 : 1;
} }
{ {
glm::quat Q = glm::angleAxis(glm::pi<float>() * 0.25f, glm::vec3(1, 2, 3)); glm::quat Q = glm::angleAxis(glm::pi<float>() * 0.25f, glm::vec3(1, 2, 3));
glm::quat N = glm::normalize(Q); glm::quat N = glm::normalize(Q);
float L = glm::length(N); float L = glm::length(N);
Error += glm::epsilonEqual(L, 1.0f, 0.000001f) ? 0 : 1; Error += glm::equal(L, 1.0f, 0.000001f) ? 0 : 1;
} }
return Error; return Error;
@ -116,7 +117,7 @@ int test_quat_euler()
float Pitch = glm::pitch(q); float Pitch = glm::pitch(q);
float Yaw = glm::yaw(q); float Yaw = glm::yaw(q);
glm::vec3 Angles = glm::eulerAngles(q); glm::vec3 Angles = glm::eulerAngles(q);
Error += glm::all(glm::epsilonEqual(Angles, glm::vec3(Pitch, Yaw, Roll), 0.000001f)) ? 0 : 1; Error += glm::all(glm::equal(Angles, glm::vec3(Pitch, Yaw, Roll), 0.000001f)) ? 0 : 1;
} }
{ {
@ -125,7 +126,7 @@ int test_quat_euler()
double Pitch = glm::pitch(q); double Pitch = glm::pitch(q);
double Yaw = glm::yaw(q); double Yaw = glm::yaw(q);
glm::dvec3 Angles = glm::eulerAngles(q); glm::dvec3 Angles = glm::eulerAngles(q);
Error += glm::all(glm::epsilonEqual(Angles, glm::dvec3(Pitch, Yaw, Roll), 0.000001)) ? 0 : 1; Error += glm::all(glm::equal(Angles, glm::dvec3(Pitch, Yaw, Roll), 0.000001)) ? 0 : 1;
} }
return Error; return Error;
@ -145,12 +146,12 @@ int test_quat_slerp()
// Testing a == 0 // Testing a == 0
// Must be id // Must be id
glm::quat id2 = glm::slerp(id, Y90rot, 0.0f); glm::quat id2 = glm::slerp(id, Y90rot, 0.0f);
Error += glm::all(glm::epsilonEqual(id, id2, Epsilon)) ? 0 : 1; Error += glm::all(glm::equal(id, id2, Epsilon)) ? 0 : 1;
// Testing a == 1 // Testing a == 1
// Must be 90° rotation on Y : 0 0.7 0 0.7 // Must be 90° rotation on Y : 0 0.7 0 0.7
glm::quat Y90rot2 = glm::slerp(id, Y90rot, 1.0f); glm::quat Y90rot2 = glm::slerp(id, Y90rot, 1.0f);
Error += glm::all(glm::epsilonEqual(Y90rot, Y90rot2, Epsilon)) ? 0 : 1; Error += glm::all(glm::equal(Y90rot, Y90rot2, Epsilon)) ? 0 : 1;
// Testing standard, easy case // Testing standard, easy case
// Must be 45° rotation on Y : 0 0.38 0 0.92 // Must be 45° rotation on Y : 0 0.38 0 0.92
@ -165,25 +166,25 @@ int test_quat_slerp()
// certainly not a 135° rotation // certainly not a 135° rotation
glm::quat Y45rot3 = glm::slerp(id , -Y90rot, 0.5f); glm::quat Y45rot3 = glm::slerp(id , -Y90rot, 0.5f);
float Y45angle3 = glm::angle(Y45rot3); float Y45angle3 = glm::angle(Y45rot3);
Error += glm::epsilonEqual(Y45angle3, glm::pi<float>() * 0.25f, Epsilon) ? 0 : 1; Error += glm::equal(Y45angle3, glm::pi<float>() * 0.25f, Epsilon) ? 0 : 1;
Error += glm::all(glm::epsilonEqual(Ym45rot2, Y45rot3, Epsilon)) ? 0 : 1; Error += glm::all(glm::equal(Ym45rot2, Y45rot3, Epsilon)) ? 0 : 1;
// Same, but inverted // Same, but inverted
// Must also be 45° rotation on Y : 0 0.38 0 0.92 // Must also be 45° rotation on Y : 0 0.38 0 0.92
// -0 -0.38 -0 -0.92 is ok too // -0 -0.38 -0 -0.92 is ok too
glm::quat Y45rot4 = glm::slerp(-Y90rot, id, 0.5f); glm::quat Y45rot4 = glm::slerp(-Y90rot, id, 0.5f);
Error += glm::all(glm::epsilonEqual(Ym45rot2, -Y45rot4, Epsilon)) ? 0 : 1; Error += glm::all(glm::equal(Ym45rot2, -Y45rot4, Epsilon)) ? 0 : 1;
// Testing q1 = q2 // Testing q1 = q2
// Must be 90° rotation on Y : 0 0.7 0 0.7 // Must be 90° rotation on Y : 0 0.7 0 0.7
glm::quat Y90rot3 = glm::slerp(Y90rot, Y90rot, 0.5f); glm::quat Y90rot3 = glm::slerp(Y90rot, Y90rot, 0.5f);
Error += glm::all(glm::epsilonEqual(Y90rot, Y90rot3, Epsilon)) ? 0 : 1; Error += glm::all(glm::equal(Y90rot, Y90rot3, Epsilon)) ? 0 : 1;
// Testing 180° rotation // Testing 180° rotation
// Must be 90° rotation on almost any axis that is on the XZ plane // Must be 90° rotation on almost any axis that is on the XZ plane
glm::quat XZ90rot = glm::slerp(id, -Y90rot, 0.5f); glm::quat XZ90rot = glm::slerp(id, -Y90rot, 0.5f);
float XZ90angle = glm::angle(XZ90rot); // Must be PI/4 = 0.78; float XZ90angle = glm::angle(XZ90rot); // Must be PI/4 = 0.78;
Error += glm::epsilonEqual(XZ90angle, glm::pi<float>() * 0.25f, Epsilon) ? 0 : 1; Error += glm::equal(XZ90angle, glm::pi<float>() * 0.25f, Epsilon) ? 0 : 1;
// Testing almost equal quaternions (this test should pass through the linear interpolation) // Testing almost equal quaternions (this test should pass through the linear interpolation)
// Must be 0 0.00X 0 0.99999 // Must be 0 0.00X 0 0.99999
@ -195,7 +196,7 @@ int test_quat_slerp()
glm::quat result = glm::slerp(a, id, 0.5f); glm::quat result = glm::slerp(a, id, 0.5f);
Error += glm::epsilonEqual(glm::pow(glm::dot(id, result), 2.f), 1.f, 0.01f) ? 0 : 1; Error += glm::equal(glm::pow(glm::dot(id, result), 2.f), 1.f, 0.01f) ? 0 : 1;
} }
return Error; return Error;
@ -230,23 +231,23 @@ int test_quat_two_axis_ctr()
glm::quat const q1(glm::vec3(1, 0, 0), glm::vec3(0, 1, 0)); glm::quat const q1(glm::vec3(1, 0, 0), glm::vec3(0, 1, 0));
glm::vec3 const v1 = q1 * glm::vec3(1, 0, 0); glm::vec3 const v1 = q1 * glm::vec3(1, 0, 0);
Error += glm::all(glm::epsilonEqual(v1, glm::vec3(0, 1, 0), 0.0001f)) ? 0 : 1; Error += glm::all(glm::equal(v1, glm::vec3(0, 1, 0), 0.0001f)) ? 0 : 1;
glm::quat const q2 = q1 * q1; glm::quat const q2 = q1 * q1;
glm::vec3 const v2 = q2 * glm::vec3(1, 0, 0); glm::vec3 const v2 = q2 * glm::vec3(1, 0, 0);
Error += glm::all(glm::epsilonEqual(v2, glm::vec3(-1, 0, 0), 0.0001f)) ? 0 : 1; Error += glm::all(glm::equal(v2, glm::vec3(-1, 0, 0), 0.0001f)) ? 0 : 1;
glm::quat const q3(glm::vec3(1, 0, 0), glm::vec3(-1, 0, 0)); glm::quat const q3(glm::vec3(1, 0, 0), glm::vec3(-1, 0, 0));
glm::vec3 const v3 = q3 * glm::vec3(1, 0, 0); glm::vec3 const v3 = q3 * glm::vec3(1, 0, 0);
Error += glm::all(glm::epsilonEqual(v3, glm::vec3(-1, 0, 0), 0.0001f)) ? 0 : 1; Error += glm::all(glm::equal(v3, glm::vec3(-1, 0, 0), 0.0001f)) ? 0 : 1;
glm::quat const q4(glm::vec3(0, 1, 0), glm::vec3(0, -1, 0)); glm::quat const q4(glm::vec3(0, 1, 0), glm::vec3(0, -1, 0));
glm::vec3 const v4 = q4 * glm::vec3(0, 1, 0); glm::vec3 const v4 = q4 * glm::vec3(0, 1, 0);
Error += glm::all(glm::epsilonEqual(v4, glm::vec3(0, -1, 0), 0.0001f)) ? 0 : 1; Error += glm::all(glm::equal(v4, glm::vec3(0, -1, 0), 0.0001f)) ? 0 : 1;
glm::quat const q5(glm::vec3(0, 0, 1), glm::vec3(0, 0, -1)); glm::quat const q5(glm::vec3(0, 0, 1), glm::vec3(0, 0, -1));
glm::vec3 const v5 = q5 * glm::vec3(0, 0, 1); glm::vec3 const v5 = q5 * glm::vec3(0, 0, 1);
Error += glm::all(glm::epsilonEqual(v5, glm::vec3(0, 0, -1), 0.0001f)) ? 0 : 1; Error += glm::all(glm::equal(v5, glm::vec3(0, 0, -1), 0.0001f)) ? 0 : 1;
return Error; return Error;
} }
@ -260,7 +261,7 @@ int test_quat_mul_vec()
glm::vec3 u(q * v); glm::vec3 u(q * v);
glm::vec3 w(u * q); glm::vec3 w(u * q);
Error += glm::all(glm::epsilonEqual(v, w, 0.01f)) ? 0 : 1; Error += glm::all(glm::equal(v, w, 0.01f)) ? 0 : 1;
return Error; return Error;
} }
@ -330,10 +331,7 @@ int test_identity()
glm::mat4 const M = glm::identity<glm::mat4x4>(); glm::mat4 const M = glm::identity<glm::mat4x4>();
glm::mat4 const N(1.0f); glm::mat4 const N(1.0f);
Error += glm::all(glm::equal(M[0], N[0], 0.0001f)) ? 0 : 1; Error += glm::all(glm::equal(M, N, 0.0001f)) ? 0 : 1;
Error += glm::all(glm::equal(M[1], N[1], 0.0001f)) ? 0 : 1;
Error += glm::all(glm::equal(M[2], N[2], 0.0001f)) ? 0 : 1;
Error += glm::all(glm::equal(M[3], N[3], 0.0001f)) ? 0 : 1;
return Error; return Error;
} }