#include #include using namespace DB; GTEST_TEST(AccurateComparison, Tests) { /// Arbitrary assortion of cases. ASSERT_TRUE(accurate::equalsOp(static_cast(123), static_cast(123))); ASSERT_TRUE(accurate::lessOp(static_cast(123), static_cast(124))); ASSERT_TRUE(accurate::lessOp(static_cast(-1), static_cast(1))); ASSERT_TRUE(accurate::lessOp(static_cast(-1), static_cast(1))); ASSERT_TRUE(!accurate::equalsOp(static_cast(-1), static_cast(-1))); ASSERT_TRUE(accurate::equalsOp(-0., 0)); ASSERT_TRUE(accurate::lessOp(-0., 1)); ASSERT_TRUE(accurate::lessOp(-0.5, 1)); ASSERT_TRUE(accurate::lessOp(0.5, 1)); ASSERT_TRUE(accurate::equalsOp(1.0, 1)); ASSERT_TRUE(accurate::greaterOp(1.1, 1)); ASSERT_TRUE(accurate::greaterOp(11.1, 1)); ASSERT_TRUE(accurate::greaterOp(11.1, 11)); ASSERT_TRUE(accurate::lessOp(-11.1, 11)); ASSERT_TRUE(accurate::lessOp(-11.1, -11)); ASSERT_TRUE(accurate::lessOp(-1.1, -1)); ASSERT_TRUE(accurate::greaterOp(-1.1, -2)); ASSERT_TRUE(accurate::greaterOp(1000., 100)); ASSERT_TRUE(accurate::greaterOp(-100., -1000)); ASSERT_TRUE(accurate::lessOp(100., 1000)); ASSERT_TRUE(accurate::lessOp(-1000., -100)); ASSERT_TRUE(accurate::lessOp(-std::numeric_limits::infinity(), 0)); ASSERT_TRUE(accurate::lessOp(-std::numeric_limits::infinity(), 1000)); ASSERT_TRUE(accurate::lessOp(-std::numeric_limits::infinity(), -1000)); ASSERT_TRUE(accurate::greaterOp(std::numeric_limits::infinity(), 0)); ASSERT_TRUE(accurate::greaterOp(std::numeric_limits::infinity(), 1000)); ASSERT_TRUE(accurate::greaterOp(std::numeric_limits::infinity(), -1000)); ASSERT_TRUE(accurate::lessOp(1, 1e100)); ASSERT_TRUE(accurate::lessOp(-1, 1e100)); ASSERT_TRUE(accurate::lessOp(-1e100, 1)); ASSERT_TRUE(accurate::lessOp(-1e100, -1)); /// Tricky cases with floats. ASSERT_TRUE(accurate::equalsOp(static_cast(9223372036854775808ULL), static_cast(9223372036854775808ULL))); ASSERT_TRUE(accurate::equalsOp(static_cast(9223372036854775808ULL), static_cast(9223372036854775808ULL))); ASSERT_TRUE(accurate::greaterOp(static_cast(9223372036854776000ULL), static_cast(9223372036854776000ULL))); ASSERT_TRUE(accurate::lessOp(static_cast(9223372000000000000ULL), static_cast(9223372000000000000ULL))); ASSERT_TRUE(accurate::equalsOp(static_cast(9223372036854775808ULL), static_cast(9223372036854775808ULL))); /* Float32 f = static_cast(9223372000000000000ULL); UInt64 u = static_cast(9223372000000000000ULL); DecomposedFloat32 components(f); std::cerr << std::fixed << std::setprecision(3) << f << ", " << components.normalized_exponent() << ", " << components.mantissa() << ", " << (components.mantissa() << (components.normalized_exponent() - 23)) << ", " << (1ULL << components.normalized_exponent()) << ", " << (components.normalized_exponent() >= static_cast(8 * sizeof(UInt64) - is_signed_v)) << ": " << components.compare(u) << "\n";*/ }