不借助任何库函数写平方根函数sqrt

来源:互联网 发布:见一落叶而知岁之将暮 编辑:程序博客网 时间:2024/06/07 07:14
#include "stdio.h"#define le  1e-6  //精度是可以修改的double sqrt(double n){double x1,x0;x0=1;while(1){x1=0.5*(x0+n/x0);if((x1-x0>-le)&&(x1-x0<le)) break;//收敛时,退出循环x0= x1;}return x1;}void main(){double n;double re;scanf("%lf",&n);re=sqrt(n);printf("%lf,%lf\n",re,n);}

原创粉丝点击