- float SqrtByBisection(float n) //用二分法
- {
- if(n<0) //小于0的按照你需要的处理
- return n;
- float mid,last;
- float low,up;
- low=0,up=n;
- mid=(low+up)/2;
- do
- {
- if(mid*mid>n)
- up=mid;
- else
- low=mid;
- last=mid;
- mid=(up+low)/2;
- }while(abs(mid-last) > eps);//精度控制
- return mid;
- }
- //该片段来自于http://www.codesnippet.cn/detail/0203201511821.html
来源: http://www.codesnippet.cn/detail/0203201511821.html