| 49 | // Check if difference in values is within tolerance range |
| 50 | template <typename U> |
| 51 | bool equal_values_relative(const U target, const U reference, const float tolerance) |
| 52 | { |
| 53 | if (are_equal_infs(target, reference)) |
| 54 | { |
| 55 | return true; |
| 56 | } |
| 57 | else if (target == reference) |
| 58 | { |
| 59 | return true; |
| 60 | } |
| 61 | else if (half_float::detail::builtin_isnan(target) && |
| 62 | half_float::detail::builtin_isnan(reference)) // determine if nan values using existing function |
| 63 | { |
| 64 | return true; |
| 65 | } |
| 66 | |
| 67 | const U epsilon = (std::is_same<half, typename std::remove_cv<U>::type>::value || (reference == 0)) |
| 68 | ? static_cast<U>(0.01) |
| 69 | : static_cast<U>(1e-05); |
| 70 | if (std::abs(static_cast<double>(reference) - static_cast<double>(target)) <= epsilon) |
| 71 | { |
| 72 | return true; |
| 73 | } |
| 74 | else |
| 75 | { |
| 76 | if (static_cast<double>(reference) == 0.0f) |
| 77 | { |
| 78 | return false; // We have checked whether _reference and _target is close. If _reference is 0 but not close to _target, it should return false |
| 79 | } |
| 80 | const double relative_change = |
| 81 | std::abs((static_cast<double>(target) - static_cast<double>(reference)) / reference); |
| 82 | return relative_change <= static_cast<U>(tolerance); |
| 83 | } |
| 84 | } |
| 85 | } // namespace |
| 86 | |
| 87 | TEST_SUITE(CPP) |
no test coverage detected