| 2485 | |
| 2486 | template <typename TYPE> |
| 2487 | void CheckFloatApproxEqualsWithUlpDistance() { |
| 2488 | using CType = |
| 2489 | std::conditional_t<is_half_float_type<TYPE>::value, Float16, typename TYPE::c_type>; |
| 2490 | auto type = TypeTraits<TYPE>::type_singleton(); |
| 2491 | std::vector<CType> a, b; |
| 2492 | a = {CType(NAN), CType(+0.0), CType(INFINITY)}; |
| 2493 | b = {CType(NAN), CType(-0.0), CType(INFINITY)}; |
| 2494 | if constexpr (is_half_float_type<TYPE>::value) { |
| 2495 | a.push_back(Float16(1.00097656)); |
| 2496 | b.push_back(Float16(0.999511719f)); |
| 2497 | } else if constexpr (std::is_same_v<TYPE, DoubleType>) { |
| 2498 | a.push_back(CType(0.9999999999999999)); |
| 2499 | b.push_back(CType(1.0000000000000002)); |
| 2500 | } else if constexpr (std::is_same_v<TYPE, FloatType>) { |
| 2501 | a.push_back(CType(1.0000001f)); |
| 2502 | b.push_back(CType(0.99999994f)); |
| 2503 | } |
| 2504 | |
| 2505 | std::shared_ptr<Array> array_a, array_b; |
| 2506 | ArrayFromVector<TYPE>(type, a, &array_a); |
| 2507 | ArrayFromVector<TYPE>(type, b, &array_b); |
| 2508 | auto options = EqualOptions::Defaults().ulp_distance(2); |
| 2509 | |
| 2510 | // Check with NaN |
| 2511 | ASSERT_FALSE(array_a->Equals(array_b, options)); |
| 2512 | ASSERT_TRUE(array_a->Equals(array_b, options.nans_equal(true))); |
| 2513 | |
| 2514 | // Check With Signed Zero |
| 2515 | ASSERT_FALSE( |
| 2516 | array_a->Equals(array_b, options.nans_equal(true).signed_zeros_equal(false))); |
| 2517 | |
| 2518 | // Check with Ulp Distance |
| 2519 | ASSERT_FALSE(array_a->Equals(array_b, options.nans_equal(true).ulp_distance(1))); |
| 2520 | } |
| 2521 | |
| 2522 | TEST(TestPrimitiveAdHoc, FloatingApproxEquals) { |
| 2523 | CheckApproxEquals<FloatType>(); |
nothing calls this directly
no test coverage detected