diff options
author | Aaron Franke <arnfranke@yahoo.com> | 2019-04-25 13:19:14 -0400 |
---|---|---|
committer | Aaron Franke <arnfranke@yahoo.com> | 2019-04-25 13:20:29 -0400 |
commit | b2e1c9c276cd833b642dfbaea3dde36d490b014e (patch) | |
tree | 1e5a46ea7be22d211525d1994f16d0240706961c /core/math/math_funcs.h | |
parent | cce2e4b07c1c4b6f2f9d72cac340d9f9ecbb790e (diff) |
[Core] Approximate equality
Diffstat (limited to 'core/math/math_funcs.h')
-rw-r--r-- | core/math/math_funcs.h | 19 |
1 files changed, 13 insertions, 6 deletions
diff --git a/core/math/math_funcs.h b/core/math/math_funcs.h index 0d209402dd..a75f2fb4ab 100644 --- a/core/math/math_funcs.h +++ b/core/math/math_funcs.h @@ -272,13 +272,20 @@ public: return diff < epsilon; } - static _ALWAYS_INLINE_ bool is_equal_approx(real_t a, real_t b, real_t epsilon = CMP_EPSILON) { - // TODO: Comparing floats for approximate-equality is non-trivial. - // Using epsilon should cover the typical cases in Godot (where a == b is used to compare two reals), such as matrix and vector comparison operators. - // A proper implementation in terms of ULPs should eventually replace the contents of this function. - // See https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/ for details. + static _ALWAYS_INLINE_ bool is_equal_approx(real_t a, real_t b) { + real_t tolerance = CMP_EPSILON * abs(a); + if (tolerance < CMP_EPSILON) { + tolerance = CMP_EPSILON; + } + return abs(a - b) < tolerance; + } + + static _ALWAYS_INLINE_ bool is_equal_approx(real_t a, real_t b, real_t tolerance) { + return abs(a - b) < tolerance; + } - return abs(a - b) < epsilon; + static _ALWAYS_INLINE_ bool is_zero_approx(real_t s) { + return abs(s) < CMP_EPSILON; } static _ALWAYS_INLINE_ float absf(float g) { |