diff options
author | Bastien Montagne <montagne29@wanadoo.fr> | 2015-07-10 15:32:35 +0300 |
---|---|---|
committer | Bastien Montagne <montagne29@wanadoo.fr> | 2015-07-10 16:02:43 +0300 |
commit | 7837f0e8332f3726e0322b0c48b0da4d7c2d5813 (patch) | |
tree | 4e0b9d6fb5faa6bacd080484d7ef625566d32699 /source/blender/blenlib/intern/math_base_inline.c | |
parent | bbcbd2eed9081e0c1127d32e41592a8dc6b5604b (diff) |
BLI_math 'compare' cleanup & enhancements.
This commit:
* Adds a 'compare_ff' function for absolute 'almost equal' comparison of floats.
* Makes 'compare_vxvx' functions use that new 'compare_ff' one.
* Adds a 'compare_ff_relative' function for secured ulp-based relative comparison of floats.
* Adds matching 'compare_vxvx_relative' functions.
* Adds some basic tests for compare_ff_relative.
See https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/
Note that we could replace our python/mathutils' EXPP_FloatsAreEqual() by BLI's compare_ff_relative
(using a very small absolute max_diff), but these do not have exact same behavior...
Left a comment there for now, we can do it later if/when we are sure it won't break anything!
Diffstat (limited to 'source/blender/blenlib/intern/math_base_inline.c')
-rw-r--r-- | source/blender/blenlib/intern/math_base_inline.c | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/source/blender/blenlib/intern/math_base_inline.c b/source/blender/blenlib/intern/math_base_inline.c index facee8b0a89..f70c12bd26e 100644 --- a/source/blender/blenlib/intern/math_base_inline.c +++ b/source/blender/blenlib/intern/math_base_inline.c @@ -252,6 +252,46 @@ MINLINE int max_iiii(int a, int b, int c, int d) return max_ii(max_iii(a, b, c), d); } +/** + * Almost-equal for IEEE floats, using absolute difference method. + * + * \param max_diff the maximum absolute difference. + */ +MINLINE int compare_ff(float a, float b, const float max_diff) +{ + return fabsf(a - b) <= max_diff; +} + +/** + * Almost-equal for IEEE floats, using their integer representation (mixing ULP and absolute difference methods). + * + * \param max_diff is the maximum absolute difference (allows to take care of the near-zero area, + * where relative difference methods cannot really work). + * \param max_ulps is the 'maximum number of floats + 1' allowed between \a a and \a b to consider them equal. + * + * \see https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/ + */ +MINLINE int compare_ff_relative(float a, float b, const float max_diff, const int max_ulps) +{ + union {float f; int i;} ua, ub; + +#if 0 /* No BLI_assert in INLINE :/ */ + BLI_assert(sizeof(float) == sizeof(int)); + BLI_assert(max_ulps < (1 << 22)); +#endif + + if (fabsf(a - b) <= max_diff) { + return 1; + } + + ua.f = a; + ub.f = b; + + /* Important to compare sign from integers, since (-0.0f < 0) is false + * (though this shall not be an issue in common cases)... */ + return ((ua.i < 0) != (ub.i < 0)) ? 0 : (abs(ua.i - ub.i) <= max_ulps) ? 1 : 0; +} + MINLINE float signf(float f) { return (f < 0.f) ? -1.f : 1.f; |