title: Float addition promoted to double?
body: <p>I had a small WTF moment this morning. Ths WTF can be summarized with this:</p>
  
  <pre><code>float x = 0.2f;
  float y = 0.1f;
  float z = x + y;
  assert(z == x + y); //This assert is triggered! (Atleast with visual studio 2008)
  </code></pre>
  
  <p>The reason seems to be that the expression <code>x + y</code> is promoted to double and compared with the truncated version in <code>z</code>. (If i change <code>z</code> to <code>double</code> the assert isn't triggered).</p>
  
  <p>I can see that for precision reasons it would make sense to perform all floating point arithmetics in double precision before converting the result to single precision. I found the following paragraph in the standard (which I guess I sort of already knew, but not in this context):</p>
  
  <p><strong>4.6.1.</strong>
  "An rvalue of type <code>float</code> can be converted to an rvalue of type <code>double</code>. The value is unchanged"</p>
  
  <p>My question is, is <code>x + y</code> guaranteed to be promoted to double or is at the compiler's discretion?</p>
  
  <p><strong>UPDATE:</strong> Since many people has claimed that one shouldn't use <code>==</code> for floating point, I just wanted to state that in the specific case I'm working with, an exact comparison is justified.</p>
  
  <p>Floating point comparision <em>is</em> tricky, here's an interesting <a href="http://realtimecollisiondetection.net/blog/?p=89" rel="nofollow noreferrer">link</a> on the subject which I think hasn't been mentioned.</p>