牛顿拉普森迭代法

来源:互联网 发布:阿里云域名交易市场 编辑:程序博客网 时间:2024/05/22 01:41
 1 //计算某数的平方根  2   #include <stdio.h> 3   4   float absoluteValue(float x) 5   { 6       if(x<0) 7       { 8           x=-x; 9       }10       return x;11   } 12  13   //计算某数平方根的函数14   float squareRoot(float x)15   {16       const float epsilon=.00001;17       float guess=1.0;18  19       while(absoluteValue(guess*guess-x)>=epsilon)20       {21           guess=(x/guess+guess)/2.0; 22       }23       return guess; 24   } 25  26   int main()27   {28       float num;29       printf("输入要求平方根的数:");30       scanf("%f",&num);31       printf("%f平方根是%f\n",num,squareRoot(num));32       return 0; 33   }

 


<script type="text/javascript"><!--google_ad_client = "ca-pub-1944176156128447";/* cnblogs 首页横幅 */google_ad_slot = "5419468456";google_ad_width = 728;google_ad_height = 90;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
原创粉丝点击