title: Same numerical code returns different output whether it is in C++ or C
body: <p>I was given an example C++ numerical code calculating derivatives in three different ways. I have to convert it to C. I thought it wouldn't give me any problems due to original using cmath anyway, but I was so wrong.  Here is the original code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include&lt;string&gt;
  #include &lt;cmath&gt;
  #include &lt;fstream&gt;
  using namespace std;
  
  
  double metoda_pochodna_1(int x, double h)
  {
      return (sin(x+h) - sin(x)) / h;
  }   
  double metoda_pochodna_2(int x, double h)
  {
      return (sin(x+(0.5*h)) - sin(x-(0.5*h))) / h;
  }
  double metoda_pochodna_3(int x, double h)
  {
      return ((sin(x-2*h) - 8*sin(x-h) + 8*sin(x+h) - sin(x+2*h)) / (12*h));
  }
  
  
  int main()
  {
      double h, w1, w2, w3, kos = cos(1.0);
      int x=1;
      ofstream wyniki;
      wyniki.open("wyniki.dat");
  
      for (h = pow(10.0, -15.0); h &lt; 1; h *= 1.01) 
      {
          w1 = log10(abs(metoda_pochodna_1(x,h) - kos));
          w2 = log10(abs(metoda_pochodna_2(x,h) - kos));
          w3 = log10(abs(metoda_pochodna_3(x,h) - kos));
          wyniki &lt;&lt; log10(h)&lt;&lt;"  "&lt;&lt; w1 &lt;&lt;"  "&lt;&lt; w2 &lt;&lt;"  "&lt;&lt; w3 &lt;&lt; "\n";
          cout &lt;&lt; log10(h)&lt;&lt;"  "&lt;&lt; w1 &lt;&lt;"  "&lt;&lt; w2 &lt;&lt;"  "&lt;&lt; w3 &lt;&lt; "\n";
      }
  
      wyniki.close();
      cout &lt;&lt; endl;
      /*system("pause");*/ //uruchamiane z windowsa
      return 0;
  }
  </code></pre>
  
  <p>And here is my C version; I just changed file handling. NOTE: I changed to <code>long double</code> during troubleshooting , but the resulting output is exactly the same as the version using regular <code>double</code>.</p>
  
  <pre><code>#include &lt;math.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  
  
  long double metoda_pochodna_1(int x,long  double h)
  {
      return (sin(x+h) - sin(x)) / h;
  }   
  long double metoda_pochodna_2(int x,long  double h)
  {
      return (sin(x+(0.5*h)) - sin(x-(0.5*h))) / h;
  }
  long double metoda_pochodna_3(int x, long double h)
  {
      return ((sin(x-2*h) - 8*sin(x-h) + 8*sin(x+h) - sin(x+2*h)) / (12*h));
  }
  
  
  int main()
  {
      long double h, w1, w2, w3, kos = cos(1.0);
      int x=1;
      FILE * file;
      file = fopen("wyniki.dat","w+");
  
  
      for (h = pow(10.0, -15.0); h &lt; 1; h *= 1.01) 
      {
          w1 = log10(abs(metoda_pochodna_1(x,h) - kos));
          w2 = log10(abs(metoda_pochodna_2(x,h) - kos));
          w3 = log10(abs(metoda_pochodna_3(x,h) - kos));
          fprintf(file,"%f %Lf %Lf %Lf\n",log10(h), w1,w2,w3);
          printf("%f %Lf %Lf %Lf\n",log10(h), w1,w2,w3);
          //wyniki &lt;&lt; log10(h)&lt;&lt;"  "&lt;&lt; w1 &lt;&lt;"  "&lt;&lt; w2 &lt;&lt;"  "&lt;&lt; w3 &lt;&lt; "\n";
          //cout &lt;&lt; log10(h)&lt;&lt;"  "&lt;&lt; w1 &lt;&lt;"  "&lt;&lt; w2 &lt;&lt;"  "&lt;&lt; w3 &lt;&lt; "\n";
      }
  
      fclose(file);
      printf("\n");
      return 0;
  }
  </code></pre>
  
  <p>This is the output of the C++ code, which I am assuming correct:</p>
  
  <blockquote>
  <pre><code>-15  -1.82947  -1.82947  -1.28553
  
  -14.9957  -2.03091  -2.03091  -1.33768
  
  14.9914  -2.41214  -2.41214  -1.39632
  
  -14.987  -2.81915  -2.81915  -1.46341
  </code></pre>
    
    <p>[...]</p>
  </blockquote>
  
  <p>And this is the output of the C version, which obviously differs (additional precision in front row is from <code>long double</code>, but the other three columns are all <code>-inf</code> regardless of whether <code>double</code> or <code>long double</code> is used):</p>
  
  <blockquote>
  <pre><code>-15.000000 -inf -inf -inf
  
  -14.995679 -inf -inf -inf
  
  -14.991357 -inf -inf -inf
  
  -14.987036 -inf -inf -inf
  </code></pre>
    
    <p>[...]</p>
  </blockquote>
  
  <p>What is wrong with my converted code?</p>