| 7730 | } |
| 7731 | |
| 7732 | template <typename FP> bool almostEqualUlps(FP lhs, FP rhs, int maxUlpDiff) { |
| 7733 | // Comparison with NaN should always be false. |
| 7734 | // This way we can rule it out before getting into the ugly details |
| 7735 | if (std::isnan(lhs) || std::isnan(rhs)) { |
| 7736 | return false; |
| 7737 | } |
| 7738 | |
| 7739 | auto lc = convert(lhs); |
| 7740 | auto rc = convert(rhs); |
| 7741 | |
| 7742 | if ((lc.i < 0) != (rc.i < 0)) { |
| 7743 | // Potentially we can have +0 and -0 |
| 7744 | return lhs == rhs; |
| 7745 | } |
| 7746 | |
| 7747 | auto ulpDiff = std::abs(lc.i - rc.i); |
| 7748 | return ulpDiff <= maxUlpDiff; |
| 7749 | } |
| 7750 | |
| 7751 | } // namespace |
| 7752 | |