sqrt函数实现
感谢杨工,让我更加认识到自己技术薄弱,这道题源自于和杨工的非正式面试,当时根本没思路,甚至没和查找有丝毫的联系,看来做自己想做的还是要付出努力的。sqrt()即开平方运算,y=x*x,已知Y的情况下求解X的值,基本的思路是找个区间,逐步计算逼近,知道需要的精度。
(1)二分查找
并不是严格的二分查找,设定寻找的区间,在这个区间中一直取中点,计算中点的平方和Y的查找,逐步逼近,直到自己需要的精度:
#define ABS_FLOAT 0.000001
bool eqs(double val1 , double val2)
{
double diff = fabs(val1 - val2) ;
if(diff < ABS_FLOAT)
{
return true ;
}
else
{
return false ;
}
} //获取开方值,二分查找的方法
double SqrtBybisection(double _value)
{
if (_value <= 0 )
return 0 ; double low = 0.0;
double high = 0.0 ; if (_value > 0 && _value < 1)
{
low = _value;
high = 1.0 ;
}
else
{
low = 1.0 ;
high = _value ;
} double mid = (low + high)/2.0 ;
double last = 0.0 ; do
{
if (mid * mid > _value)
{
high = mid ;
}
else
{
low = mid ;
} last = mid ;
mid = (high + low )/ 2.0 ; //std::cout << mid << std::endl ; }while(! eqs( last , mid)) ; return mid ;
}
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
牛顿迭代法通过泰勒公司展开,通过切线逐步逼近,具体推到可以参考:牛顿逼近 , sqrt实现的代码:
//牛顿迭代法求解
/* f(x) = x^2 - v --> x = x0 - f(x0)/2x0 -->x = (x0 + v / x0) / 2 ;
-->
*/
double SqrtByNewton(const double& _val)
{
double nrt = _val ;
double last_nrt = 0 ;
while (! eqs( nrt , last_nrt))
{
last_nrt = nrt ;
nrt = (nrt + _val / nrt) / 2.0 ;
}
return nrt ;
}
3 技巧算法
看到这种解法,我也很惊讶,程序员真是无底线啊~~
先看看浮点数表示,浮点数不论是float还是double在存储方式上都是遵从IEEE的规范的,float遵从的是IEEE R32.24 ,而double 遵从的是R64.53。
数学中浮点用S=M*2^N, 在计算机中 主要由三部分构成:符号位+指数位(N)+尾数(M),符号位:0为正1为负,指数位:2^M ,移位存储,尾数:即有效数字,规定整数部分为1
float 浮点数内存分布:
| 31 | 30~23 | 22~0 |
| 1 位 符号位 | 8位 指数位 | 23位 尾数 |
| 63 | 62~52 | 51~0 |
| 1 位 符号位 | 11位 指数位 | 52位 尾数 |
| 符号位 | 指数位 | 尾数 |
| 0 | 10000010 | 0001 0000 0000 0000 0000 000 |
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
float sqrtinv(float x)
{
float xhalf = 0.5f*x;
int i = *(int*)&x; // get bits for floating VALUE
i = 0x5f375a86- (i>>1); // gives initial guess y0
x = *(float*)&i; // convert bits BACK to float
x = x*(1.5f-xhalf*x*x); // Newton step, repeating increases accuracy
x = x*(1.5f-xhalf*x*x); // Newton step, repeating increases accuracy
x = x*(1.5f-xhalf*x*x); // Newton step, repeating increases accuracy return 1/x;
}
这个算法速度据说比系统函数还要快,确实,迭代的步骤少来很多,具体解释和浮点的表示有关,可以参考下文:一般而言,一个float数据
共32个bit,和int数据一样。其中前23位为有效数字
,后面接着一个8位数据
表示指数,最后一位表示符号,由于这里被开方的数总是大于0,所以我们暂不考虑最后一个符号位。此时

如果我们把计算机内的浮点数
看做一个整数
,那么

现在开始逐步分析函数。这个函数的主体有四个语句,分别的功能是:
int i = *(int*)&x; 这条语句把
转成
。
i = 0x5f3759df - (i>>1); 这条语句从
计算
。
y = *(float*)&i; 这条语句将
转换为
。
y = y*(1.5f - xhalf*y*y); 这时候的y是近似解;此步就是经典的牛顿迭代法。迭代次数越多越准确。关键是第二步 i = 0x5f3759df - (i>>1); 这条语句从
计算
原理:
令

用
和
带入之后两边取对数,再利用近似表示

算一算就得到:

若取
,
就是程序里所用的常量0x5f3759df。至于为何选择这个
,则应该是曲线拟合实验的结果。
4 测试结果
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

sqrt函数实现的更多相关文章
- 转:一个Sqrt函数引发的血案
转自:http://www.cnblogs.com/pkuoliver/archive/2010/10/06/1844725.html 源码下载地址:http://diducoder.com/sotr ...
- [转载]求平方根sqrt()函数的底层算法效率问题
我们平时经常会有一些数据运算的操作,需要调用sqrt,exp,abs等函数,那么时候你有没有想过:这个些函数系统是如何实现的?就拿最常用的sqrt函数来说吧,系统怎么来实现这个经常调用的函数呢? 虽然 ...
- Sqrt函数高效实现
转自一个Sqrt函数引发的血案 我们平时经常会有一些数据运算的操作,需要调用sqrt,exp,abs等函数,那么时候你有没有想过:这个些函数系统是如何实现的?就拿最常用的sqrt函数来说吧,系统怎么来 ...
- 一个Sqrt函数引发的血案(转)
作者: 码农1946 来源: 博客园 发布时间: 2013-10-09 11:37 阅读: 4556 次 推荐: 41 原文链接 [收藏] 好吧,我承认我标题党了,不过既然你来了, ...
- 【转载】一个Sqrt函数引发的血案
转自:http://www.cnblogs.com/pkuoliver/archive/2010/10/06/sotry-about-sqrt.html 源码下载地址:http://diducoder ...
- 一个Sqrt函数引发的血案
源码下载地址:http://diducoder.com/sotry-about-sqrt.html 好吧,我承认我标题党了,不过既然你来了,就认真看下去吧,保证你有收获. 我们平时经常会有一些数据运算 ...
- sqrt函数实现(神奇的算法)
我们平时经常会有一些数据运算的操作,需要调用sqrt,exp,abs等函数,那么时候你有没有想过:这个些函数系统是如何实现的?就拿最常用的sqrt函数来说吧,系统怎么来实现这个经常调用的函数呢? 虽然 ...
- php sqrt()函数 语法
php sqrt()函数 语法 作用:sqrt()函数的作用是对参数进行求平方根 语法:sqrt(X) 参数: 参数 描述 X 进行求平方根的数字 说明:返回将参数X进行开平方后的结果江苏大理石平台 ...
- PHP sqrt() 函数
实例 返回不同数的平方根: <?phpecho(sqrt(0) . "<br>");echo(sqrt(1) . "<br>"); ...
随机推荐
- 基于Twemproxy的Redis集群方案
概述 由于单台redis服务器的内存管理能力有限,使用过大内存redis服务器的性能急剧下降,且服务器发生故障将直接影响大面积业务.为了获取更好的缓存性能及扩展型,我们将需要搭建redis集群来满足需 ...
- l中断的实现
转自:http://blog.chinaunix.net/uid-25014876-id-90740.html xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ...
- **对比$_POST、$GLOBALS['HTTP_RAW_POST_DATA']和file_get_contents('php://input')
最近在开发微信接口,又学到了一些新的技术点,今天就把学到的关于接收数据的技术点给简单的罗列下. public function __construct($token, $wxuser = ''){ $ ...
- 个人电脑配置FTP服务器,四张图搞定。项目需要,并自己写了个客户端实现下载和上传的功能!
测试结果:
- PDA手持终端实现零售行业商场和超市仓储管理和销售开单自动化和系统化
近几年,连锁药店得到了迅速扩张,由于药品种类过于繁杂.客户分散票据过多,急剧扩张之后的连锁药店企业在信息流处理上遇到了各种各样的问题 到现在已经有3000多家连锁药房,需要采购的正规药品就有数万个品种 ...
- Extjs3.3 + swfUpload2.2 实现多文件上传组件
[该上传组件已经停止更新,该上传组件已经在项目中使用.在使用过程中如果发现bug请大家回复此贴.2011-02-27] 主要是为了用swfUpload实现上传,为了新鲜好玩. 理解swfUpload可 ...
- Codeforces Round #192 (Div. 2) A. Cakeminator
#include <iostream> #include <vector> using namespace std; int main(){ int r,c; cin > ...
- Html5的DeviceOrientation特性
设备定位API 引用W3C中的设备定位API的规范描述可知,该API“……定义了多种新型DOM事件,旨在提供与主机设备相关的物理朝向与运动状态信息.”由API提供的数据产生自多种来源,其中包括设备上的 ...
- 【BZOJ】3223: Tyvj 1729 文艺平衡树(splay)
http://www.lydsy.com/JudgeOnline/problem.php?id=3223 默默的.. #include <cstdio> #include <cstr ...
- 【BZOJ】1041: [HAOI2008]圆上的整点(几何)
http://www.lydsy.com:808/JudgeOnline/problem.php?id=1041 所谓的神题,我不会,直接题解..看了半天看懂题解了.详见hzwer博客 这题呢,我只能 ...