use FLOAT_ERROR instead of FLOAT_EPSILON

This commit is contained in:
hyzboy 2024-11-05 00:40:39 +08:00
parent 6d9bced95c
commit 2dfcdf0e41
3 changed files with 30 additions and 29 deletions

View File

@ -14,15 +14,15 @@ namespace hgl
constexpr const double HGL_DOUBLE_MAX =std::numeric_limits<double>::max(); ///<最大双精度浮点数
constexpr const double HGL_DOUBLE_EPSILON =std::numeric_limits<double>::epsilon(); ///<双精度浮点数精度最小值
template<typename T> bool IsNearlyZero(const T value);
constexpr const float HGL_HALF_FLOAT_ERROR=0.001f; ///<半精度浮点数最小误差值
constexpr const float HGL_FLOAT_ERROR =0.0001f; ///<浮点数最小误差值
constexpr const double HGL_DOUBLE_ERROR =0.00000001; ///<双精度浮点数最小误差值
template<> inline bool IsNearlyZero<float>(const float value){return(fabsf(value) <= HGL_FLOAT_EPSILON);}
template<> inline bool IsNearlyZero<double>(const double value){return(fabs(value) <= HGL_DOUBLE_EPSILON);}
inline bool IsNearlyZero(const float value,const float err=HGL_FLOAT_ERROR){return(fabsf(value) <= err);}
inline bool IsNearlyZero(const double value,const double err=HGL_DOUBLE_ERROR){return(fabs(value) <= err);}
template<typename T> bool IsNearlyEqual(const T a,const T b);
template<> inline bool IsNearlyEqual<float>(const float a,const float b){return(fabsf(a - b) <= HGL_FLOAT_EPSILON);}
template<> inline bool IsNearlyEqual<double>(const double a,const double b){return(fabs(a - b) <= HGL_DOUBLE_EPSILON); }
inline bool IsNearlyEqual(const float a,const float b,const float err=HGL_FLOAT_ERROR){return(fabsf(a - b) <= err);}
inline bool IsNearlyEqual(const double a,const double b,const double err=HGL_DOUBLE_ERROR){return(fabs(a - b) <= err); }
constexpr const double HGL_E =2.7182818284590452353602874713526624977572470936999595749669676277240766303535475945713821785251664274; //欧拉数(自然对数的底数)
constexpr const double HGL_LOG2E =1.44269504088896340736;

View File

@ -20,14 +20,14 @@ namespace hgl
#define DEFINE_MATRIX(num) using Matrix##num##f=glm::mat##num; \
const Matrix##num##f Identity##num##f=Matrix##num##f(1.0f); \
inline bool IsIdentityMatrix(const Matrix##num##f &m){return(hgl_cmp(m,Identity##num##f)==0);} \
inline bool IsNearlyEqual(const Matrix##num##f &m1,const Matrix##num##f &m2) \
inline bool IsNearlyEqual(const Matrix##num##f &m1,const Matrix##num##f &m2,const float err=HGL_FLOAT_ERROR) \
{ \
float *f1=(float *)&m1;\
float *f2=(float *)&m2;\
\
for(int i=0;i<sizeof(Matrix##num##f)/sizeof(float);i++) \
{ \
if(!IsNearlyEqual(*f1,*f2)) \
if(!IsNearlyEqual(*f1,*f2,err)) \
return(false); \
\
++f1;++f2; \
@ -46,9 +46,9 @@ namespace hgl
//#undef DEFINE_MATRIX
inline const bool IsNearlyEqual(const Quatf &q1,const Quatf &q2)
inline const bool IsNearlyEqual(const Quatf &q1,const Quatf &q2,const float err=HGL_FLOAT_ERROR)
{
return glm::all(glm::epsilonEqual(q1,q2,glm::epsilon<float>()));
return glm::all(glm::epsilonEqual(q1,q2,err));
}
inline Matrix4f inverse(const Matrix4f &m)

View File

@ -263,37 +263,38 @@ namespace hgl
return from + (to - from) * alpha;
}
inline bool IsNearlyEqual(const float v1,const float v2,const float epsilon=HGL_FLOAT_EPSILON)
inline bool IsNearlyEqual(const Vector2f &v1,const Vector2f &v2,const float err=HGL_FLOAT_ERROR)
{
return glm::epsilonEqual(v1,v2,epsilon);
if(!IsNearlyEqual(v1.x,v2.x,err))return(false);
if(!IsNearlyEqual(v1.y,v2.y,err))return(false);
return(true);
}
inline bool IsNearlyEqual(const Vector2f &v1,const Vector2f &v2,const float epsilon=HGL_FLOAT_EPSILON)
inline bool IsNearlyEqual(const Vector2d &v1,const Vector2d &v2,const double err=HGL_DOUBLE_ERROR)
{
glm::bvec2 result=glm::epsilonEqual(v1,v2,epsilon);
if(!IsNearlyEqual(v1.x,v2.x,err))return(false);
if(!IsNearlyEqual(v1.y,v2.y,err))return(false);
return result.x && result.y;
return(true);
}
inline bool IsNearlyEqual(const Vector2d &v1,const Vector2d &v2,const double epsilon=HGL_DOUBLE_EPSILON)
inline bool IsNearlyEqual(const Vector3f &v1,const Vector3f &v2,const float err=HGL_FLOAT_ERROR)
{
glm::bvec2 result=glm::epsilonEqual(v1,v2,epsilon);
if(!IsNearlyEqual(v1.x,v2.x,err))return(false);
if(!IsNearlyEqual(v1.y,v2.y,err))return(false);
if(!IsNearlyEqual(v1.z,v2.z,err))return(false);
return result.x && result.y;
return(true);
}
inline bool IsNearlyEqual(const Vector3f &v1,const Vector3f &v2,const float epsilon=HGL_FLOAT_EPSILON)
inline bool IsNearlyEqual(const Vector3d &v1,const Vector3d &v2,const double err=HGL_DOUBLE_ERROR)
{
glm::bvec3 result=glm::epsilonEqual(v1,v2,epsilon);
if(!IsNearlyEqual(v1.x,v2.x,err))return(false);
if(!IsNearlyEqual(v1.y,v2.y,err))return(false);
if(!IsNearlyEqual(v1.z,v2.z,err))return(false);
return result.x && result.y && result.z;
}
inline bool IsNearlyEqual(const Vector3d &v1,const Vector3d &v2,const double epsilon=HGL_DOUBLE_EPSILON)
{
glm::bvec3 result=glm::epsilonEqual(v1,v2,epsilon);
return result.x && result.y && result.z;
return(true);
}
template<typename T,glm::qualifier Q>