title: Accuracy of Rosenbrock's test function calculation
body: <p>I want to calculate Rosenbrock's test function</p>
  
  <p><img src="https://i.stack.imgur.com/MgJqd.png" alt="enter image description here"></p>
  
  <p>I have implemented the following C/C++ code</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  /********/
  /* MAIN */
  /********/
  int main()
  {
      const int N = 900000;
  
      float *x = (float *)malloc(N * sizeof(float));
      for (int i=0; i&lt;N; i++) x[i] = 3.f;
  
      float sum_host = 0.f;
      for (int i=0; i&lt;N-1; i++) {
          float temp = (100.f * (x[i+1] - x[i] * x[i]) * (x[i+1] - x[i] * x[i]) + (x[i] - 1.f) * (x[i] - 1.f));
          sum_host = sum_host + temp;
          printf("%i %f %f\n", i, temp, sum_host);
      }
      printf("Result for Rosenbrock's test function calculation = %f\n", sum_host);
  
  }
  </code></pre>
  
  <p>Since the <code>x</code> array is initialized to <code>3.f</code>, then each summation term should be <code>3604.f</code>, so that the final summation involving <code>899999</code> terms should be <code>3243596396</code>. However, the result I get is <code>3229239296</code>, with an absolute error of <code>14357100</code>. If I measure the difference between two consecutive partial summations, I see that it is <code>3600.f</code> for the early partial summations and then it drops to <code>3584</code> for the last ones, while it should always be <code>3604.f</code>.</p>
  
  <p>If I use Kahan summation algorithm as</p>
  
  <pre><code>sum_host = 0.f;
  float c        = 0.f;
  for (int i=0; i&lt;N-1; i++) {
      float temp = (100.f * (x[i+1] - x[i] * x[i]) * (x[i+1] - x[i] * x[i]) + (x[i] - 1.f) * (x[i] - 1.f)) - c;
      float t    = sum_host + temp;
      c          = (t - sum_host) - temp;
      sum_host = t;
  }
  </code></pre>
  
  <p>the result I get is <code>3243596288</code>, with a much smaller absolute error of <code>108</code>.</p>
  
  <p>I'm pretty sure that this effect I see should be ascribed to the precision of floating point arithmetics. Could someone confirm this and provide me an explanation of the mechanism according to which this occurs? </p>