Is it ok to compare floating points to 0.0 without epsilon?
<p>I am aware, that to compare two floating point values one needs to use some epsilon precision, as they are not exact. However, I wonder if there are edge cases, where I don't need that epsilon.</p>

<p>In particular, I would like to know if it is always safe to do something like this:</p>

<pre><code>double foo(double x){
    if (x &lt; 0.0) return 0.0;
    else return somethingelse(x); // somethingelse(x) != 0.0
}

int main(){
   int x = -3.0;
   if (foo(x) == 0.0) { 
     std::cout &lt;&lt; "^- is this comparison ok?" &lt;&lt; std::endl; 
   }
}
</code></pre>

<p>I know that there are better ways to write <code>foo</code> (e.g. returning a flag in addition), but I wonder if in general is it ok to assign <code>0.0</code> to a floating point variable and later compare it to <code>0.0</code>.</p>

<p>Or more general, does the following comparison yield true always?</p>

<pre><code>double x = 3.3;
double y = 3.3;
if (x == y) { std::cout &lt;&lt; "is an epsilon required here?" &lt;&lt; std::endl; }
</code></pre>

<p>When I tried it, it seems to work, but it might be that one should not rely on that. </p>