title: GCC problem with raw double type comparisons
body: <p>I have the following bit of code, however when compiling it with GCC 4.4 with various optimization flags I get some unexpected results when its run.</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  int main()
  {
     const unsigned int cnt = 10;
     double lst[cnt] = { 0.0 };
     const double v[4] = { 131.313, 737.373, 979.797, 731.137 };
  
     for(unsigned int i = 0; i &lt; cnt; ++i) {
        lst[i] = v[i % 4] * i;
     }
  
     for(unsigned int i = 0; i &lt; cnt; ++i) {
        double d = v[i % 4] * i;
        if(lst[i] != d) {
           std::cout &lt;&lt; "error @ : " &lt;&lt; i &lt;&lt; std::endl;
           return 1;
        }
     }
     return 0;
  }
  </code></pre>
  
  <ul>
  <li><p>when compiled with: <strong><em>"g++ -pedantic -Wall -Werror -O1 -o test test.cpp"</em></strong> I get the following output: "error @ : 3"</p></li>
  <li><p>when compiled with: <strong><em>"g++ -pedantic -Wall -Werror -O2 -o test test.cpp"</em></strong> I get the following output: "error @ : 3"</p></li>
  <li><p>when compiled with: <strong><em>"g++ -pedantic -Wall -Werror -O3 -o test test.cpp"</em></strong> I get no errors</p></li>
  <li><p>when compiled with: <strong><em>"g++ -pedantic -Wall -Werror -o test test.cpp"</em></strong> I get no errors</p></li>
  </ul>
  
  <p>I do not believe this to be an issue related to rounding, or epsilon difference in the comparison. I've tried this with Intel v10 and MSVC 9.0 and they all seem to work as expected. I believe this should be nothing more than a bitwise compare.</p>
  
  <p>If I replace the if-statement with the following:  <strong><em><code>if (static_cast&lt;long long int&gt;(lst[i]) != static_cast&lt;long long int&gt;(d))</code></em></strong>, and add "-Wno-long-long" I get no errors in any of the optimization modes when run.</p>
  
  <p>If I add <strong><em><code>std::cout &lt;&lt; d &lt;&lt; std::endl;</code></em></strong> before the "return 1", I get no errors in any of the optimization modes when run.</p>
  
  <p>Is this a bug in my code, or is there something wrong with GCC and the way it handles the double type?</p>
  
  <p><strong>Note:</strong> I've just tried this with gcc versions 4.3 and 3.3, the error is not exhibited.</p>
  
  <p><strong>Resolution:</strong> Mike Dinsdale noted the following bug report: <a href="http://gcc.gnu.org/bugzilla/show_bug.cgi?id=323" rel="noreferrer">http://gcc.gnu.org/bugzilla/show_bug.cgi?id=323</a>  It seems the GCC team are not <em>completely sure</em> about nature of problem. </p>
  
  <p>As suggested in the bug report a possible resolution is to use the ffloat-store option. I've tried this and it works, however the results from a performance point of view are not that great, though ymmv.</p>