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

This commit is contained in:
Groove
2018-07-28 20:24:45 +02:00
parent 6c981b07c1
commit 5ecc27ff93
7 changed files with 73 additions and 70 deletions

View File

@@ -80,21 +80,23 @@ int test_vec4_ctor()
Error += glm::all(glm::equal(A, L)) ? 0 : 1;
Error += glm::all(glm::equal(A, M)) ? 0 : 1;
}
# elif GLM_SWIZZLE == GLM_SWIZZLE_OPERATOR
# endif
# if GLM_SWIZZLE
{
glm::vec4 A = glm::vec4(1.0f, 2.0f, 3.0f, 4.0f);
glm::vec4 B = A.xyzw();
glm::vec4 C(A.xyzw());
glm::vec4 D(A.xyzw());
glm::vec4 E(A.x, A.yzw());
glm::vec4 F(A.x, A.yzw());
glm::vec4 G(A.xyz(), A.w);
glm::vec4 H(A.xyz(), A.w);
glm::vec4 I(A.xy(), A.zw());
glm::vec4 J(A.xy(), A.zw());
glm::vec4 K(A.x, A.y, A.zw());
glm::vec4 L(A.x, A.yz(), A.w);
glm::vec4 M(A.xy(), A.z, A.w);
glm::ivec4 A = glm::vec4(1.0f, 2.0f, 3.0f, 4.0f);
glm::ivec4 B = A.xyzw();
glm::ivec4 C(A.xyzw());
glm::ivec4 D(A.xyzw());
glm::ivec4 E(A.x, A.yzw());
glm::ivec4 F(A.x, A.yzw());
glm::ivec4 G(A.xyz(), A.w);
glm::ivec4 H(A.xyz(), A.w);
glm::ivec4 I(A.xy(), A.zw());
glm::ivec4 J(A.xy(), A.zw());
glm::ivec4 K(A.x, A.y, A.zw());
glm::ivec4 L(A.x, A.yz(), A.w);
glm::ivec4 M(A.xy(), A.z, A.w);
Error += glm::all(glm::equal(A, B)) ? 0 : 1;
Error += glm::all(glm::equal(A, C)) ? 0 : 1;
@@ -109,7 +111,7 @@ int test_vec4_ctor()
Error += glm::all(glm::equal(A, L)) ? 0 : 1;
Error += glm::all(glm::equal(A, M)) ? 0 : 1;
}
# endif//GLM_SWIZZLE == GLM_SWIZZLE_OPERATOR
# endif//GLM_SWIZZLE
{
glm::ivec4 A(1);
@@ -119,18 +121,18 @@ int test_vec4_ctor()
}
{
std::vector<glm::vec4> Tests;
Tests.push_back(glm::vec4(glm::vec2(1, 2), 3, 4));
Tests.push_back(glm::vec4(1, glm::vec2(2, 3), 4));
Tests.push_back(glm::vec4(1, 2, glm::vec2(3, 4)));
Tests.push_back(glm::vec4(glm::vec3(1, 2, 3), 4));
Tests.push_back(glm::vec4(1, glm::vec3(2, 3, 4)));
Tests.push_back(glm::vec4(glm::vec2(1, 2), glm::vec2(3, 4)));
Tests.push_back(glm::vec4(1, 2, 3, 4));
Tests.push_back(glm::vec4(glm::vec4(1, 2, 3, 4)));
std::vector<glm::ivec4> Tests;
Tests.push_back(glm::ivec4(glm::ivec2(1, 2), 3, 4));
Tests.push_back(glm::ivec4(1, glm::ivec2(2, 3), 4));
Tests.push_back(glm::ivec4(1, 2, glm::ivec2(3, 4)));
Tests.push_back(glm::ivec4(glm::ivec3(1, 2, 3), 4));
Tests.push_back(glm::ivec4(1, glm::ivec3(2, 3, 4)));
Tests.push_back(glm::ivec4(glm::ivec2(1, 2), glm::ivec2(3, 4)));
Tests.push_back(glm::ivec4(1, 2, 3, 4));
Tests.push_back(glm::ivec4(glm::ivec4(1, 2, 3, 4)));
for(std::size_t i = 0; i < Tests.size(); ++i)
Error += Tests[i] == glm::vec4(1, 2, 3, 4) ? 0 : 1;
Error += Tests[i] == glm::ivec4(1, 2, 3, 4) ? 0 : 1;
}
return Error;

View File

