title: Different evaluation when compiling with -O3
body: <p>The following Code has different behavior, when it is compiled with <code>g++ -O3 ..</code> and without <code>-O3</code>. </p>
  
  <p>I know that <code>updateError</code> is not returning anything, which can lead to undefined behavior. What i don't understand is, that <code>error</code> is set to zero inside the loop if compiled with -O3, and <code>if(error &gt; eps)</code> is evaluated as false, while it is still evaluated as true in the <code>while()</code> statement. .</p>
  
  <p>I have also tried to change it to a do{}while() loop, in that case it worked also with -O3...</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  
  using namespace std;
  
  
  double updateError(){
      printf("updating Error\n");
  }
  
  int main(int argc, char *argv[]){
  
      double eps = 1.e-10;
      double error = 2*eps;
      int iter = 0;
  
      while(error &gt; eps){
          error = updateError();
          iter++;
          printf("Error on iteration %i: %.20e\n", iter, error);
          if(error &gt; eps) printf("error bigger\n\n");
          else printf("error smaller\n\n");
          if(iter == 5){
              printf("not converged in 5 iterations!!\n");
              break;
          }
      }
      return 0;
  }
  </code></pre>
  
  <p>Console output with -O3:</p>
  
  <pre><code>updating Error
  Error on iteration 1: 0.00000000000000000000e+00
  error smaller
  
  updating Error
  Error on iteration 2: 0.00000000000000000000e+00
  error smaller
  
  updating Error
  Error on iteration 3: 0.00000000000000000000e+00
  error smaller
  
  updating Error
  Error on iteration 4: 0.00000000000000000000e+00
  error smaller
  
  updating Error
  Error on iteration 5: 0.00000000000000000000e+00
  error smaller
  
  not converged in 5 iterations!!
  </code></pre>
  
  <p>Console output without -O3:</p>
  
  <pre><code>updating Error
  Error on iteration 1: 6.92743341769227318242e-310
  error smaller
  </code></pre>