title: Getting wrong square-root estimation
body: <p>I have written a program in C which is calculating for me the square root with the <a href="https://en.wikipedia.org/wiki/Methods_of_computing_square_roots" rel="nofollow noreferrer">heron procedure</a>. <code>x</code> is my number, <code>r</code> is estimated value and <code>steps</code> are steps. I want to output the difference between the exact value and the value obtained by the heron method. But it seems that my function is not correct. For my calculated value I get no value. Can anyone help me?</p>
  <pre class="lang-c prettyprint-override"><code>#include &lt;stdio.h&gt;
  #include &lt;math.h&gt;
      
  int heron (x, r, steps)
  {
    int k = 0;
    double xold, xnew;
    double rel_error = 1.0;
  
    while(k &lt;= steps &amp;&amp; rel_error &gt; 1e-4) {
      ++k;
      xnew = .5 * (xold + x / xold);
      rel_error = (xnew - xold) / xnew;
      if(rel_error &lt; 0) 
        rel_error = -rel_error;
        xold = xnew;
      }
      printf(&quot;exact value: %.10f\n&quot;, sqrt(x));
      return (xnew);
  }
      
  int main()
  {
    int x=4, r=10, steps=50;
    printf(&quot;%f\n&quot;, heron(x, r, steps));
    return 0;
  }
  </code></pre>