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

View File

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

View File

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

View File

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

View File

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

View File

@ -16,7 +16,6 @@ int test_equal()
Error += glm::all(glm::equal(glm::mat4x3(1), glm::mat4x3(1), 0.001f)) ? 0 : 1; Error += glm::all(glm::equal(glm::mat4x3(1), glm::mat4x3(1), 0.001f)) ? 0 : 1;
Error += glm::all(glm::equal(glm::mat4x3(1), glm::mat4x3(2), glm::vec4(0.001f))) ? 1 : 0; Error += glm::all(glm::equal(glm::mat4x3(1), glm::mat4x3(2), glm::vec4(0.001f))) ? 1 : 0;
Error += glm::all(glm::equal(glm::mat4x3(1), glm::mat4x3(1))) ? 0 : 1;
return Error; return Error;
} }
@ -35,7 +34,6 @@ int test_notEqual()
Error += !glm::any(glm::notEqual(glm::mat4x3(1), glm::mat4x3(1), 0.001f)) ? 0 : 1; Error += !glm::any(glm::notEqual(glm::mat4x3(1), glm::mat4x3(1), 0.001f)) ? 0 : 1;
Error += !glm::any(glm::notEqual(glm::mat4x3(1), glm::mat4x3(2), glm::vec4(0.001f))) ? 1 : 0; Error += !glm::any(glm::notEqual(glm::mat4x3(1), glm::mat4x3(2), glm::vec4(0.001f))) ? 1 : 0;
Error += !glm::any(glm::notEqual(glm::mat4x3(1), glm::mat4x3(1))) ? 0 : 1;
return Error; return Error;
} }

View File

@ -1,6 +1,7 @@
#include <glm/packing.hpp> #include <glm/packing.hpp>
#include <glm/gtc/packing.hpp> #include <glm/gtc/packing.hpp>
#include <glm/gtc/epsilon.hpp> #include <glm/gtc/epsilon.hpp>
#include <glm/ext/vector_relational.hpp>
#include <cstdio> #include <cstdio>
#include <vector> #include <vector>
@ -101,8 +102,8 @@ int test_Half4x16()
glm::u16vec4 p2 = glm::packHalf(v0); glm::u16vec4 p2 = glm::packHalf(v0);
glm::vec4 v2 = glm::unpackHalf(p2); glm::vec4 v2 = glm::unpackHalf(p2);
Error += glm::all(glm::equal(v0, v1)) ? 0 : 1; Error += glm::all(glm::equal(v0, v1, glm::epsilon<float>())) ? 0 : 1;
Error += glm::all(glm::equal(v0, v2)) ? 0 : 1; Error += glm::all(glm::equal(v0, v2, glm::epsilon<float>())) ? 0 : 1;
} }
return Error; return Error;
@ -236,7 +237,7 @@ int test_F2x11_1x10()
glm::vec3 v0 = glm::unpackF2x11_1x10(p0); glm::vec3 v0 = glm::unpackF2x11_1x10(p0);
glm::uint32 p1 = glm::packF2x11_1x10(v0); glm::uint32 p1 = glm::packF2x11_1x10(v0);
glm::vec3 v1 = glm::unpackF2x11_1x10(p1); glm::vec3 v1 = glm::unpackF2x11_1x10(p1);
Error += glm::all(glm::equal(v0, v1)) ? 0 : 1; Error += glm::all(glm::equal(v0, v1, glm::epsilon<float>())) ? 0 : 1;
} }
return Error; return Error;
@ -260,7 +261,7 @@ int test_F3x9_E1x5()
glm::vec3 v0 = glm::unpackF3x9_E1x5(p0); glm::vec3 v0 = glm::unpackF3x9_E1x5(p0);
glm::uint32 p1 = glm::packF3x9_E1x5(v0); glm::uint32 p1 = glm::packF3x9_E1x5(v0);
glm::vec3 v1 = glm::unpackF3x9_E1x5(p1); glm::vec3 v1 = glm::unpackF3x9_E1x5(p1);
Error += glm::all(glm::epsilonEqual(v0, v1, 0.01f)) ? 0 : 1; Error += glm::all(glm::equal(v0, v1, glm::epsilon<float>())) ? 0 : 1;
} }
return Error; return Error;
@ -276,7 +277,7 @@ int test_RGBM()
glm::vec4 const RGBM = glm::packRGBM(Color); glm::vec4 const RGBM = glm::packRGBM(Color);
glm::vec3 const Result= glm::unpackRGBM(RGBM); glm::vec3 const Result= glm::unpackRGBM(RGBM);
Error += glm::all(glm::epsilonEqual(Color, Result, 0.01f)) ? 0 : 1; Error += glm::all(glm::equal(Color, Result, 0.01f)) ? 0 : 1;
} }
return Error; return Error;