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 /tests | |
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 'tests')
-rw-r--r-- | tests/gtests/blenlib/BLI_math_base_test.cc | 87 | ||||
-rw-r--r-- | tests/gtests/blenlib/CMakeLists.txt | 1 |
2 files changed, 88 insertions, 0 deletions
diff --git a/tests/gtests/blenlib/BLI_math_base_test.cc b/tests/gtests/blenlib/BLI_math_base_test.cc new file mode 100644 index 00000000000..0059eb54482 --- /dev/null +++ b/tests/gtests/blenlib/BLI_math_base_test.cc @@ -0,0 +1,87 @@ +/* Apache License, Version 2.0 */ + +#include "testing/testing.h" + +#include "BLI_math.h" + +/* In tests below, when we are using -1.0f as max_diff value, we actually turn the function into a pure-ULP one. */ + +/* Put this here, since we cannot use BLI_assert() in inline math files it seems... */ +TEST(math_base, CompareFFRelativeValid) +{ + EXPECT_TRUE(sizeof(float) == sizeof(int)); +} + +TEST(math_base, CompareFFRelativeNormal) +{ + float f1 = 1.99999988f; /* *(float *)&(*(int *)&f2 - 1) */ + float f2 = 2.00000000f; + float f3 = 2.00000048f; /* *(float *)&(*(int *)&f2 + 2) */ + float f4 = 2.10000000f; /* *(float *)&(*(int *)&f2 + 419430) */ + + const float max_diff = FLT_EPSILON * 0.1f; + + EXPECT_TRUE(compare_ff_relative(f1, f2, max_diff, 1)); + EXPECT_TRUE(compare_ff_relative(f2, f1, max_diff, 1)); + + EXPECT_TRUE(compare_ff_relative(f3, f2, max_diff, 2)); + EXPECT_TRUE(compare_ff_relative(f2, f3, max_diff, 2)); + + EXPECT_FALSE(compare_ff_relative(f3, f2, max_diff, 1)); + EXPECT_FALSE(compare_ff_relative(f2, f3, max_diff, 1)); + + + EXPECT_FALSE(compare_ff_relative(f3, f2, -1.0f, 1)); + EXPECT_FALSE(compare_ff_relative(f2, f3, -1.0f, 1)); + + EXPECT_TRUE(compare_ff_relative(f3, f2, -1.0f, 2)); + EXPECT_TRUE(compare_ff_relative(f2, f3, -1.0f, 2)); + + + EXPECT_FALSE(compare_ff_relative(f4, f2, max_diff, 64)); + EXPECT_FALSE(compare_ff_relative(f2, f4, max_diff, 64)); + + EXPECT_TRUE(compare_ff_relative(f1, f3, max_diff, 64)); + EXPECT_TRUE(compare_ff_relative(f3, f1, max_diff, 64)); +} + +TEST(math_base, CompareFFRelativeZero) +{ + float f0 = 0.0f; + float f1 = 4.2038954e-045f; /* *(float *)&(*(int *)&f0 + 3) */ + + float fn0 = -0.0f; + float fn1 = -2.8025969e-045f; /* *(float *)&(*(int *)&fn0 - 2) */ + + const float max_diff = FLT_EPSILON * 0.1f; + + EXPECT_TRUE(compare_ff_relative(f0, f1, -1.0f, 3)); + EXPECT_TRUE(compare_ff_relative(f1, f0, -1.0f, 3)); + + EXPECT_FALSE(compare_ff_relative(f0, f1, -1.0f, 1)); + EXPECT_FALSE(compare_ff_relative(f1, f0, -1.0f, 1)); + + EXPECT_TRUE(compare_ff_relative(fn0, fn1, -1.0f, 8)); + EXPECT_TRUE(compare_ff_relative(fn1, fn0, -1.0f, 8)); + + + EXPECT_TRUE(compare_ff_relative(f0, f1, max_diff, 1)); + EXPECT_TRUE(compare_ff_relative(f1, f0, max_diff, 1)); + + EXPECT_TRUE(compare_ff_relative(fn0, f0, max_diff, 1)); + EXPECT_TRUE(compare_ff_relative(f0, fn0, max_diff, 1)); + + EXPECT_TRUE(compare_ff_relative(f0, fn1, max_diff, 1)); + EXPECT_TRUE(compare_ff_relative(fn1, f0, max_diff, 1)); + + + /* Note: in theory, this should return false, since 0.0f and -0.0f have 0x80000000 diff, + * but overflow in substraction seems to break something here + * (abs(*(int *)&fn0 - *(int *)&f0) == 0x80000000 == fn0), probably because int32 cannot hold this abs value. + * this is yet another illustration of why one shall never use (near-)zero floats in pure-ULP comparison. */ +// EXPECT_FALSE(compare_ff_relative(fn0, f0, -1.0f, 1024)); +// EXPECT_FALSE(compare_ff_relative(f0, fn0, -1.0f, 1024)); + + EXPECT_FALSE(compare_ff_relative(fn0, f1, -1.0f, 1024)); + EXPECT_FALSE(compare_ff_relative(f1, fn0, -1.0f, 1024)); +} diff --git a/tests/gtests/blenlib/CMakeLists.txt b/tests/gtests/blenlib/CMakeLists.txt index b4cb2b9d0ea..acd9f944e4c 100644 --- a/tests/gtests/blenlib/CMakeLists.txt +++ b/tests/gtests/blenlib/CMakeLists.txt @@ -38,6 +38,7 @@ set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} ${PLATFORM_LIN BLENDER_TEST(BLI_stack "bf_blenlib") BLENDER_TEST(BLI_math_color "bf_blenlib") BLENDER_TEST(BLI_math_geom "bf_blenlib") +BLENDER_TEST(BLI_math_base "bf_blenlib") BLENDER_TEST(BLI_string "bf_blenlib") BLENDER_TEST(BLI_path_util "bf_blenlib;extern_wcwidth;${ZLIB_LIBRARIES}") BLENDER_TEST(BLI_polyfill2d "bf_blenlib") |