@@ -194,8 +194,8 @@ int test_cpp_version()
int test_operators()
{
glm::ivec3 A(1.0f);
glm::ivec3 B(1.0f);
glm::ivec3 A(1);
glm::ivec3 B(1);
bool R = A != B;
bool S = A == B;

View File

@@ -1,6 +1,8 @@
#include <glm/glm.hpp>
#include <glm/gtc/vec1.hpp>
#include <glm/gtc/quaternion.hpp>
#include <glm/gtc/constants.hpp>
#include <glm/ext/vector_relational.hpp>
#include <glm/glm.hpp>
static int test_vec1_ctor()
{
@@ -107,10 +109,10 @@ static int test_mat2x2_ctor()
} A, B;
A.f = glm::mat2x2(0);
Error += glm::all(glm::equal(A.i[0], glm::vec2(0))) ? 0 : 1;
Error += glm::all(glm::equal(A.i[0], glm::vec2(0), glm::epsilon<float>())) ? 0 : 1;
B.f = glm::mat2x2(1);
Error += glm::all(glm::equal(B.i[0], glm::vec2(1, 0))) ? 0 : 1;
Error += glm::all(glm::equal(B.i[0], glm::vec2(1, 0), glm::epsilon<float>())) ? 0 : 1;
}
# endif//GLM_LANG & GLM_LANG_CXX11_FLAG
@@ -130,10 +132,10 @@ static int test_mat2x3_ctor()
} A, B;
A.f = glm::mat2x3(0);
Error += glm::all(glm::equal(A.i[0], glm::vec3(0))) ? 0 : 1;
Error += glm::all(glm::equal(A.i[0], glm::vec3(0), glm::epsilon<float>())) ? 0 : 1;
B.f = glm::mat2x3(1);
Error += glm::all(glm::equal(B.i[0], glm::vec3(1, 0, 0))) ? 0 : 1;
Error += glm::all(glm::equal(B.i[0], glm::vec3(1, 0, 0), glm::epsilon<float>())) ? 0 : 1;
}
# endif//GLM_LANG & GLM_LANG_CXX11_FLAG
@@ -154,11 +156,11 @@ static int test_mat2x4_ctor()
A.f = glm::mat2x4(0);
glm::vec4 const C(0, 0, 0, 0);
Error += glm::all(glm::equal(A.i[0], C)) ? 0 : 1;
Error += glm::all(glm::equal(A.i[0], C, glm::epsilon<float>())) ? 0 : 1;
B.f = glm::mat2x4(1);
glm::vec4 const D(1, 0, 0, 0);
Error += glm::all(glm::equal(B.i[0], D)) ? 0 : 1;
Error += glm::all(glm::equal(B.i[0], D, glm::epsilon<float>())) ? 0 : 1;
}
# endif//GLM_LANG & GLM_LANG_CXX11_FLAG
@@ -178,10 +180,10 @@ static int test_mat3x2_ctor()
} A, B;
A.f = glm::mat3x2(0);
Error += glm::all(glm::equal(A.i[0], glm::vec2(0))) ? 0 : 1;
Error += glm::all(glm::equal(A.i[0], glm::vec2(0), glm::epsilon<float>())) ? 0 : 1;
B.f = glm::mat3x2(1);
Error += glm::all(glm::equal(B.i[0], glm::vec2(1, 0))) ? 0 : 1;
Error += glm::all(glm::equal(B.i[0], glm::vec2(1, 0), glm::epsilon<float>())) ? 0 : 1;
}
# endif//GLM_LANG & GLM_LANG_CXX11_FLAG
@@ -201,10 +203,10 @@ static int test_mat3x3_ctor()
} A, B;
A.f = glm::mat3x3(0);
Error += glm::all(glm::equal(A.i[0], glm::vec3(0))) ? 0 : 1;
Error += glm::all(glm::equal(A.i[0], glm::vec3(0), glm::epsilon<float>())) ? 0 : 1;
B.f = glm::mat3x3(1);
Error += glm::all(glm::equal(B.i[0], glm::vec3(1, 0, 0))) ? 0 : 1;
Error += glm::all(glm::equal(B.i[0], glm::vec3(1, 0, 0), glm::epsilon<float>())) ? 0 : 1;
}
# endif//GLM_LANG & GLM_LANG_CXX11_FLAG
@@ -224,10 +226,10 @@ static int test_mat3x4_ctor()
} A, B;
A.f = glm::mat3x4(0);
Error += glm::all(glm::equal(A.i[0], glm::vec4(0))) ? 0 : 1;
Error += glm::all(glm::equal(A.i[0], glm::vec4(0), glm::epsilon<float>())) ? 0 : 1;
B.f = glm::mat3x4(1);
Error += glm::all(glm::equal(B.i[0], glm::vec4(1, 0, 0, 0))) ? 0 : 1;
Error += glm::all(glm::equal(B.i[0], glm::vec4(1, 0, 0, 0), glm::epsilon<float>())) ? 0 : 1;
}
# endif//GLM_LANG & GLM_LANG_CXX11_FLAG
@@ -247,10 +249,10 @@ static int test_mat4x2_ctor()
} A, B;
A.f = glm::mat4x2(0);
Error += glm::all(glm::equal(A.i[0], glm::vec2(0))) ? 0 : 1;
Error += glm::all(glm::equal(A.i[0], glm::vec2(0), glm::epsilon<float>())) ? 0 : 1;
B.f = glm::mat4x2(1);
Error += glm::all(glm::equal(B.i[0], glm::vec2(1, 0))) ? 0 : 1;
Error += glm::all(glm::equal(B.i[0], glm::vec2(1, 0), glm::epsilon<float>())) ? 0 : 1;
}
# endif//GLM_LANG & GLM_LANG_CXX11_FLAG
@@ -270,10 +272,10 @@ static int test_mat4x3_ctor()
} A, B;
A.f = glm::mat4x3(0);
Error += glm::all(glm::equal(A.i[0], glm::vec3(0))) ? 0 : 1;
Error += glm::all(glm::equal(A.i[0], glm::vec3(0), glm::epsilon<float>())) ? 0 : 1;
B.f = glm::mat4x3(1);
Error += glm::all(glm::equal(B.i[0], glm::vec3(1, 0, 0))) ? 0 : 1;
Error += glm::all(glm::equal(B.i[0], glm::vec3(1, 0, 0), glm::epsilon<float>())) ? 0 : 1;
}
# endif//GLM_LANG & GLM_LANG_CXX11_FLAG
@@ -293,10 +295,10 @@ static int test_mat4x4_ctor()
} A, B;
A.f = glm::mat4(0);
Error += glm::all(glm::equal(A.i[0], glm::vec4(0))) ? 0 : 1;
Error += glm::all(glm::equal(A.i[0], glm::vec4(0), glm::epsilon<float>())) ? 0 : 1;
B.f = glm::mat4(1);
Error += glm::all(glm::equal(B.i[0], glm::vec4(1, 0, 0, 0))) ? 0 : 1;
Error += glm::all(glm::equal(B.i[0], glm::vec4(1, 0, 0, 0), glm::epsilon<float>())) ? 0 : 1;
}
# endif//GLM_LANG & GLM_LANG_CXX11_FLAG
@@ -316,10 +318,10 @@ static int test_quat_ctor()
} A, B;
A.f = glm::quat(0, 0, 0, 0);
Error += glm::all(glm::equal(A.i, glm::quat(0, 0, 0, 0))) ? 0 : 1;
Error += glm::all(glm::equal(A.i, glm::quat(0, 0, 0, 0), glm::epsilon<float>())) ? 0 : 1;
B.f = glm::quat(1, 1, 1, 1);
Error += glm::all(glm::equal(B.i, glm::quat(1, 1, 1, 1))) ? 0 : 1;
Error += glm::all(glm::equal(B.i, glm::quat(1, 1, 1, 1), glm::epsilon<float>())) ? 0 : 1;
}
# endif//GLM_LANG & GLM_LANG_CXX11_FLAG

