MCPcopy Create free account
hub / github.com/apache/arrow / CheckFloatApproxEqualsWithUlpDistance

Function CheckFloatApproxEqualsWithUlpDistance

cpp/src/arrow/array/array_test.cc:2425–2458  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2423
2424template <typename TYPE>
2425void CheckFloatApproxEqualsWithUlpDistance() {
2426 using CType =
2427 std::conditional_t<is_half_float_type<TYPE>::value, Float16, typename TYPE::c_type>;
2428 auto type = TypeTraits<TYPE>::type_singleton();
2429 std::vector<CType> a, b;
2430 a = {CType(NAN), CType(+0.0), CType(INFINITY)};
2431 b = {CType(NAN), CType(-0.0), CType(INFINITY)};
2432 if constexpr (is_half_float_type<TYPE>::value) {
2433 a.push_back(Float16(1.00097656));
2434 b.push_back(Float16(0.999511719f));
2435 } else if constexpr (std::is_same_v<TYPE, DoubleType>) {
2436 a.push_back(CType(0.9999999999999999));
2437 b.push_back(CType(1.0000000000000002));
2438 } else if constexpr (std::is_same_v<TYPE, FloatType>) {
2439 a.push_back(CType(1.0000001f));
2440 b.push_back(CType(0.99999994f));
2441 }
2442
2443 std::shared_ptr<Array> array_a, array_b;
2444 ArrayFromVector<TYPE>(type, a, &array_a);
2445 ArrayFromVector<TYPE>(type, b, &array_b);
2446 auto options = EqualOptions::Defaults().ulp_distance(2);
2447
2448 // Check with NaN
2449 ASSERT_FALSE(array_a->Equals(array_b, options));
2450 ASSERT_TRUE(array_a->Equals(array_b, options.nans_equal(true)));
2451
2452 // Check With Signed Zero
2453 ASSERT_FALSE(
2454 array_a->Equals(array_b, options.nans_equal(true).signed_zeros_equal(false)));
2455
2456 // Check with Ulp Distance
2457 ASSERT_FALSE(array_a->Equals(array_b, options.nans_equal(true).ulp_distance(1)));
2458}
2459
2460TEST(TestPrimitiveAdHoc, FloatingApproxEquals) {
2461 CheckApproxEquals<FloatType>();

Callers

nothing calls this directly

Calls 5

push_backMethod · 0.80
type_singletonFunction · 0.50
Float16Function · 0.50
DefaultsFunction · 0.50
EqualsMethod · 0.45

Tested by

no test coverage detected