Index: cc/base/math_util_unittest.cc |
diff --git a/cc/base/math_util_unittest.cc b/cc/base/math_util_unittest.cc |
index 9d44fba8ac034b9a7637bff7e0f70408fef11ca4..972a51185f176dcdc0a8b61e366e27ddc17760b2 100644 |
--- a/cc/base/math_util_unittest.cc |
+++ b/cc/base/math_util_unittest.cc |
@@ -384,5 +384,46 @@ TEST(MathUtilTest, RoundDownUnderflow) { |
EXPECT_TRUE(MathUtil::VerifyRoundDown<int16_t>(-123, 50)); |
} |
+TEST(MathUtilTest, AlmostEqualFloats) { |
+ float f1 = 7.33907556533813f; |
vmpstr
2015/10/19 18:58:38
nit: f1 -> first_value, f2 -> second_value (or som
|
+ float f2 = 7.33907508850098f; |
+ |
+ EXPECT_TRUE(f1 != f2); |
+ |
+ int max_precision = std::numeric_limits<float>::digits10 - 1; |
+ |
+ // Check with all possible precisions, the almost equal floats (differing by |
+ // by small magnitude of epsilon) are same. |
+ for (int i = 1; i <= max_precision; ++i) { |
+ float f3 = MathUtil::RoundToFixedPrecision(f1, i); |
vmpstr
2015/10/19 18:58:38
nit: f3 -> rounded_first_value, f2 -> rounded_seco
|
+ float f4 = MathUtil::RoundToFixedPrecision(f2, i); |
+ EXPECT_TRUE(f3 == f4); |
+ } |
+ |
+ // Check with different precisions, different values are returned. |
+ for (int i = 1; i <= max_precision; ++i) { |
+ for (int j = i + 1; j <= max_precision; ++j) { |
+ float f3 = MathUtil::RoundToFixedPrecision(f1, i); |
+ float f4 = MathUtil::RoundToFixedPrecision(f1, j); |
+ EXPECT_FALSE(f3 == f4); |
+ } |
+ } |
+ |
+ // Different floats differing by some magnitude of epsilon. |
+ float f5 = f1 + (10 * std::numeric_limits<float>::epsilon()); |
+ EXPECT_TRUE(f1 != f5); |
+ EXPECT_TRUE(MathUtil::RoundToFixedPrecision(f1, 2) == |
+ MathUtil::RoundToFixedPrecision(f5, 2)); |
+ |
+ // Check different floats. |
+ float f6 = 7.3f; |
+ float f7 = 7.4f; |
+ for (int i = 1; i <= max_precision; ++i) { |
+ float f8 = MathUtil::RoundToFixedPrecision(f6, i); |
+ float f9 = MathUtil::RoundToFixedPrecision(f7, i); |
+ EXPECT_FALSE(f8 == f9); |
+ } |
+} |
+ |
} // namespace |
} // namespace cc |