View File

@@ -18,8 +18,8 @@ static int test_operators()
int Error = 0;
{
glm::ivec2 A(1.0f);
glm::ivec2 B(1.0f);
glm::ivec2 A(1);
glm::ivec2 B(1);
Error += A != B ? 1 : 0;
Error += A == B ? 0 : 1;
}

View File

@@ -523,19 +523,19 @@ static int test_swizzle()
# if GLM_SWIZZLE == GLM_SWIZZLE_OPERATOR
{
glm::vec4 A = glm::vec4(1.0f, 2.0f, 3.0f, 4.0f);
glm::vec4 B = A.xyzw;
glm::vec4 C(A.xyzw);
glm::vec4 D(A.xyzw());
glm::vec4 E(A.x, A.yzw);
glm::vec4 F(A.x, A.yzw());
glm::vec4 G(A.xyz, A.w);
glm::vec4 H(A.xyz(), A.w);
glm::vec4 I(A.xy, A.zw);
glm::vec4 J(A.xy(), A.zw());
glm::vec4 K(A.x, A.y, A.zw);
glm::vec4 L(A.x, A.yz, A.w);
glm::vec4 M(A.xy, A.z, A.w);
glm::ivec4 A = glm::ivec4(1.0f, 2.0f, 3.0f, 4.0f);
glm::ivec4 B = A.xyzw;
glm::ivec4 C(A.xyzw);
glm::ivec4 D(A.xyzw());
glm::ivec4 E(A.x, A.yzw);
glm::ivec4 F(A.x, A.yzw());
glm::ivec4 G(A.xyz, A.w);
glm::ivec4 H(A.xyz(), A.w);
glm::ivec4 I(A.xy, A.zw);
glm::ivec4 J(A.xy(), A.zw());
glm::ivec4 K(A.x, A.y, A.zw);
glm::ivec4 L(A.x, A.yz, A.w);
glm::ivec4 M(A.xy, A.z, A.w);
Error += glm::all(glm::equal(A, B)) ? 0 : 1;
Error += glm::all(glm::equal(A, C)) ? 0 : 1;