Commit 03bbeb90 authored by gcwenger's avatar gcwenger Committed by Robert Kimball

all_close_f error reporting clarification. all_close_f test cases spaced out for…

all_close_f error reporting clarification. all_close_f test cases spaced out for easier interpretation. (#2247)
parent e9e4f418
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
// limitations under the License. // limitations under the License.
//***************************************************************************** //*****************************************************************************
#include <algorithm>
#include <bitset> #include <bitset>
#include <cmath> #include <cmath>
#include <limits> #include <limits>
...@@ -48,7 +49,23 @@ string float_to_bits(float f) ...@@ -48,7 +49,23 @@ string float_to_bits(float f)
FloatUnion fu{f}; FloatUnion fu{f};
stringstream ss; stringstream ss;
ss << bitset<32>(fu.i); ss << bitset<32>(fu.i);
return ss.str(); string unformatted = ss.str();
string formatted;
formatted.reserve(41);
// Sign
formatted.push_back(unformatted[0]);
formatted.append(" ");
// Exponent
formatted.append(unformatted, 1, 8);
formatted.append(" ");
// Mantissa
formatted.append(unformatted, 9, 3);
for (int i = 12; i < 32; i += 4)
{
formatted.push_back(' ');
formatted.append(unformatted, i, 4);
}
return formatted;
} }
string double_to_bits(double d) string double_to_bits(double d)
...@@ -56,16 +73,35 @@ string double_to_bits(double d) ...@@ -56,16 +73,35 @@ string double_to_bits(double d)
DoubleUnion du{d}; DoubleUnion du{d};
stringstream ss; stringstream ss;
ss << bitset<64>(du.i); ss << bitset<64>(du.i);
return ss.str(); string unformatted = ss.str();
string formatted;
formatted.reserve(80);
// Sign
formatted.push_back(unformatted[0]);
formatted.append(" ");
// Exponent
formatted.append(unformatted, 1, 11);
formatted.push_back(' ');
// Mantissa
for (int i = 12; i < 64; i += 4)
{
formatted.push_back(' ');
formatted.append(unformatted, i, 4);
}
return formatted;
} }
float bits_to_float(const string& s) float bits_to_float(const string& s)
{ {
if (s.size() != 32) string unformatted = s;
unformatted.erase(remove_if(unformatted.begin(), unformatted.end(), ::isspace),
unformatted.end());
if (unformatted.size() != 32)
{ {
throw ngraph_error("Input length must be 32"); throw ngraph_error("Input length must be 32");
} }
bitset<32> bs(s); bitset<32> bs(unformatted);
FloatUnion fu; FloatUnion fu;
fu.i = static_cast<uint32_t>(bs.to_ulong()); fu.i = static_cast<uint32_t>(bs.to_ulong());
return fu.f; return fu.f;
...@@ -73,11 +109,15 @@ float bits_to_float(const string& s) ...@@ -73,11 +109,15 @@ float bits_to_float(const string& s)
double bits_to_double(const string& s) double bits_to_double(const string& s)
{ {
if (s.size() != 64) string unformatted = s;
unformatted.erase(remove_if(unformatted.begin(), unformatted.end(), ::isspace),
unformatted.end());
if (unformatted.size() != 64)
{ {
throw ngraph_error("Input length must be 64"); throw ngraph_error("Input length must be 64");
} }
bitset<64> bs(s); bitset<64> bs(unformatted);
DoubleUnion du; DoubleUnion du;
du.i = static_cast<uint64_t>(bs.to_ullong()); du.i = static_cast<uint64_t>(bs.to_ullong());
return du.d; return du.d;
...@@ -333,8 +373,10 @@ INSTANTIATE_TEST_CASE_P( ...@@ -333,8 +373,10 @@ INSTANTIATE_TEST_CASE_P(
// Targeted bit // Targeted bit
// | // |
// v // v
// 2 3 4 5 6 (error allowed in 6th bit or later, w/ implicit leading bit)
// s e e e e e e e e m m m m m m m m m m m m m m m m m m m m m m m // s e e e e e e e e m m m m m m m m m m m m m m m m m m m m m m m
// =>| 8 | // =>| 8 | (8 w/ implicit leading bit)
// ^
// | 2 |<= // | 2 |<=
// //
// [Upper bound] // [Upper bound]
...@@ -363,26 +405,26 @@ INSTANTIATE_TEST_CASE_P( ...@@ -363,26 +405,26 @@ INSTANTIATE_TEST_CASE_P(
TEST(all_close_f, mantissa_8_near_0) TEST(all_close_f, mantissa_8_near_0)
{ {
// 0.f, the ground-truth value // 0.f, the ground-truth value
float expected = bits_to_float("00000000000000000000000000000000"); float expected = bits_to_float("0 00000000 000 0000 0000 0000 0000 0000");
float computed; float computed;
// ~3.67342E-40, the exact upper bound // ~3.67342E-40, the exact upper bound
computed = bits_to_float("00000000000001000000000000000000"); computed = bits_to_float("0 00000000 000 0100 0000 0000 0000 0000");
EXPECT_TRUE(test::close_f(expected, computed, 8, 2)); EXPECT_TRUE(test::close_f(expected, computed, 8, 2));
EXPECT_TRUE(test::all_close_f(vector<float>({expected}), vector<float>({computed}), 8, 2)); EXPECT_TRUE(test::all_close_f(vector<float>({expected}), vector<float>({computed}), 8, 2));
// ~3.67343E-40, the next representable number bigger than upper bound // ~3.67343E-40, the next representable number bigger than upper bound
computed = bits_to_float("00000000000001000000000000000001"); computed = bits_to_float("0 00000000 000 0100 0000 0000 0000 0001");
EXPECT_FALSE(test::close_f(expected, computed, 8, 2)); EXPECT_FALSE(test::close_f(expected, computed, 8, 2));
EXPECT_FALSE(test::all_close_f(vector<float>({expected}), vector<float>({computed}), 8, 2)); EXPECT_FALSE(test::all_close_f(vector<float>({expected}), vector<float>({computed}), 8, 2));
// ~-3.67342E-40, the exact lower bound // ~-3.67342E-40, the exact lower bound
computed = bits_to_float("10000000000001000000000000000000"); computed = bits_to_float("1 00000000 000 0100 0000 0000 0000 0000");
EXPECT_TRUE(test::close_f(expected, computed, 8, 2)); EXPECT_TRUE(test::close_f(expected, computed, 8, 2));
EXPECT_TRUE(test::all_close_f(vector<float>({expected}), vector<float>({computed}), 8, 2)); EXPECT_TRUE(test::all_close_f(vector<float>({expected}), vector<float>({computed}), 8, 2));
// ~-3.67343E-40, the next representable number smaller than lower bound // ~-3.67343E-40, the next representable number smaller than lower bound
computed = bits_to_float("10000000000001000000000000000001"); computed = bits_to_float("1 00000000 000 0100 0000 0000 0000 0001");
EXPECT_FALSE(test::close_f(expected, computed, 8, 2)); EXPECT_FALSE(test::close_f(expected, computed, 8, 2));
EXPECT_FALSE(test::all_close_f(vector<float>({expected}), vector<float>({computed}), 8, 2)); EXPECT_FALSE(test::all_close_f(vector<float>({expected}), vector<float>({computed}), 8, 2));
} }
...@@ -394,8 +436,10 @@ TEST(all_close_f, mantissa_8_near_0) ...@@ -394,8 +436,10 @@ TEST(all_close_f, mantissa_8_near_0)
// Targeted bit // Targeted bit
// | // |
// v // v
// 2 3 4 5 6 (error allowed in 6th bit or later, w/ implicit leading bit)
// s e e e e e e e e m m m m m m m m m m m m m m m m m m m m m m m // s e e e e e e e e m m m m m m m m m m m m m m m m m m m m m m m
// =>| 8 | // =>| 8 | (8 w/ implicit leading bit)
// ^
// | 2 |<= // | 2 |<=
// //
// [Upper bound] // [Upper bound]
...@@ -424,26 +468,26 @@ TEST(all_close_f, mantissa_8_near_0) ...@@ -424,26 +468,26 @@ TEST(all_close_f, mantissa_8_near_0)
TEST(all_close_f, mantissa_8_near_n0) TEST(all_close_f, mantissa_8_near_n0)
{ {
// 0.f, the ground-truth value // 0.f, the ground-truth value
float expected = bits_to_float("10000000000000000000000000000000"); float expected = bits_to_float("1 00000000 000 0000 0000 0000 0000 0000");
float computed; float computed;
// ~3.67342E-40, the exact upper bound // ~3.67342E-40, the exact upper bound
computed = bits_to_float("00000000000001000000000000000000"); computed = bits_to_float("0 00000000 000 0100 0000 0000 0000 0000");
EXPECT_TRUE(test::close_f(expected, computed, 8, 2)); EXPECT_TRUE(test::close_f(expected, computed, 8, 2));
EXPECT_TRUE(test::all_close_f(vector<float>({expected}), vector<float>({computed}), 8, 2)); EXPECT_TRUE(test::all_close_f(vector<float>({expected}), vector<float>({computed}), 8, 2));
// ~3.67343E-40, the next representable number bigger than upper bound // ~3.67343E-40, the next representable number bigger than upper bound
computed = bits_to_float("00000000000001000000000000000001"); computed = bits_to_float("0 00000000 000 0100 0000 0000 0000 0001");
EXPECT_FALSE(test::close_f(expected, computed, 8, 2)); EXPECT_FALSE(test::close_f(expected, computed, 8, 2));
EXPECT_FALSE(test::all_close_f(vector<float>({expected}), vector<float>({computed}), 8, 2)); EXPECT_FALSE(test::all_close_f(vector<float>({expected}), vector<float>({computed}), 8, 2));
// ~-3.67342E-40, the exact lower bound // ~-3.67342E-40, the exact lower bound
computed = bits_to_float("10000000000001000000000000000000"); computed = bits_to_float("1 00000000 000 0100 0000 0000 0000 0000");
EXPECT_TRUE(test::close_f(expected, computed, 8, 2)); EXPECT_TRUE(test::close_f(expected, computed, 8, 2));
EXPECT_TRUE(test::all_close_f(vector<float>({expected}), vector<float>({computed}), 8, 2)); EXPECT_TRUE(test::all_close_f(vector<float>({expected}), vector<float>({computed}), 8, 2));
// ~-3.67343E-40, the next representable number smaller than lower bound // ~-3.67343E-40, the next representable number smaller than lower bound
computed = bits_to_float("10000000000001000000000000000001"); computed = bits_to_float("1 00000000 000 0100 0000 0000 0000 0001");
EXPECT_FALSE(test::close_f(expected, computed, 8, 2)); EXPECT_FALSE(test::close_f(expected, computed, 8, 2));
EXPECT_FALSE(test::all_close_f(vector<float>({expected}), vector<float>({computed}), 8, 2)); EXPECT_FALSE(test::all_close_f(vector<float>({expected}), vector<float>({computed}), 8, 2));
} }
...@@ -455,8 +499,10 @@ TEST(all_close_f, mantissa_8_near_n0) ...@@ -455,8 +499,10 @@ TEST(all_close_f, mantissa_8_near_n0)
// Targeted bit // Targeted bit
// | // |
// v // v
// 2 3 4 5 6 (error allowed in 6th bit or later, w/ implicit leading bit)
// s e e e e e e e e m m m m m m m m m m m m m m m m m m m m m m m // s e e e e e e e e m m m m m m m m m m m m m m m m m m m m m m m
// =>| 8 | // =>| 8 | (8 w/ implicit leading bit)
// ^
// | 2 |<= // | 2 |<=
// //
// [Upper bound] // [Upper bound]
...@@ -479,26 +525,26 @@ TEST(all_close_f, mantissa_8_near_n0) ...@@ -479,26 +525,26 @@ TEST(all_close_f, mantissa_8_near_n0)
TEST(all_close_f, mantissa_8_near_1) TEST(all_close_f, mantissa_8_near_1)
{ {
// 1.f, the ground-truth value // 1.f, the ground-truth value
float expected = bits_to_float("00111111100000000000000000000000"); float expected = bits_to_float("0 01111111 000 0000 0000 0000 0000 0000");
float computed; float computed;
// 1.03125f, the exact upper bound // 1.03125f, the exact upper bound
computed = bits_to_float("00111111100001000000000000000000"); computed = bits_to_float("0 01111111 000 0100 0000 0000 0000 0000");
EXPECT_TRUE(test::close_f(expected, computed, 8, 2)); EXPECT_TRUE(test::close_f(expected, computed, 8, 2));
EXPECT_TRUE(test::all_close_f(vector<float>({expected}), vector<float>({computed}), 8, 2)); EXPECT_TRUE(test::all_close_f(vector<float>({expected}), vector<float>({computed}), 8, 2));
// 1.031250119f, the next representable number bigger than upper bound // 1.031250119f, the next representable number bigger than upper bound
computed = bits_to_float("00111111100001000000000000000001"); computed = bits_to_float("0 01111111 000 0100 0000 0000 0000 0001");
EXPECT_FALSE(test::close_f(expected, computed, 8, 2)); EXPECT_FALSE(test::close_f(expected, computed, 8, 2));
EXPECT_FALSE(test::all_close_f(vector<float>({expected}), vector<float>({computed}), 8, 2)); EXPECT_FALSE(test::all_close_f(vector<float>({expected}), vector<float>({computed}), 8, 2));
// 0.984375f, the exact lower bound // 0.984375f, the exact lower bound
computed = bits_to_float("00111111011111000000000000000000"); computed = bits_to_float("0 01111110 111 1100 0000 0000 0000 0000");
EXPECT_TRUE(test::close_f(expected, computed, 8, 2)); EXPECT_TRUE(test::close_f(expected, computed, 8, 2));
EXPECT_TRUE(test::all_close_f(vector<float>({expected}), vector<float>({computed}), 8, 2)); EXPECT_TRUE(test::all_close_f(vector<float>({expected}), vector<float>({computed}), 8, 2));
// 0.9843749404f, the next representable number smaller than lower bound // 0.9843749404f, the next representable number smaller than lower bound
computed = bits_to_float("00111111011110111111111111111111"); computed = bits_to_float("0 01111110 111 1011 1111 1111 1111 1111");
EXPECT_FALSE(test::close_f(expected, computed, 8, 2)); EXPECT_FALSE(test::close_f(expected, computed, 8, 2));
EXPECT_FALSE(test::all_close_f(vector<float>({expected}), vector<float>({computed}), 8, 2)); EXPECT_FALSE(test::all_close_f(vector<float>({expected}), vector<float>({computed}), 8, 2));
} }
...@@ -510,8 +556,10 @@ TEST(all_close_f, mantissa_8_near_1) ...@@ -510,8 +556,10 @@ TEST(all_close_f, mantissa_8_near_1)
// Targeted bit // Targeted bit
// | // |
// v // v
// 2 3 4 5 6 (error allowed in 6th bit or later, w/ implicit leading bit)
// s e e e e e e e e m m m m m m m m m m m m m m m m m m m m m m m // s e e e e e e e e m m m m m m m m m m m m m m m m m m m m m m m
// =>| 8 | // =>| 8 | (8 w/ implicit leading bit)
// ^
// | 2 |<= // | 2 |<=
// //
// [Upper bound] // [Upper bound]
...@@ -534,26 +582,26 @@ TEST(all_close_f, mantissa_8_near_1) ...@@ -534,26 +582,26 @@ TEST(all_close_f, mantissa_8_near_1)
TEST(all_close_f, mantissa_8_near_n1) TEST(all_close_f, mantissa_8_near_n1)
{ {
// -1.f, the ground-truth value // -1.f, the ground-truth value
float expected = bits_to_float("10111111100000000000000000000000"); float expected = bits_to_float("1 01111111 000 0000 0000 0000 0000 0000");
float computed; float computed;
// -0.984375f, the exact upper bound // -0.984375f, the exact upper bound
computed = bits_to_float("10111111011111000000000000000000"); computed = bits_to_float("1 01111110 111 1100 0000 0000 0000 0000");
EXPECT_TRUE(test::close_f(expected, computed, 8, 2)); EXPECT_TRUE(test::close_f(expected, computed, 8, 2));
EXPECT_TRUE(test::all_close_f(vector<float>({expected}), vector<float>({computed}), 8, 2)); EXPECT_TRUE(test::all_close_f(vector<float>({expected}), vector<float>({computed}), 8, 2));
// -0.984374940395355224609375f, the next representable number bigger than upper bound // -0.984374940395355224609375f, the next representable number bigger than upper bound
computed = bits_to_float("10111111011110111111111111111111"); computed = bits_to_float("1 01111110 111 1011 1111 1111 1111 1111");
EXPECT_FALSE(test::close_f(expected, computed, 8, 2)); EXPECT_FALSE(test::close_f(expected, computed, 8, 2));
EXPECT_FALSE(test::all_close_f(vector<float>({expected}), vector<float>({computed}), 8, 2)); EXPECT_FALSE(test::all_close_f(vector<float>({expected}), vector<float>({computed}), 8, 2));
// -1.03125f, the exact lower bound // -1.03125f, the exact lower bound
computed = bits_to_float("10111111100001000000000000000000"); computed = bits_to_float("1 01111111 000 0100 0000 0000 0000 0000");
EXPECT_TRUE(test::close_f(expected, computed, 8, 2)); EXPECT_TRUE(test::close_f(expected, computed, 8, 2));
EXPECT_TRUE(test::all_close_f(vector<float>({expected}), vector<float>({computed}), 8, 2)); EXPECT_TRUE(test::all_close_f(vector<float>({expected}), vector<float>({computed}), 8, 2));
// -1.03125011920928955078125f, the next representable number smaller than lower bound // -1.03125011920928955078125f, the next representable number smaller than lower bound
computed = bits_to_float("10111111100001000000000000000001"); computed = bits_to_float("1 01111111 000 0100 0000 0000 0000 0001");
EXPECT_FALSE(test::close_f(expected, computed, 8, 2)); EXPECT_FALSE(test::close_f(expected, computed, 8, 2));
EXPECT_FALSE(test::all_close_f(vector<float>({expected}), vector<float>({computed}), 8, 2)); EXPECT_FALSE(test::all_close_f(vector<float>({expected}), vector<float>({computed}), 8, 2));
} }
...@@ -564,8 +612,10 @@ TEST(all_close_f, mantissa_8_near_n1) ...@@ -564,8 +612,10 @@ TEST(all_close_f, mantissa_8_near_n1)
// Targeted bit // Targeted bit
// | // |
// v // v
// 2 3 4 5 6 (error allowed in 6th bit or later, w/ implicit leading bit)
// s e e e e e e e e m m m m m m m m m m m m m m m m m m m m m m m // s e e e e e e e e m m m m m m m m m m m m m m m m m m m m m m m
// =>| 8 | // =>| 8 | (8 w/ implicit leading bit)
// ^
// | 2 |<= // | 2 |<=
TEST(all_close_f, mantissa_8_near_0_1_10_100_1000) TEST(all_close_f, mantissa_8_near_0_1_10_100_1000)
{ {
...@@ -576,11 +626,13 @@ TEST(all_close_f, mantissa_8_near_0_1_10_100_1000) ...@@ -576,11 +626,13 @@ TEST(all_close_f, mantissa_8_near_0_1_10_100_1000)
float smaller_than_lower_bound; float smaller_than_lower_bound;
// Bounds around 0: 0 +- 3.67e-40 // Bounds around 0: 0 +- 3.67e-40
expected = 0.f; // 00000000000000000000000000000000 expected = 0.f; // 0 00000000 000 0000 0000 0000 0000 0000
upper_bound = 3.67342e-40f; // 00000000000001000000000000000000, approximated upper_bound = 3.67342e-40f; // 0 00000000 000 0100 0000 0000 0000 0000, approximated
bigger_than_upper_bound = 3.67343e-40f; // 00000000000001000000000000000001, approximated bigger_than_upper_bound =
lower_bound = -3.67342e-40f; // 10000000000001000000000000000000, approximated 3.67343e-40f; // 0 00000000 000 0100 0000 0000 0000 0001, approximated
smaller_than_lower_bound = 3.67343e-40f; // 10000000000001000000000000000001, approximated lower_bound = -3.67342e-40f; // 1 00000000 000 0100 0000 0000 0000 0000, approximated
smaller_than_lower_bound =
3.67343e-40f; // 1 00000000 000 0100 0000 0000 0000 0001, approximated
EXPECT_TRUE(test::close_f(expected, upper_bound, 8, 2)); EXPECT_TRUE(test::close_f(expected, upper_bound, 8, 2));
EXPECT_TRUE(test::all_close_f(vector<float>({expected}), vector<float>({upper_bound}), 8, 2)); EXPECT_TRUE(test::all_close_f(vector<float>({expected}), vector<float>({upper_bound}), 8, 2));
EXPECT_FALSE(test::close_f(expected, bigger_than_upper_bound, 8, 2)); EXPECT_FALSE(test::close_f(expected, bigger_than_upper_bound, 8, 2));
...@@ -593,11 +645,11 @@ TEST(all_close_f, mantissa_8_near_0_1_10_100_1000) ...@@ -593,11 +645,11 @@ TEST(all_close_f, mantissa_8_near_0_1_10_100_1000)
vector<float>({expected}), vector<float>({smaller_than_lower_bound}), 8, 2)); vector<float>({expected}), vector<float>({smaller_than_lower_bound}), 8, 2));
// Bounds around 1: 1 +- 0.03 // Bounds around 1: 1 +- 0.03
expected = 1.f; // 00111111100000000000000000000000 expected = 1.f; // 0 01111111 000 0000 0000 0000 0000 0000
upper_bound = 1.03125f; // 00111111100001000000000000000000 upper_bound = 1.03125f; // 0 01111111 000 0100 0000 0000 0000 0000
bigger_than_upper_bound = 1.031250119f; // 00111111100001000000000000000001 bigger_than_upper_bound = 1.031250119f; // 0 01111111 000 0100 0000 0000 0000 0001
lower_bound = 0.984375f; // 00111111011111000000000000000000 lower_bound = 0.984375f; // 0 01111110 111 1100 0000 0000 0000 0000
smaller_than_lower_bound = 0.9843749404f; // 00111111011110111111111111111111 smaller_than_lower_bound = 0.9843749404f; // 0 01111110 111 1011 1111 1111 1111 1111
EXPECT_TRUE(test::close_f(expected, upper_bound, 8, 2)); EXPECT_TRUE(test::close_f(expected, upper_bound, 8, 2));
EXPECT_TRUE(test::all_close_f(vector<float>({expected}), vector<float>({upper_bound}), 8, 2)); EXPECT_TRUE(test::all_close_f(vector<float>({expected}), vector<float>({upper_bound}), 8, 2));
EXPECT_FALSE(test::close_f(expected, bigger_than_upper_bound, 8, 2)); EXPECT_FALSE(test::close_f(expected, bigger_than_upper_bound, 8, 2));
...@@ -610,11 +662,11 @@ TEST(all_close_f, mantissa_8_near_0_1_10_100_1000) ...@@ -610,11 +662,11 @@ TEST(all_close_f, mantissa_8_near_0_1_10_100_1000)
vector<float>({expected}), vector<float>({smaller_than_lower_bound}), 8, 2)); vector<float>({expected}), vector<float>({smaller_than_lower_bound}), 8, 2));
// Bounds around 10: 10 +- 0.25 // Bounds around 10: 10 +- 0.25
expected = 10.f; // 01000001001000000000000000000000 expected = 10.f; // 0 10000010 010 0000 0000 0000 0000 0000
upper_bound = 10.25f; // 01000001001001000000000000000000 upper_bound = 10.25f; // 0 10000010 010 0100 0000 0000 0000 0000
bigger_than_upper_bound = 10.25000095367431640625f; // 01000001001001000000000000000001 bigger_than_upper_bound = 10.25000095367431640625f; // 0 10000010 010 0100 0000 0000 0000 0001
lower_bound = 9.75f; // 01000001000111000000000000000000 lower_bound = 9.75f; // 0 10000010 001 1100 0000 0000 0000 0000
smaller_than_lower_bound = 9.74999904632568359375f; // 01000001000110111111111111111111 smaller_than_lower_bound = 9.74999904632568359375f; // 0 10000010 001 1011 1111 1111 1111 1111
EXPECT_TRUE(test::close_f(expected, upper_bound, 8, 2)); EXPECT_TRUE(test::close_f(expected, upper_bound, 8, 2));
EXPECT_TRUE(test::all_close_f(vector<float>({expected}), vector<float>({upper_bound}), 8, 2)); EXPECT_TRUE(test::all_close_f(vector<float>({expected}), vector<float>({upper_bound}), 8, 2));
EXPECT_FALSE(test::close_f(expected, bigger_than_upper_bound, 8, 2)); EXPECT_FALSE(test::close_f(expected, bigger_than_upper_bound, 8, 2));
...@@ -627,11 +679,11 @@ TEST(all_close_f, mantissa_8_near_0_1_10_100_1000) ...@@ -627,11 +679,11 @@ TEST(all_close_f, mantissa_8_near_0_1_10_100_1000)
vector<float>({expected}), vector<float>({smaller_than_lower_bound}), 8, 2)); vector<float>({expected}), vector<float>({smaller_than_lower_bound}), 8, 2));
// Bounds around 100: 100 +- 2 // Bounds around 100: 100 +- 2
expected = 100.f; // 01000010110010000000000000000000 expected = 100.f; // 0 10000101 100 1000 0000 0000 0000 0000
upper_bound = 102.f; // 01000010110011000000000000000000 upper_bound = 102.f; // 0 10000101 100 1100 0000 0000 0000 0000
bigger_than_upper_bound = 102.00000762939453125f; // 01000010110011000000000000000001 bigger_than_upper_bound = 102.00000762939453125f; // 0 10000101 100 1100 0000 0000 0000 0001
lower_bound = 98.0f; // 01000010110001000000000000000000 lower_bound = 98.0f; // 0 10000101 100 0100 0000 0000 0000 0000
smaller_than_lower_bound = 97.99999237060546875f; // 01000010110000111111111111111111 smaller_than_lower_bound = 97.99999237060546875f; // 0 10000101 100 0011 1111 1111 1111 1111
EXPECT_TRUE(test::close_f(expected, upper_bound, 8, 2)); EXPECT_TRUE(test::close_f(expected, upper_bound, 8, 2));
EXPECT_TRUE(test::all_close_f(vector<float>({expected}), vector<float>({upper_bound}), 8, 2)); EXPECT_TRUE(test::all_close_f(vector<float>({expected}), vector<float>({upper_bound}), 8, 2));
EXPECT_FALSE(test::close_f(expected, bigger_than_upper_bound, 8, 2)); EXPECT_FALSE(test::close_f(expected, bigger_than_upper_bound, 8, 2));
...@@ -644,11 +696,11 @@ TEST(all_close_f, mantissa_8_near_0_1_10_100_1000) ...@@ -644,11 +696,11 @@ TEST(all_close_f, mantissa_8_near_0_1_10_100_1000)
vector<float>({expected}), vector<float>({smaller_than_lower_bound}), 8, 2)); vector<float>({expected}), vector<float>({smaller_than_lower_bound}), 8, 2));
// Bounds around 1000: 1000 +- 16 // Bounds around 1000: 1000 +- 16
expected = 1000.f; // 01000100011110100000000000000000 expected = 1000.f; // 0 10001000 111 1010 0000 0000 0000 0000
upper_bound = 1016.f; // 01000100011111100000000000000000 upper_bound = 1016.f; // 0 10001000 111 1110 0000 0000 0000 0000
bigger_than_upper_bound = 1016.00006103515625f; // 01000100011111100000000000000001 bigger_than_upper_bound = 1016.00006103515625f; // 0 10001000 111 1110 0000 0000 0000 0001
lower_bound = 984.0f; // 01000100011101100000000000000000 lower_bound = 984.0f; // 0 10001000 111 0110 0000 0000 0000 0000
smaller_than_lower_bound = 983.99993896484375f; // 01000100011101011111111111111111 smaller_than_lower_bound = 983.99993896484375f; // 0 10001000 111 0101 1111 1111 1111 1111
EXPECT_TRUE(test::close_f(expected, upper_bound, 8, 2)); EXPECT_TRUE(test::close_f(expected, upper_bound, 8, 2));
EXPECT_TRUE(test::all_close_f(vector<float>({expected}), vector<float>({upper_bound}), 8, 2)); EXPECT_TRUE(test::all_close_f(vector<float>({expected}), vector<float>({upper_bound}), 8, 2));
EXPECT_FALSE(test::close_f(expected, bigger_than_upper_bound, 8, 2)); EXPECT_FALSE(test::close_f(expected, bigger_than_upper_bound, 8, 2));
...@@ -666,9 +718,11 @@ TEST(all_close_f, mantissa_8_near_0_1_10_100_1000) ...@@ -666,9 +718,11 @@ TEST(all_close_f, mantissa_8_near_0_1_10_100_1000)
// //
// Targeted bit // Targeted bit
// | // |
// v // (22 bits must match, w/ implicit leading bit) v
// 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2
// s e e e e e e e e m m m m m m m m m m m m m m m m m m m m m m m // s e e e e e e e e m m m m m m m m m m m m m m m m m m m m m m m
// =>| 24 | // =>| 24 (w/ implicit leading bit) |
// ^
// | 2 |<= // | 2 |<=
TEST(all_close_f, mantissa_24_near_0_1_10_100_1000) TEST(all_close_f, mantissa_24_near_0_1_10_100_1000)
{ {
...@@ -680,10 +734,10 @@ TEST(all_close_f, mantissa_24_near_0_1_10_100_1000) ...@@ -680,10 +734,10 @@ TEST(all_close_f, mantissa_24_near_0_1_10_100_1000)
// Bounds around 0: 0 +- 5.6e-45 // Bounds around 0: 0 +- 5.6e-45
expected = 0.f; expected = 0.f;
upper_bound = bits_to_float("00000000000000000000000000000100"); upper_bound = bits_to_float("0 00000000 000 0000 0000 0000 0000 0100");
bigger_than_upper_bound = bits_to_float("00000000000000000000000000000101"); bigger_than_upper_bound = bits_to_float("0 00000000 000 0000 0000 0000 0000 0101");
lower_bound = bits_to_float("10000000000000000000000000000100"); lower_bound = bits_to_float("1 00000000 000 0000 0000 0000 0000 0100");
smaller_than_lower_bound = bits_to_float("10000000000000000000000000000101"); smaller_than_lower_bound = bits_to_float("1 00000000 000 0000 0000 0000 0000 0101");
EXPECT_TRUE(test::close_f(expected, upper_bound, 24, 2)); EXPECT_TRUE(test::close_f(expected, upper_bound, 24, 2));
EXPECT_TRUE(test::all_close_f(vector<float>({expected}), vector<float>({upper_bound}), 24, 2)); EXPECT_TRUE(test::all_close_f(vector<float>({expected}), vector<float>({upper_bound}), 24, 2));
EXPECT_FALSE(test::close_f(expected, bigger_than_upper_bound, 24, 2)); EXPECT_FALSE(test::close_f(expected, bigger_than_upper_bound, 24, 2));
...@@ -697,10 +751,10 @@ TEST(all_close_f, mantissa_24_near_0_1_10_100_1000) ...@@ -697,10 +751,10 @@ TEST(all_close_f, mantissa_24_near_0_1_10_100_1000)
// Bounds around 1: 1 +- 4.77e-7 // Bounds around 1: 1 +- 4.77e-7
expected = 1.f; expected = 1.f;
upper_bound = bits_to_float("00111111100000000000000000000100"); upper_bound = bits_to_float("0 01111111 000 0000 0000 0000 0000 0100");
bigger_than_upper_bound = bits_to_float("00111111100000000000000000000101"); bigger_than_upper_bound = bits_to_float("0 01111111 000 0000 0000 0000 0000 0101");
lower_bound = bits_to_float("00111111011111111111111111111100"); lower_bound = bits_to_float("0 01111110 111 1111 1111 1111 1111 1100");
smaller_than_lower_bound = bits_to_float("00111111011111111111111111111011"); smaller_than_lower_bound = bits_to_float("0 01111110 111 1111 1111 1111 1111 1011");
EXPECT_TRUE(test::close_f(expected, upper_bound, 24, 2)); EXPECT_TRUE(test::close_f(expected, upper_bound, 24, 2));
EXPECT_TRUE(test::all_close_f(vector<float>({expected}), vector<float>({upper_bound}), 24, 2)); EXPECT_TRUE(test::all_close_f(vector<float>({expected}), vector<float>({upper_bound}), 24, 2));
EXPECT_FALSE(test::close_f(expected, bigger_than_upper_bound, 24, 2)); EXPECT_FALSE(test::close_f(expected, bigger_than_upper_bound, 24, 2));
...@@ -714,10 +768,10 @@ TEST(all_close_f, mantissa_24_near_0_1_10_100_1000) ...@@ -714,10 +768,10 @@ TEST(all_close_f, mantissa_24_near_0_1_10_100_1000)
// Bounds around 10: 10 +- 3.81e-6 // Bounds around 10: 10 +- 3.81e-6
expected = 10.f; expected = 10.f;
upper_bound = bits_to_float("01000001001000000000000000000100"); upper_bound = bits_to_float("0 10000010 010 0000 0000 0000 0000 0100");
bigger_than_upper_bound = bits_to_float("01000001001000000000000000000101"); bigger_than_upper_bound = bits_to_float("0 10000010 010 0000 0000 0000 0000 0101");
lower_bound = bits_to_float("01000001000111111111111111111100"); lower_bound = bits_to_float("0 10000010 001 1111 1111 1111 1111 1100");
smaller_than_lower_bound = bits_to_float("01000001000111111111111111111011"); smaller_than_lower_bound = bits_to_float("0 10000010 001 1111 1111 1111 1111 1011");
EXPECT_TRUE(test::close_f(expected, upper_bound, 24, 2)); EXPECT_TRUE(test::close_f(expected, upper_bound, 24, 2));
EXPECT_TRUE(test::all_close_f(vector<float>({expected}), vector<float>({upper_bound}), 24, 2)); EXPECT_TRUE(test::all_close_f(vector<float>({expected}), vector<float>({upper_bound}), 24, 2));
EXPECT_FALSE(test::close_f(expected, bigger_than_upper_bound, 24, 2)); EXPECT_FALSE(test::close_f(expected, bigger_than_upper_bound, 24, 2));
...@@ -731,10 +785,10 @@ TEST(all_close_f, mantissa_24_near_0_1_10_100_1000) ...@@ -731,10 +785,10 @@ TEST(all_close_f, mantissa_24_near_0_1_10_100_1000)
// Bounds around 100: 100 +- 3.05e-5 // Bounds around 100: 100 +- 3.05e-5
expected = 100.f; expected = 100.f;
upper_bound = bits_to_float("01000010110010000000000000000100"); upper_bound = bits_to_float("0 10000101 100 1000 0000 0000 0000 0100");
bigger_than_upper_bound = bits_to_float("01000010110010000000000000000101"); bigger_than_upper_bound = bits_to_float("0 10000101 100 1000 0000 0000 0000 0101");
lower_bound = bits_to_float("01000010110001111111111111111100"); lower_bound = bits_to_float("0 10000101 100 0111 1111 1111 1111 1100");
smaller_than_lower_bound = bits_to_float("01000010110001111111111111111011"); smaller_than_lower_bound = bits_to_float("0 10000101 100 0111 1111 1111 1111 1011");
EXPECT_TRUE(test::close_f(expected, upper_bound, 24, 2)); EXPECT_TRUE(test::close_f(expected, upper_bound, 24, 2));
EXPECT_TRUE(test::all_close_f(vector<float>({expected}), vector<float>({upper_bound}), 24, 2)); EXPECT_TRUE(test::all_close_f(vector<float>({expected}), vector<float>({upper_bound}), 24, 2));
EXPECT_FALSE(test::close_f(expected, bigger_than_upper_bound, 24, 2)); EXPECT_FALSE(test::close_f(expected, bigger_than_upper_bound, 24, 2));
...@@ -748,10 +802,10 @@ TEST(all_close_f, mantissa_24_near_0_1_10_100_1000) ...@@ -748,10 +802,10 @@ TEST(all_close_f, mantissa_24_near_0_1_10_100_1000)
// Bounds around 1000: 1000 +- 2.44e-4 // Bounds around 1000: 1000 +- 2.44e-4
expected = 1000.f; expected = 1000.f;
upper_bound = bits_to_float("01000100011110100000000000000100"); upper_bound = bits_to_float("0 10001000 111 1010 0000 0000 0000 0100");
bigger_than_upper_bound = bits_to_float("01000100011110100000000000000101"); bigger_than_upper_bound = bits_to_float("0 10001000 111 1010 0000 0000 0000 0101");
lower_bound = bits_to_float("01000100011110011111111111111100"); lower_bound = bits_to_float("0 10001000 111 1001 1111 1111 1111 1100");
smaller_than_lower_bound = bits_to_float("01000100011110011111111111111011"); smaller_than_lower_bound = bits_to_float("0 10001000 111 1001 1111 1111 1111 1011");
EXPECT_TRUE(test::close_f(expected, upper_bound, 24, 2)); EXPECT_TRUE(test::close_f(expected, upper_bound, 24, 2));
EXPECT_TRUE(test::all_close_f(vector<float>({expected}), vector<float>({upper_bound}), 24, 2)); EXPECT_TRUE(test::all_close_f(vector<float>({expected}), vector<float>({upper_bound}), 24, 2));
EXPECT_FALSE(test::close_f(expected, bigger_than_upper_bound, 24, 2)); EXPECT_FALSE(test::close_f(expected, bigger_than_upper_bound, 24, 2));
......
...@@ -295,17 +295,19 @@ uint32_t test::matching_mantissa_bits(uint64_t distance) ...@@ -295,17 +295,19 @@ uint32_t test::matching_mantissa_bits(uint64_t distance)
<< " mantissa bits.\n"; << " mantissa bits.\n";
} }
msg << "passing criteria: " << (mantissa_bits - tolerance_bits) << " mantissa bits (" msg << "passing criteria - mismatch allowed @ mantissa bit: "
<< mantissa_bits << " mantissa bits w/ " << tolerance_bits << " tolerance bits)\n"; << (mantissa_bits - tolerance_bits) << " or later (" << mantissa_bits
<< " mantissa bits w/ " << tolerance_bits << " tolerance bits)\n";
msg << std::setprecision(std::numeric_limits<long double>::digits10 + 1) msg << std::setprecision(std::numeric_limits<long double>::digits10 + 1)
<< "tightest match: " << matching_mantissa_bits(min_distance) << " mantissa bits (" << "tightest match - mismatch occurred @ mantissa bit: "
<< a[min_distance_index] << " vs " << b[min_distance_index] << " at [" << min_distance_index << matching_mantissa_bits(min_distance) << " or next bit (" << a[min_distance_index]
<< "])\n"; << " vs " << b[min_distance_index] << " at [" << min_distance_index << "])\n";
msg << std::setprecision(std::numeric_limits<long double>::digits10 + 1) msg << std::setprecision(std::numeric_limits<long double>::digits10 + 1)
<< "loosest match: " << matching_mantissa_bits(max_distance) << " mantissa bits (" << "loosest match - mismatch occurred @ mantissa bit: "
<< a[max_distance_index] << " vs " << b[max_distance_index] << " at [" << max_distance_index << matching_mantissa_bits(max_distance) << " or next bit (" << a[max_distance_index]
<< "])\n"; << " vs " << b[max_distance_index] << " at [" << max_distance_index << "])\n";
msg << "median match: " << matching_mantissa_bits(median_distance) << " mantissa bits\n"; msg << "median match - mismatch occurred @ mantissa bit: "
<< matching_mantissa_bits(median_distance) << " or next bit\n";
::testing::AssertionResult res = ::testing::AssertionResult res =
rc ? ::testing::AssertionSuccess() : ::testing::AssertionFailure(); rc ? ::testing::AssertionSuccess() : ::testing::AssertionFailure();
...@@ -383,17 +385,19 @@ uint32_t test::matching_mantissa_bits(uint64_t distance) ...@@ -383,17 +385,19 @@ uint32_t test::matching_mantissa_bits(uint64_t distance)
<< " mantissa bits.\n"; << " mantissa bits.\n";
} }
msg << "passing criteria: " << (mantissa_bits - tolerance_bits) << " mantissa bits (" msg << "passing criteria - mismatch allowed @ mantissa bit: "
<< mantissa_bits << " mantissa bits w/ " << tolerance_bits << " tolerance bits)\n"; << (mantissa_bits - tolerance_bits) << " or later (" << mantissa_bits
<< " mantissa bits w/ " << tolerance_bits << " tolerance bits)\n";
msg << std::setprecision(std::numeric_limits<long double>::digits10 + 1) msg << std::setprecision(std::numeric_limits<long double>::digits10 + 1)
<< "tightest match: " << matching_mantissa_bits(min_distance) << " mantissa bits (" << "tightest match - mismatch occurred @ mantissa bit: "
<< a[min_distance_index] << " vs " << b[min_distance_index] << " at [" << min_distance_index << matching_mantissa_bits(min_distance) << " or next bit (" << a[min_distance_index]
<< "])\n"; << " vs " << b[min_distance_index] << " at [" << min_distance_index << "])\n";
msg << std::setprecision(std::numeric_limits<long double>::digits10 + 1) msg << std::setprecision(std::numeric_limits<long double>::digits10 + 1)
<< "loosest match: " << matching_mantissa_bits(max_distance) << " mantissa bits (" << "loosest match - mismatch occurred @ mantissa bit: "
<< a[max_distance_index] << " vs " << b[max_distance_index] << " at [" << max_distance_index << matching_mantissa_bits(max_distance) << " or next bit (" << a[max_distance_index]
<< "])\n"; << " vs " << b[max_distance_index] << " at [" << max_distance_index << "])\n";
msg << "median match: " << matching_mantissa_bits(median_distance) << " mantissa bits\n"; msg << "median match - mismatch occurred @ mantissa bit: "
<< matching_mantissa_bits(median_distance) << " or next bit\n";
::testing::AssertionResult res = ::testing::AssertionResult res =
rc ? ::testing::AssertionSuccess() : ::testing::AssertionFailure(); rc ? ::testing::AssertionSuccess() : ::testing::AssertionFailure();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment