From 21d030ad3dfad6a6b866b5fdd218d2953d4b8dcc Mon Sep 17 00:00:00 2001 From: Groove Date: Sat, 28 Jul 2018 21:06:00 +0200 Subject: [PATCH] Fixed error: comparing floating point with == or != is unsafe --- test/gtc/gtc_quaternion.cpp | 72 ++++++++++++++++++------------------- 1 file changed, 35 insertions(+), 37 deletions(-) diff --git a/test/gtc/gtc_quaternion.cpp b/test/gtc/gtc_quaternion.cpp index 80653b88..1086c50d 100644 --- a/test/gtc/gtc_quaternion.cpp +++ b/test/gtc/gtc_quaternion.cpp @@ -1,3 +1,4 @@ +#include #include #include #include @@ -12,25 +13,25 @@ int test_quat_angle() glm::quat Q = glm::angleAxis(glm::pi() * 0.25f, glm::vec3(0, 0, 1)); glm::quat N = glm::normalize(Q); 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); - Error += glm::epsilonEqual(A, glm::pi() * 0.25f, 0.01f) ? 0 : 1; + Error += glm::equal(A, glm::pi() * 0.25f, 0.01f) ? 0 : 1; } { glm::quat Q = glm::angleAxis(glm::pi() * 0.25f, glm::normalize(glm::vec3(0, 1, 1))); glm::quat N = glm::normalize(Q); 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); - Error += glm::epsilonEqual(A, glm::pi() * 0.25f, 0.01f) ? 0 : 1; + Error += glm::equal(A, glm::pi() * 0.25f, 0.01f) ? 0 : 1; } { glm::quat Q = glm::angleAxis(glm::pi() * 0.25f, glm::normalize(glm::vec3(1, 2, 3))); glm::quat N = glm::normalize(Q); 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); - Error += glm::epsilonEqual(A, glm::pi() * 0.25f, 0.01f) ? 0 : 1; + Error += glm::equal(A, glm::pi() * 0.25f, 0.01f) ? 0 : 1; } return Error; @@ -45,10 +46,10 @@ int test_quat_angleAxis() glm::quat C = glm::mix(A, B, 0.5f); glm::quat D = glm::angleAxis(glm::pi() * 0.25f, glm::vec3(0, 0, 1)); - Error += glm::epsilonEqual(C.x, D.x, 0.01f) ? 0 : 1; - Error += glm::epsilonEqual(C.y, D.y, 0.01f) ? 0 : 1; - Error += glm::epsilonEqual(C.z, D.z, 0.01f) ? 0 : 1; - Error += glm::epsilonEqual(C.w, D.w, 0.01f) ? 0 : 1; + Error += glm::equal(C.x, D.x, 0.01f) ? 0 : 1; + Error += glm::equal(C.y, D.y, 0.01f) ? 0 : 1; + Error += glm::equal(C.z, D.z, 0.01f) ? 0 : 1; + Error += glm::equal(C.w, D.w, 0.01f) ? 0 : 1; return Error; } @@ -62,10 +63,10 @@ int test_quat_mix() glm::quat C = glm::mix(A, B, 0.5f); glm::quat D = glm::angleAxis(glm::pi() * 0.25f, glm::vec3(0, 0, 1)); - Error += glm::epsilonEqual(C.x, D.x, 0.01f) ? 0 : 1; - Error += glm::epsilonEqual(C.y, D.y, 0.01f) ? 0 : 1; - Error += glm::epsilonEqual(C.z, D.z, 0.01f) ? 0 : 1; - Error += glm::epsilonEqual(C.w, D.w, 0.01f) ? 0 : 1; + Error += glm::equal(C.x, D.x, 0.01f) ? 0 : 1; + Error += glm::equal(C.y, D.y, 0.01f) ? 0 : 1; + Error += glm::equal(C.z, D.z, 0.01f) ? 0 : 1; + Error += glm::equal(C.w, D.w, 0.01f) ? 0 : 1; return Error; } @@ -88,19 +89,19 @@ int test_quat_normalize() glm::quat Q = glm::angleAxis(glm::pi() * 0.25f, glm::vec3(0, 0, 1)); glm::quat N = glm::normalize(Q); 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() * 0.25f, glm::vec3(0, 0, 2)); glm::quat N = glm::normalize(Q); 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() * 0.25f, glm::vec3(1, 2, 3)); glm::quat N = glm::normalize(Q); 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; @@ -116,7 +117,7 @@ int test_quat_euler() float Pitch = glm::pitch(q); float Yaw = glm::yaw(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 Yaw = glm::yaw(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; @@ -145,12 +146,12 @@ int test_quat_slerp() // Testing a == 0 // Must be id 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 // Must be 90° rotation on Y : 0 0.7 0 0.7 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 // 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 glm::quat Y45rot3 = glm::slerp(id , -Y90rot, 0.5f); float Y45angle3 = glm::angle(Y45rot3); - Error += glm::epsilonEqual(Y45angle3, glm::pi() * 0.25f, Epsilon) ? 0 : 1; - Error += glm::all(glm::epsilonEqual(Ym45rot2, Y45rot3, Epsilon)) ? 0 : 1; + Error += glm::equal(Y45angle3, glm::pi() * 0.25f, Epsilon) ? 0 : 1; + Error += glm::all(glm::equal(Ym45rot2, Y45rot3, Epsilon)) ? 0 : 1; // Same, but inverted // Must also be 45° rotation on Y : 0 0.38 0 0.92 // -0 -0.38 -0 -0.92 is ok too 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 // Must be 90° rotation on Y : 0 0.7 0 0.7 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 // Must be 90° rotation on almost any axis that is on the XZ plane glm::quat XZ90rot = glm::slerp(id, -Y90rot, 0.5f); float XZ90angle = glm::angle(XZ90rot); // Must be PI/4 = 0.78; - Error += glm::epsilonEqual(XZ90angle, glm::pi() * 0.25f, Epsilon) ? 0 : 1; + Error += glm::equal(XZ90angle, glm::pi() * 0.25f, Epsilon) ? 0 : 1; // Testing almost equal quaternions (this test should pass through the linear interpolation) // 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); - 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; @@ -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::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::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::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::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::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; } @@ -260,7 +261,7 @@ int test_quat_mul_vec() glm::vec3 u(q * v); 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; } @@ -330,10 +331,7 @@ int test_identity() glm::mat4 const M = glm::identity(); 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[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; + Error += glm::all(glm::equal(M, N, 0.0001f)) ? 0 : 1; return Error; }