title: Why does this code for newton's method not work in C (using a while loop)?
body: <pre><code>#include &lt;stdio.h&gt;
  #include &lt;math.h&gt;
  
  int main()
  {
      // ax^3 + bx^2 + cx + d = 0
      float a,b,c,d,guess,derivative;
      double functional_value, accuracy;
  
      printf(&quot;Please enter values for a,b,c,d respectively :\n&quot;);
      scanf(&quot;%f %f %f %f&quot;, &amp;a, &amp;b, &amp;c, &amp;d);
  
      printf(&quot;Please enter your best guess for a root:\n&quot;);
      scanf(&quot;%f&quot;, &amp;guess);
  
      printf(&quot;How close you want the value to be ?\n&quot;);
      scanf(&quot;%f&quot;, &amp;accuracy);
  
      while (fabs(functional_value) &lt;= accuracy &amp;&amp; derivative != 0)
      {
          functional_value = a*pow(guess,3) + b*pow(guess,2) + c*(guess) + d ;
          // 3ax^2 + 2bx + c
          derivative = 3*a* pow(guess,2) + 2*b*(guess) + c ;
          guess -= ((functional_value)/(derivative)) ;
      }
  
      printf(&quot;A root of the equation is approximately equal to %f&quot;, guess);
  
      return 0;
  }
  </code></pre>
  <p>I am new to programming, I was just learning C in college. And I wanted to make a program that would calculate roots using newton's method for up to cubic polynomials. So I wrote the above code.</p>
  <p>But when I run it, the loop seems to run infinitely. I even tried giving the inputs as 0 1 -2 1 (a quadratic equation whose root is 1) but the loop is still running.</p>
  <p>I don't notice any mistakes. Can anyone please help me in finding my mistake ? like what's wrong ?</p>
  <p>At first I didn't have any parameter for the accuracy i.e. how close one would want the root to be, so I added that as a double.</p>
  <p>I have looked at somewhat similar questions but I don't know all the things in C they are using.</p>
  <p>Edit#1 : Thanks for the comments and pointing out my foolish syntax mistakes</p>
  <p>Edit#2 : Now the program runs but it doesn't calculate a root still. I get the following output</p>
  <pre><code>Please enter values for a,b,c,d respectively :
  0 1 -2 1
  Please enter your best guess for a root:
  0.5
  How close you want the value to be ?
  0.01
  A root of the equation is approximately equal to nan
  Process finished with exit code 0
  </code></pre>
  <p>Edit#3 : Thanks to Eric Postpichilli now the program does calculate a root after using fabs() and comparing the functional_value to be less than or equal to accuracy. But I still can't understand why will a division by zero occur, I have used an &quot;AND&quot; operator in the loop condition to try and prevent that</p>