newtonmethod.c

来自「自己做的常用库和实现的数据结构。public domain.」· C语言 代码 · 共 25 行

C
25
字号
/* Demo of Newton Method.
*/

/* abs */
#define abs(x) ((x<0)?-(x):x)

float sqrt (x,acc)
float x,acc;
{
  float guess=1;

  while (abs(guess*guess-x)>=acc)
    guess = (x/guess+guess)/2;
  return (guess);
}

main ()
{
  printf ("square root of 2.0 with the accuarcy of 0.0001 is %f \n",
			      sqrt(2.0,0.0001));
  printf ("square root of 2.0 with the accuarcy of 0.01 is %f\n",
			      sqrt (2.0,0.01));
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?