How to write a drop-in replacements for fundamental type (float) that override operator== in C++?
<p>We can not compare binary floating point directly. 
I am writting a drop-in replacements class for float 
which override the bulti-in compare operators::</p>

<pre><code>template&lt;class U&gt;
class Float {
    private:
        U val;
    public:
        Float(U v = 0): val(v) {}
        operator U() const { return val; }

        friend bool operator&lt;(Float a, Float b) { return a.val + 1e-6 &lt; b.val; }
        friend bool operator==(Float a, Float b) { return !(a &lt; b) &amp;&amp; !(b &lt; a); }

        friend Float operator*(Float a, Float b) { return a.val * b.val; }
        template&lt;class T&gt;
        friend Float operator*(T a, Float b) { return a * b.val; }
        template&lt;class T&gt;
        friend Float operator*(Float a, T b) { return a.val * b; }
};
</code></pre>

<p>Now we can write something like this::</p>

<pre><code>#include&lt;assert.h&gt;
int main() {
    Float&lt;double&gt; a = 0.2, b = 0.02;
    assert(a * a == 2 * b);
}
</code></pre>

<p>However, this code will show a unexpected behavior::</p>

<pre><code>#include&lt;complex&gt;
int main() {
    std::complex&lt; Float&lt;double&gt; &gt; a(0.2);
    a * 2.0;
}
</code></pre>

<p>It will call <code>Float::operator*(std::complex&lt;Float&lt;double&gt; &gt;, Float)</code> again and again 
like a recursive endless loop, and finally get stack overflow.
How to fix this?</p>

<p><strong>Edit</strong></p>

<p>DeadMG and Charles Bailey point out that From ISO/IEC 14882:2011, 26.4: "The effect of instantiating the template complex for any type other than float, double, or long double is unspecified."</p>

<p>Maybe I have given a wrong counter example. We could still discuss how to write a good drop-in replacements class for fundamental type.</p>

<p>Let me clear my motivation, <code>assert(0.1 * 0.1 == 0.01);</code> is counterintuitive. That is why I write a Float class which used "almost equal" behavior to compare two floating number.</p>
