| 7933 | |
| 7934 | template <typename FP> |
| 7935 | bool almostEqualUlps(FP lhs, FP rhs, int maxUlpDiff) { |
| 7936 | // Comparison with NaN should always be false. |
| 7937 | // This way we can rule it out before getting into the ugly details |
| 7938 | if (std::isnan(lhs) || std::isnan(rhs)) { |
| 7939 | return false; |
| 7940 | } |
| 7941 | |
| 7942 | auto lc = convert(lhs); |
| 7943 | auto rc = convert(rhs); |
| 7944 | |
| 7945 | if ((lc.i < 0) != (rc.i < 0)) { |
| 7946 | // Potentially we can have +0 and -0 |
| 7947 | return lhs == rhs; |
| 7948 | } |
| 7949 | |
| 7950 | auto ulpDiff = std::abs(lc.i - rc.i); |
| 7951 | return ulpDiff <= maxUlpDiff; |
| 7952 | } |
| 7953 | |
| 7954 | } |
| 7955 | |