Check for exact equality before approximate equality
This commit is contained in:
parent
62c0185cb3
commit
f8b4cf0fc4
3 changed files with 21 additions and 1 deletions
|
@ -300,6 +300,11 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
static _ALWAYS_INLINE_ bool is_equal_approx(real_t a, real_t b) {
|
static _ALWAYS_INLINE_ bool is_equal_approx(real_t a, real_t b) {
|
||||||
|
// Check for exact equality first, required to handle "infinity" values.
|
||||||
|
if (a == b) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
// Then check for approximate equality.
|
||||||
real_t tolerance = CMP_EPSILON * abs(a);
|
real_t tolerance = CMP_EPSILON * abs(a);
|
||||||
if (tolerance < CMP_EPSILON) {
|
if (tolerance < CMP_EPSILON) {
|
||||||
tolerance = CMP_EPSILON;
|
tolerance = CMP_EPSILON;
|
||||||
|
@ -308,6 +313,11 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
static _ALWAYS_INLINE_ bool is_equal_approx(real_t a, real_t b, real_t tolerance) {
|
static _ALWAYS_INLINE_ bool is_equal_approx(real_t a, real_t b, real_t tolerance) {
|
||||||
|
// Check for exact equality first, required to handle "infinity" values.
|
||||||
|
if (a == b) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
// Then check for approximate equality.
|
||||||
return abs(a - b) < tolerance;
|
return abs(a - b) < tolerance;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -158,6 +158,11 @@ namespace Godot
|
||||||
|
|
||||||
public static bool IsEqualApprox(real_t a, real_t b)
|
public static bool IsEqualApprox(real_t a, real_t b)
|
||||||
{
|
{
|
||||||
|
// Check for exact equality first, required to handle "infinity" values.
|
||||||
|
if (a == b) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
// Then check for approximate equality.
|
||||||
real_t tolerance = Epsilon * Abs(a);
|
real_t tolerance = Epsilon * Abs(a);
|
||||||
if (tolerance < Epsilon) {
|
if (tolerance < Epsilon) {
|
||||||
tolerance = Epsilon;
|
tolerance = Epsilon;
|
||||||
|
|
|
@ -48,6 +48,11 @@ namespace Godot
|
||||||
|
|
||||||
public static bool IsEqualApprox(real_t a, real_t b, real_t tolerance)
|
public static bool IsEqualApprox(real_t a, real_t b, real_t tolerance)
|
||||||
{
|
{
|
||||||
|
// Check for exact equality first, required to handle "infinity" values.
|
||||||
|
if (a == b) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
// Then check for approximate equality.
|
||||||
return Abs(a - b) < tolerance;
|
return Abs(a - b) < tolerance;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue