Generic and accurate floating point "equality" Like those who have come before me, I foolishly have sought to implement a generic, efficient, idiomatic, and most importantly correct method of comparing floating point numbers for equality. Like those who have come before me, I have most likely failed in at least one of those regards.  I'd appreciate comments on any aspect of the following code

#include &lt;cmath&gt;
#include &lt;cstddef&gt;
#include &lt;cstdint&gt;
#include &lt;limits&gt;
#include &lt;type_traits&gt;

template&lt;bool B, typename T, typename DEFAULT = void&gt;
struct cond
{
    static constexpr bool value = B;
    using default_type = DEFAULT;
    using type = T;
};

template&lt;typename Head, typename... Tail&gt;
struct select
{
    using type = 
        typename std::conditional&lt;Head::value, 
                                 typename Head::type, 
                                 typename select&lt;Tail...&gt;::type&gt;::type;
};

template &lt;typename T&gt;
struct select&lt;T&gt;
{
    using type = T;
};

template &lt;bool B, typename T, typename DEFAULT&gt;
struct select&lt;cond&lt;B, T, DEFAULT&gt;&gt;
{
    using type = typename std::conditional&lt;B, T, DEFAULT&gt;::type;
};

template &lt;typename Head, typename... Tail&gt;
using select_t = typename select&lt;Head, Tail...&gt;::type;

template &lt;typename FloatingPoint&gt;
struct SelectIntegralOfSameSize {
    using type = select_t&lt;
        cond&lt;sizeof(FloatingPoint) == sizeof(int64_t), int64_t&gt;,
        cond&lt;sizeof(FloatingPoint) == sizeof(int32_t), int32_t&gt;,
        cond&lt;sizeof(FloatingPoint) == sizeof(int16_t), int16_t&gt;,
        cond&lt;sizeof(FloatingPoint) == sizeof(int8_t), int8_t&gt;
    &gt;;
    static_assert(!std::is_void&lt;type&gt;::value, "Could not find sane integral type");
};

template &lt;typename FP, typename I = typename SelectIntegralOfSameSize&lt;FP&gt;::type&gt;
union Fp_Int_Union {
    FP fp_;
    I i_;
};


template &lt;typename FP&gt;
bool nearlyEqual(FP left, FP right, FP maxDiff, std::size_t maxUlpsDiff) {
    // handle NaNs and infinities
    if (!std::isfinite(left) || !std::isfinite(right)) {
        return false;
    }

    // ULP comparison breaks for different signs
    if ((left &lt; 0) != (right &lt; 0)) {
        // Handle -0 == +0
        return left == right;
    }

    // ULP comparison fails near zero
    if (std::abs(left - right) &lt;= maxDiff) {
        return true;
    }

    Fp_Int_Union&lt;FP&gt; l, r;
    l.fp_ = left;
    r.fp_ = right;

    return std::abs(l.i_ - r.i_) &lt;= maxUlpsDiff;
}
Consider using std::nextafter()

The math library includes the function std::nextafter() that will give you the next floating point number with the smallest possible, strictly positive difference with the given number. This is much safer than union-casting a float into an int, because there is no guarantee that two close floating point numbers will also be close as integers. In particular, that is only true if the exponent of both floating point numbers is the same. For example:

bool closeEnough = false;

if (left &lt;= right) {
    do {
        if (left &gt;= right) {
            closeEnough = true;
            break;
        }
        left = std::nextafter(left);
    } while (maxUlpsDiff--);
} else {
    // handle the left &gt; right case
}

return closeEnough;


You'd obviously only want to use this method for small values of maxUlpsDiff.

Is it really what you need?

You are trying to check whether two floating point numbers are close, for two possible ways of closeness: floating point difference, and ULP distance. Is that really what you need? Are you sure that the values you choose for maxDiff and maxUlpsDiff are exactly right? If not, you might get false positives or negatives.

Can you reformulate your floating point calculations into integer calculations? Integers may have a limitted range, but at least they are exact. Maybe you can use them to perform fixed-point arithmetic, or perhaps you can use two integers to form a rational number.